【问题标题】:Unable to add Pages to PDF using PDFBox using separate method from main()无法使用与 main() 分开的方法使用 PDFBox 将页面添加到 PDF
【发布时间】:2016-02-11 17:23:06
【问题描述】:

我正在尝试根据内容大小动态添加 PDF 页面。 为此,我不想弄乱主方法,而是创建了一个单独的方法来编写 PDF 并从 main() 调用该方法,如下所示:

//PDF Log Method
PDlog("Create First Page", "b");
PDlog("add more page", "b");
PDlog("close pdf", "b");

我在每个 if 条件的末尾添加了 system.out.println,并且所有三个都打印在 IDE 屏幕上。但是在 3 个方法调用结束后正在生成一个空 PDF。当我只有一个 if 条件并且只调用一次 PDlog 方法时,PDF 正在被正确保存和关闭。

如何从 main 中多次调用此方法并继续多次添加页面和内容?

下面是方法代码:

public static void PDlog(String action, String msg) throws IOException, ClassNotFoundException, SQLException, InterruptedException, COSVisitorException {
    //Master PDF Log File --------------------------------------------------------------------
    String masterPDLog = "X:\\eHub\\QA\\eHub_Automation_Log.pdf";
    // Create a document and add a page to it
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);

    if (action.equals("Create First Page")) {       
            document.addPage(page);
            // Create a new font object selecting one of the PDF base fonts
            PDFont font = PDType1Font.TIMES_ROMAN;
            PDFont boldFont = PDType1Font.TIMES_BOLD;
            //File for CTS Logo --------------------
            InputStream in = new FileInputStream(new File("X:\\eHub\\QA\\img\\cts.jpg"));
            PDJpeg img = new PDJpeg(document, in);

            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            //Place CTS Logo
            //contentStream.drawImage(img, 500, 750);
            contentStream.drawXObject( img, 450, 700, 50, 50 );

            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
            contentStream.beginText();
            contentStream.setFont( boldFont, 20 );
            contentStream.setNonStrokingColor(Color.BLUE);
            contentStream.moveTextPositionByAmount( 120, 650 );
            contentStream.drawString("eHub Automated Data Quality Report");
            contentStream.endText();

            contentStream.beginText();
            contentStream.setFont( boldFont, 20 );
            contentStream.setNonStrokingColor(Color.BLUE);
            contentStream.moveTextPositionByAmount( 140, 600 );
            contentStream.drawString("Data Profiling/Quality/Analysis");
            contentStream.endText();
            // Make sure that the content stream is closed:
            contentStream.close();
            //document.save(masterPDLog);

            System.out.println("1ST PAGE ADDED");

    }
    else if (action.equals("add more page")) {
            PDFont font = PDType1Font.TIMES_ROMAN;
            document.addPage(page);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont( font, 20 );
            contentStream.setNonStrokingColor(Color.BLACK);
            contentStream.moveTextPositionByAmount( 100, 800 );
            contentStream.drawString("eHub Automated Data Quality Report");
            contentStream.endText();
            contentStream.close();              

            //document.save(masterPDLog);
            System.out.println("2ND PAGE ADDED");
    }
    else if (action.equals("close pdf")) {
        PDFont font = PDType1Font.TIMES_ROMAN;

        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.beginText();
        contentStream.setFont( font, 20 );
        contentStream.setNonStrokingColor(Color.BLACK);
        contentStream.moveTextPositionByAmount( 100, 800 );
        contentStream.drawString("eHub Automated Data Quality Report");
        contentStream.endText();
        contentStream.close();


        document.save(masterPDLog);
        document.close();
        System.out.println("PDF CLOSED");
}

【问题讨论】:

    标签: java pdf pdf-generation pdfbox


    【解决方案1】:

    您每次都创建文档,这就是原因。

    只需将文档对象传递给您的方法,然后首先创建文档 - 这是您的代码,已更正:

    public static void main(String[] args) throws IOException, COSVisitorException
    {
        PDDocument document = new PDDocument();
        pdlog("Create First Page", "b", document);
        pdlog("add more page", "b", document);
        pdlog("close pdf", "b", document);
    }
    
    public static void pdlog(String action, String msg, PDDocument document) throws IOException, COSVisitorException
    {
        //Master PDF Log File --------------------------------------------------------------------
        String masterPDLog = "X:\\eHub\\QA\\eHub_Automation_Log.pdf";
        // Create a document and add a page to it
        PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
    
        if (action.equals("Create First Page"))
        {
            document.addPage(page);
            // Create a new font object selecting one of the PDF base fonts
            PDFont font = PDType1Font.TIMES_ROMAN;
            PDFont boldFont = PDType1Font.TIMES_BOLD;
            //File for CTS Logo --------------------
            InputStream in = new FileInputStream(new File("X:\\eHub\\QA\\img\\cts.jpg"));
            PDJpeg img = new PDJpeg(document, in);
    
            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
    
                //Place CTS Logo
            //contentStream.drawImage(img, 500, 750);
            contentStream.drawXObject(img, 450, 700, 50, 50);
    
            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
            contentStream.beginText();
            contentStream.setFont(boldFont, 20);
            contentStream.setNonStrokingColor(Color.BLUE);
            contentStream.moveTextPositionByAmount(120, 650);
            contentStream.drawString("eHub Automated Data Quality Report");
            contentStream.endText();
    
            contentStream.beginText();
            contentStream.setFont(boldFont, 20);
            contentStream.setNonStrokingColor(Color.BLUE);
            contentStream.moveTextPositionByAmount(140, 600);
            contentStream.drawString("Data Profiling/Quality/Analysis");
            contentStream.endText();
            // Make sure that the content stream is closed:
            contentStream.close();
            //document.save(masterPDLog);
    
            System.out.println("1ST PAGE ADDED");
    
        }
        else if (action.equals("add more page"))
        {
            PDFont font = PDType1Font.TIMES_ROMAN;
            document.addPage(page);
    
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont(font, 20);
            contentStream.setNonStrokingColor(Color.BLACK);
            contentStream.moveTextPositionByAmount(100, 800);
            contentStream.drawString("eHub Automated Data Quality Report");
            contentStream.endText();
            contentStream.close();
    
            //document.save(masterPDLog);
            System.out.println("2ND PAGE ADDED");
        }
        else if (action.equals("close pdf"))
        {
            PDFont font = PDType1Font.TIMES_ROMAN;
    
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont(font, 20);
            contentStream.setNonStrokingColor(Color.BLACK);
            contentStream.moveTextPositionByAmount(100, 800);
            contentStream.drawString("eHub Automated Data Quality Report");
            contentStream.endText();
            contentStream.close();
    
            document.save(masterPDLog);
            document.close();
            System.out.println("PDF CLOSED");
        }
    }
    

    您的“关闭 pdf”操作没有多大意义,您正在写入一个从未附加的 PDPage。

    【讨论】:

      【解决方案2】:

      首先,感谢蒂尔曼回答我的实际问题。 同时,出于实际目的,我改变了动态写入 PDF 的方法,因为代码流经 If Else 和 Loops...。相反,我选择简单地继续使用 PrintWriter 记录到简单的文本文件。 在代码的最后,我正在调用一个方法来读取文本日志文件的每一行并将其放入 PDF 文档中。总结:一次性文本到 PDF 的转换。

      我探索了 iText 并选择了它而不是 Apache PDFBox,它可能比 PDFBox 快 2-3 倍,并且添加页面是隐式和自动的。

      【讨论】:

        猜你喜欢
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-29
        • 2013-07-15
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多