How to convert existing pdf files to A4 size using pdfbox?
<p>I want to set a size(A4) to an existing document.</p>

<p>I am using pdfbox for watermarking. I used the following <a href="https://stackoverflow.com/questions/8929954/watermarking-with-pdfbox">link</a> 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. </p>

<p>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.</p>

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

<p>Here is the code</p>

<pre><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 &lt; 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&lt;Integer, String&gt; overlayGuide = new HashMap&lt;Integer, String&gt;();
            for(int i=0; i&lt;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.       
</code></pre>

<p><strong>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).</strong> </p>