【问题标题】:PDF Page Scaling With Apache PDFBox使用 Apache PDFBox 进行 PDF 页面缩放
【发布时间】:2018-10-31 19:48:19
【问题描述】:

我正在尝试调整 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

【问题讨论】:

    标签: java apache pdf pdf-generation pdfbox


    【解决方案1】:

    如果我删除你的 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2013-07-15
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2016-06-07
      相关资源
      最近更新 更多