DEV Community

Facundo Ezequiel Diaz Cappella
Facundo Ezequiel Diaz Cappella

Posted on • Edited on

6 2

PDFBox create multi-line PDF dynamically

Hi, I found this library PDFBox to create pdf documents and when I started to use I feel this library less intuitive than the others I used, for that I think this post will be helpful if you are using pdfbox for the first time.

The First step, will be to add the dependency, you can check here the last version:
MVNRepository

Now we will create a java class, to create a pdf from a big string. This class will be adding a new line for every sentence and after 25 lines will be adding a new page. (Of course, I put 25 lines, but you can put whatever you want).

I will add two constants the font size and leading:

    private final static float FONT_SIZE = 12;
    private final static float LEADING = 1.5f * 12;
Enter fullscreen mode Exit fullscreen mode

If you see the value of leading it's using the font size.

Here you can see the main public method of this class, in my case "the big text" always have the new line code \n , but you can break your text using a different condition, this is to create a new line per sentences:

    private void createMultiLinePagePdf(String text) throws IOException {
        PDDocument doc = new PDDocument();

        // break big text using my endof line flag
        String[] listText = text.split("\n");

        // create new page and add to the document, this is the 
        // first page
        PDPage page = new PDPage();
        doc.addPage(page);


        PDPageContentStream contentStream = new 
        PDPageContentStream(doc, page);

        // here we set the leading and font
        contentStream.setFont(getFont(), FONT_SIZE);
        contentStream.setLeading(LEADING);


        contentStream.beginText();
        contentStream.newLineAtOffset(25, 700);
        int countLines = 0;


        // here we iterate the array with all the sentences, and every 25 lines we add a new page
        for (String textLine : listText) {
       // after 25 lines, we create a new page
            if (countLines == 25) { 
                contentStream.endText();
                contentStream.close();
                countLines = 0;
                contentStream = returnNewPageContent(doc);
            }

            writeLine(doc, contentStream, textLine);
            countLines += 1;
        }
        contentStream.endText();
        contentStream.close();


        doc.save("mypdf.pdf");
        doc.close();


    }
Enter fullscreen mode Exit fullscreen mode

This method writes every line:

    private void writeLine(PDDocument doc, PDPageContentStream contentStream, String textLine) throws IOException {
        contentStream.showText(newLineText);
        contentStream.newLine();
    }
Enter fullscreen mode Exit fullscreen mode

This method create a new page:

    private PDPageContentStream returnNewPageContent(PDDocument doc) throws IOException {
        PDPage page = new PDPage();
        doc.addPage(page);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.setFont(getFont(), FONT_SIZE);
        contentStream.setLeading(LEADING);
        contentStream.beginText();
        contentStream.newLineAtOffset(25, 700);


        return contentStream;
    }
Enter fullscreen mode Exit fullscreen mode

And Finally, the get font method:

    private static PDFont getFont() throws IOException {
        return PDType1Font.TIMES_ROMAN;
    }
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay