PDFBox: Add multiple lines of text to a page


/ Published in: Java
Save to your folder(s)

Creates a new document with a single page, and adds two lines of text to it.


Copy this code and paste it in your HTML
  1. /**
  2.  * @param args the command line arguments
  3.  * @throws java.io.IOException
  4.  */
  5. public static void main(String[] args) throws IOException {
  6. PDDocument document = new PDDocument();
  7. PDPage page = new PDPage(PDRectangle.A4);
  8.  
  9. document.addPage(page);
  10.  
  11. PDPageContentStream contentStream = new PDPageContentStream(document, page);
  12.  
  13. contentStream.beginText();
  14.  
  15. contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
  16. contentStream.setLeading(15f);
  17. contentStream.newLineAtOffset(25, 500);
  18.  
  19. contentStream.showText("First Line");
  20. contentStream.newLine();
  21. contentStream.showText("Second Line");
  22.  
  23. contentStream.endText();
  24.  
  25. contentStream.close();
  26.  
  27. document.save("");
  28. document.close();
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.