B4J Question [solved] How to creating pdf file with two or more pages with PDFBox


Active Member
Licensed User
Longtime User
From this post of spsp about creating and viewing pdf files, i have two question:

Is it possible to create a new pdf document starting from a template, that is, an empty pdf document and fill in the information? the empty document can be an invoice or similar.

First question, how to create a new pdf document from the template.

Second question, how to add pages to the document when the invoice is very long.



Active Member
Licensed User
Longtime User
It works in the following way:

Root document:
Private box As PDFBox
box.Initialize("", File.Combine(File.DirData(appName), sourcepdf))
Dim doc As PDDocument = box.Document

Content stream first page:
Dim p1 As PDPage = doc.GetPage(nPage)
Dim cs As PDPageContentStream ' Create a PageContentStream
cs.Initialize("", doc, p1, "APPEND", False, False)

Some writing ops:
SetPDFText(cs, hlp.COURIER, 10, 0, 0, 0, 2, True, mm, 265, pdaValId, 4.71)

In a loop:
cs.close ' end of current page
Dim px As PDPage = doc.GetPage(nPage) ' new page
Dim cs As PDPageContentStream ' restart content stream
cs.Initialize("", doc, px, "APPEND", False, False) ' add the page

SetPDFText ...
SetPDFText ...

Finally, close the current stream and save the document.
The original pdf document (template) must have several pages to take one at a time for the cs. The unneeded pages can be removed from original template doc.removePage(index)

Upvote 0