使用Apache PDFBox进行PDF页面扩展

时间:2018-05-22 06:15:40

标签: java apache pdf pdf-generation pdfbox

我正在尝试调整PDF大小以在底部创建一个用于页脚水印的自由空间。无论方向和页面大小如何,都需要创建页脚空间。我实施的用于重新缩放页面的代码如下:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
        //String pdfFilename = "/MuhimbiPOC/Templates/Source_doc_withheaderfooter.pdf";
        //String pdfFilename = "/MuhimbiPOC/Templates/3.pdf";
        //String pdfFilename = "C:\\Users\\H176298\\Documents\\Documents\\ERS\\ERS-Build_20.0_Muhimbi\\Sample Files\\Pages_from_annual_report_2009.pdf";
        String pdfFilename = "C:\\Users\\H176298\\Documents\\Documents\\ERS\\ERS-Build_20.0_Muhimbi\\Sample Files\\extract_A17_FlightPlan.pdf";
        PDDocument document = PDDocument.load(new File(pdfFilename));
        PDDocument documentPDF = new PDDocument();
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        PDPage pge = new PDPage();
        int pageCounter = 0;
        for (PDPage page : document.getPages())
        {

            int rotation = page.getRotation();
            final PDRectangle mediaBox = page.getMediaBox();
            final PDRectangle cropBox = page.getCropBox();
            boolean isLandscape = mediaBox.getWidth() > mediaBox.getHeight();
            /**
             * Code to Detect the Portrait Mode of Page
             */
            boolean rotate = rotation == 90 || rotation == 270;
            System.out.println("Landscape  " + isLandscape);
            System.out.println("pageSize.getHeight() :: " + cropBox.getHeight());
                System.out.println("pageSize.getWidth() :: " + cropBox.getWidth());
                System.out.println("pageSize.getUpperRightX() :: " + cropBox.getUpperRightX());
                System.out.println("pageSize.getUpperRightY() :: " + cropBox.getUpperRightY());
                System.out.println("pageSize.getLowerLeftX() :: " + cropBox.getLowerLeftX());
                System.out.println("pageSize.getLowerLeftY() :: " + cropBox.getLowerLeftY());
            if(!rotate && !isLandscape) {
                System.out.println("Not Rotation ............................... " + rotate);
                mediaBox.setUpperRightX((float) (page.getMediaBox().getUpperRightX()));
                mediaBox.setUpperRightY(page.getMediaBox().getUpperRightY() + page.getMediaBox().getHeight() * 0.5f);
                mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY() - page.getMediaBox().getHeight() * 0.5f); 
                cropBox.setUpperRightX((float) (page. getCropBox().getUpperRightX()) + (float) (page. getCropBox().getUpperRightX())*0.5f);
                cropBox.setUpperRightY(page.getCropBox().getUpperRightY());
                cropBox.setLowerLeftX(page.getCropBox().getLowerLeftX() - page.getCropBox().getWidth() * 0.5f);
            }else {

                /***************************************************************************************/
                mediaBox.setUpperRightX((float) (Math.abs(page.getMediaBox().getUpperRightX()) + Math.abs(page.getMediaBox().getWidth()) * 0.1f));
                mediaBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()));
                mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY()); 
                mediaBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()) + Math.abs(page.getMediaBox().getHeight()) * 0.1f);
                mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY() - Math.abs(page.getMediaBox().getHeight()) * 0.1f); 
                cropBox.setUpperRightX((float) (Math.abs(page. getCropBox().getUpperRightX())) + (float) (Math.abs(page. getCropBox().getUpperRightX()))*0.1f);
                cropBox.setUpperRightY(Math.abs(page.getMediaBox().getUpperRightY()));
                cropBox.setLowerLeftY(page.getCropBox().getLowerLeftY());  
                cropBox.setLowerLeftY(page.getCropBox().getLowerLeftY() - Math.abs(page.getCropBox().getHeight() * 0.1f));  
                cropBox.setLowerLeftX(page.getCropBox().getLowerLeftX() - (float) (page. getCropBox().getLowerLeftX())*0.1f);
                System.out.println("Rotation ...............................");

                // note that the page number parameter is zero based
            }
            /****************************************************************************/
            System.out.println("==================================== " + rotate);
            System.out.println((float) (page.getMediaBox().getWidth()) + " :: " + page.getMediaBox().getHeight());
            System.out.println(mediaBox.getWidth() + " :: " + mediaBox.getHeight());
            System.out.println("pageSize.getHeight() :: " + cropBox.getHeight());
            System.out.println("pageSize.getWidth() :: " + cropBox.getWidth());
            System.out.println("pageSize.getUpperRightX() :: " + cropBox.getUpperRightX());
            System.out.println("pageSize.getUpperRightY() :: " + cropBox.getUpperRightY());
            System.out.println("pageSize.getLowerLeftX() :: " + cropBox.getLowerLeftX());
            System.out.println("pageSize.getLowerLeftY() :: " + cropBox.getLowerLeftY());
            System.out.println("===================================="); 
            page.setMediaBox(mediaBox);
            page.setCropBox(cropBox);


        }
        System.out.println("No. of Pages :: " + document.getNumberOfPages());
        document.save(pdfFilename + "_test.pdf");
        System.out.println("Task Completed ... @ " + new Date());
        document.close();


    }
     catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

除少数情况外,它适用于大多数情况。请查找附带此代码失败的示例文件。

PDF SAMPLE FILE 1

PDF SAMPLE FILE 2

1 个答案:

答案 0 :(得分:0)

如果我删除你的if循环,它甚至适用于附加的pdf。看起来你在if循环中所做的事情并不完整。也许你错过了setLowerLeftX。

if(!rotate && !isLandscape) {
                System.out.println("Not Rotation ............................... " + rotate);
                mediaBox.setUpperRightX((float) (page.getMediaBox().getUpperRightX()));
                mediaBox.setUpperRightY(page.getMediaBox().getUpperRightY() + page.getMediaBox().getHeight() * 0.5f);
                mediaBox.setLowerLeftY(page.getMediaBox().getLowerLeftY() - page.getMediaBox().getHeight() * 0.5f); 
                cropBox.setUpperRightX((float) (page. getCropBox().getUpperRightX()) + (float) (page. getCropBox().getUpperRightX())*0.5f);
                cropBox.setUpperRightY(page.getCropBox().getUpperRightY());
                cropBox.setLowerLeftX(page.getCropBox().getLowerLeftX() - page.getCropBox().getWidth() * 0.5f);
            }else