无法使用 PDFBox 使用与 main() 不同的方法将页面添加到 PDF

Unable to add Pages to PDF using PDFBox using separate method from main()

我正在尝试根据内容大小动态添加 PDF 页面。 为此,我不想弄乱 main 方法,而是创建了一个单独的方法来编写 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");
}

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

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

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");
    }
}

您的 "close pdf" 操作没有多大意义,您正在写入一个永远不会附加的 PDPage。

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

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