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

roerGarcia

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.

TIA
 

roerGarcia

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:
Multi-pages:
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 ...
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

Schakalaka

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:
Multi-pages:
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 ...
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)

=)
hello,
have you find a solution for your problem?
i have a similar need: I have fix text and some form fields and i need to generate 2 pages in dinamic way.
this is my project:


 

Attachments

  • pdfgen.zip
    227.2 KB · Views: 37
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
have you find a solution for your problem?
You have quoted the solution.

Anyway: Posting to an existing thread is a mistake. You should ALWAYS create a NEW THREAD for your Issues.
 
Upvote 0
Top