1

I'm using the following method to create a PDF file:

private void createPdf() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(new PDPage());

    PDPageContentStream content = new PDPageContentStream(doc, page);

    content.beginText();
    content.setFont(PDType1Font.HELVETICA, 26);
    content.showText("Example Text");
    content.endText();

    content.close();

    doc.save("report.pdf");
    doc.close();
}

It creates a new file with a white page, but no text is shown. What's wrong?

I use Apache PDFBox 2.0.7.

3
  • 2
    Have you looked at the bottom of the page? Commented Aug 5, 2017 at 13:57
  • 2
    Vega, what @Tilman hints at is that you did not indicate any position for the text. Thus, it is drawn at (0,0) which in case of your code is in the lower left corner of the page. Commented Aug 5, 2017 at 14:14
  • I double-checked, but there is nothing on the page. Thanks for the hint. Any other ideas? Commented Aug 5, 2017 at 23:46

1 Answer 1

3

Change this code

PDPage page = new PDPage();
doc.addPage(new PDPage());

to this

PDPage page = new PDPage();
doc.addPage(page);

You made the mistake to add a new page with nothing. The operations you did were done on another object.

Your text should now be visible at the bottom of the page. (y = 0 is bottom in PDF)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.