Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApachePDFBox进行PDF页面缩放_Java_Apache_Pdf_Pdf Generation_Pdfbox - Fatal编程技术网

Java 使用ApachePDFBox进行PDF页面缩放

Java 使用ApachePDFBox进行PDF页面缩放,java,apache,pdf,pdf-generation,pdfbox,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_withheaderfoo

我正在尝试调整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();
    }
    }
除少数情况外,它适用于大多数情况。请查找此代码失败的示例文件附件


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