2

I want to set a size(A4) to an existing document.

I am using pdfbox for watermarking. I used the following link to add watermark. Here I am using another file in which watermark text is there. Latter we are only adding this layer as overlay to original file.

Here the problem arises when file with watermark text is with different size than original document to which the watermark is to be added. In those case the watermark is not getting added properly in terms of position.

Version: I am using pdfbox 1.8. I tried with 2.0 but I am more comfortable with this version.

Here is the code

            PDDocument originalPdfFile = PDDocument.load(filename); 
            PDRectangle pdRect=new PDRectangle(595, 842);//Here I am setting height and width in terms of points
            List PageList = originalPdfFile.getDocumentCatalog().getAllPages();
            int noOfPages=PageList.size();
            System.out.println("No of pages in original document="+noOfPages);
            PDPage page=new PDPage();
            //PDPage page=new PDPage(PDPage.PAGE_SIZE_A4);
            //Here also I tried to add page size
            for (int i = 0; i < PageList.size(); i++) {
                page=(PDPage)PageList.get(i);
                System.out.println("Original Document size in page before cropping: "+(i+1)+", Page Resolution: "+page.getMediaBox());
                page.setMediaBox(pdRect);
                System.out.println("Original Document size in page after cropping: "+(i+1)+", Page Resolution: "+page.getMediaBox());
                //System.out.println("Original Document size in page: "+i+", Height: "+page.getMediaBox().getHeight()+",Width: "+page.getMediaBox().getWidth());
                PDRectangle rec=page.getMediaBox();
                generateWatermarkText(organisationName,rec);
            }


            HashMap<Integer, String> overlayGuide = new HashMap<Integer, String>();
            for(int i=0; i<originalPdfFile.getNumberOfPages(); i++)
            {
                overlayGuide.put(i+1, "C:/drm/final/final.pdf");
                //watermarktext.pdf is the document which is a one page PDF with your watermark image in it.

            }
            Overlay overlay = new Overlay();
            overlay.setInputPDF(originalPdfFile);
            overlay.setOutputFile(filename);
            overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
            overlay.overlay(overlayGuide,false);
           //pdf will have the original PDF with watermarks.       

The above code add watermark successfully but I am not able to shrink the page. This line PDRectangle pdRect=new PDRectangle(595, 842); crops the page but it cuts the contains of the page, which I don't want. I want the contains but to should be fit in that page and the page should be of specified size(like A4 in my case).

Community
  • 1
  • 1
sandy
  • 93
  • 2
  • 9
  • 1
    in 2.0.* call `setMediaBox(PDRectangle.A4);` but I suspect that it is more complex. If so, then please edit your question to include your code and link to the PDFs, and mentiom your PDFBox version. – Tilman Hausherr Oct 28 '16 at 11:39
  • In my case the original pdf document contains pages which are not of same size,some have more width than other. In that case whatever watermark I am using is not applied properly. – sandy Oct 28 '16 at 12:33
  • You only set the mediabox. You might also have to set the cropbox. – mkl Oct 28 '16 at 16:07
  • how to use cropbox to set size? – sandy Oct 30 '16 at 13:57
  • I used page.setMediaBox(PDPage.PAGE_SIZE_A4); But when I checked the file in acrobat reader its showing old size only. – sandy Oct 30 '16 at 15:23
  • I tried with cropbox,it added blank header n footer to the document..I tried with some size(A4),if page is less than that size,its adding margin to fill rest of the areas with it – sandy Oct 31 '16 at 05:27

0 Answers0