sumon c
sumon c

Reputation: 789

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

I am trying to dynamically add PDF pages depending upon content size. for that I did not want to clutter the main method, instead I created a separate method to write the PDF and call the method from main() like below:

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

I have added system.out.println at the end of each if condition, and all three are getting printed on IDE screen. but an Empty PDF is being generated after 3 method calls end. When I had only one if condition and called the PDlog method only once, the PDF is being saved and closed properly.

How can I call this method multiple times from main and keep on adding page and content multiple times?

Below is the method code:

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

Upvotes: 0

Views: 607

Answers (2)

sumon c
sumon c

Reputation: 789

1st of all, thanks Tilman for answering my actual question. In the meantime, for practical purposes, I have changed my approach to writing dynamically to a PDF, as the code flows through If Else and Loops.... instead I have chosen to simply keep on Logging to simple text file using PrintWriter. At the final end of the code, I am calling a method to Read each line of the Text Log file and place in a PDF document. To Summarize: One time Text to PDF conversion.

I explored iText and chose it over Apache PDFBox, it is perhaps 2-3 times faster than PDFBox and adding page is implicit and automatic.

Upvotes: 0

Tilman Hausherr
Tilman Hausherr

Reputation: 18936

You create the document each time, that is why.

Just pass the document object to your method, and create the document first - here's your code, corrected:

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

Your "close pdf" action does not make much sense, you are writing to a PDPage that is never appended.

Upvotes: 2

Related Questions