Installation and your first PDF
Requirements
- B4J 10.70 and Java 19 are the target environment for this version.
- Install
B4JPdf.b4xlibin the B4J Additional Libraries folder. - The
manifest.txtdeclares:JavaObject,CSSUtils,B4XCollections,ByteConverter,jFX,jRandomAccessFile,jReflection,XUI Views,pdfbox-app-3.0.8.jar,bcpkix-jdk18on-1.85.jar,bcutil-jdk18on-1.85.jarandbcprov-jdk18on-1.85.jar. - Those third-party JARs are not automatically bundled in the library. Install as appropriate and observe their licenses.
Example: title, paragraph and table
Call this procedure from your B4J application. The layout unit here is mm.
Sub CreateReportPDF(OutputDir As String, OutputFile As String)
Dim pdf As PDFManager
pdf.Initialize(Me, "PDF", "mm")
pdf.SetMargins(20, 20, 25, 25)
Dim heading As PDFParagraph = pdf.AddParagraph("Sales report")
heading.SetFontSize(20).SetBold(True).SetAlignment("C")
pdf.AddParagraph("Monthly report generated with B4J.")
pdf.AddLine2(1, 0xFF64748B)
Dim t As PDFTable = pdf.AddTable
t.SetColWidths(Array As Double(60, 60))
Dim header As PDFTableRow = t.AddRow
t.AddCell(header, "Product").SetBold(True)
t.AddCell(header, "Quantity").SetBold(True)
Dim row As PDFTableRow = t.AddRow
t.AddCell(row, "Coffee")
t.AddCell(row, "25")
pdf.Save(OutputDir, OutputFile)
End Sub
Sub PDF_SaveCompleted(Success As Boolean)
Log("PDF saved: " & Success)
End Sub
Use case: invoices, internal reports, customer certificates and data-driven documents.
PDFManager.Save(Directory, FileName) does not return a Boolean. Inspect the SaveCompleted(Success As Boolean) event rather than assuming success.
Headers, footers and images
Dim pdf As PDFManager
pdf.Initialize(Me, "PDF", "mm")
pdf.getHeader.SetHeight(15)
pdf.getHeader.AddParagraph("My company")
pdf.getFooter.AddParagraph("Generated document")
pdf.AddImage(File.DirAssets, "logo.png", 35, 18)
pdf.AddParagraph("Report body")
pdf.AddPageBreak
pdf.AddParagraph("Next page")
pdf.Save(File.DirApp, "report.pdf")
Put logo.png in the app's Files resources before using File.DirAssets.
PDFManager builds documents, PDFParagraph styles text, PDFTable builds rows and cells. For modifying or signing existing PDFs use PDFBoxTools.
Next: PDF operations and forms.