jozinho22
jozinho22

Reputation: 592

Why my pdf created with Apache PdfBox doesn't integrate new lines?

I want to create a multiple line pdf document. I'm using PdfBox from Apache.

I had this simple code :

        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);

        // Retrieving the pages of the document
        PDPageContentStream contentStream = new PDPageContentStream(document, page);

        contentStream.beginText();
        contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

        contentStream.showText("blabla");
        contentStream.newLine(); 
        contentStream.showText("blabla");
        contentStream.newLine(); 
        contentStream.showText("blabla");
        contentStream.newLine(); 

And I get only a simple line like this : "blablablablablabla"

Can somebody help me please ?

Thank you

Upvotes: 0

Views: 673

Answers (1)

stack_sod
stack_sod

Reputation: 86

I think you've forgotten to use setLeading? Before using contentStream.newLine() you need to use contentStream.setLeading(float)

(main source here: https://www.javatpoint.com/pdfbox-adding-multiple-lines)

I've edited the relevant part of your code:

contentStream.beginText();
contentStream.newLineAtOffset(20,600); // set starting position
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

contentStream.setLeading(14.5f);  // set the size of the newline to something reasonable

contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();
contentStream.showText("blabla");
contentStream.newLine();

Running this on my machine makes multiple lines, so long as the rest of the code works (closing and opening the file correctly).

Upvotes: 2

Related Questions