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

时间:2016-02-11 17:23:06

标签: java pdf pdf-generation pdfbox

我正在尝试根据内容大小动态添加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");
}

2 个答案:

答案 0 :(得分:2)

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

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

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。

答案 1 :(得分:0)

首先,感谢蒂尔曼回答我的实际问题。 与此同时,出于实际目的,我已经改变了我的动态写入PDF的方法,因为代码流过If Else和Loops ....而我选择只使用PrintWriter继续使用Logging to simple text。 在代码的最后,我正在调用一个方法来读取文本日志文件的每一行并放在PDF文档中。总结:一次文本到PDF的转换。

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