Create a multi-page PDF using PDFBox:
(a) Create a new page, a new content stream, Go to the beginning on the left, start writing. When writing each word, check if a space gap is required for the width of the media box. If crosses, go to the next line on the left and start writing. Continue writing to the last line of the page.
(b) Close the contentStream and add the current page to the document when the write operation reaches the last line of the current page,
(c) Repeat steps (a) and (b) until the last record / line / line is recorded.
PDDocument document = new PDDocument(); PDFont font = PDType1Font.HELVETICA; //For Each Page: PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.setFont(font, 12); contentStream.beginText(); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString("PDF BOX TEXT CONTENT"); contentStream.endText(); contentStream.close(); document.addPage(page); //After All Content is written: document.save(pdfFile); document.close();
Hint: Use font options such as size / height and remaining height of the media window to determine the last line of the page.
AVA
source share