Technical Support
🟢 Online now
Hello 👋

How can we help you today?
Contact on WhatsApp
B4X Store
← All libraries
PDF B4J · Java 19 · v1.0

B4JPdf Pro

Guides for PDF generation, transformations, viewing, forms, P12/PFX digital signatures and multilingual fonts in B4J.

Templates & tables
v1.0 documentation

Templates, tables and visual callouts — practical B4JPdf tutorial

This extends the existing B4JPdf documentation. It is based on B4JPdf(6).b4xlib public methods and the attached example_pdf/B4JPdfExample.b4j project. Code follows the source example's patterns, but is not claimed to have been freshly compiled.

What can you build?

Use PDFManager, PDFTable, PDFParagraph, PDFTemplateManager and PDFChartHelper to create invoices, quotations, receipts, sales and inventory reports, technical reports, meeting minutes, delivery notes, certificates and shipping documents. The sample project contains 12 example templates, not 12 mandatory document standards.

Example template What to customize
Commercial invoice, pro forma, quotation Parties, item columns, amounts and tax rules
Payment receipt and credit note Amount, reason, supporting image
Sales and inventory Metrics, items, status colors and charts
Technical report and meeting minutes Findings, actions and owners
Delivery and international shipping Address, packages, QR and CJK font
Participation certificate Recipient, duration, typography and decorative layout

All sample transaction data is fictional. Adapt legal and tax requirements to your jurisdiction.

1. Create a custom template from scratch

' Declare in Class_Globals or Process_Globals
Private TemplateTheme As PDFTemplateManager

Sub CreateQuote
    Dim pdf As PDFManager
    pdf.Initialize(Me, "MyTemplate", "mm")
    pdf.SetMargins(18, 18, 34, 22)

    TemplateTheme.Initialize(pdf)
    TemplateTheme.SetThemeColor(0xFF2563EB)
    pdf.getHeader.AddParagraph("MY COMPANY").SetBold(True).SetFontSize(10).ApplyStyle
    pdf.getFooter.AddParagraph("B4JPdf demonstration").SetFontSize(8).ApplyStyle

    pdf.AddParagraph("QUOTATION").SetBold(True).SetFontSize(21).SetTextColor(0xFF2563EB).ApplyStyle
    pdf.AddParagraph("Customer: Example Company").SetFontSize(11).ApplyStyle
    pdf.AddLine2(0.6, 0xFF2563EB)

    Dim table As PDFTable = pdf.AddTable
    table.SetHeaderRowsCount(1)
    table.SetColWidths(Array As Double(80, 35, 45))
    table.SetBorderColor(0xFFE2E8F0)
    Dim row As PDFTableRow = table.AddRow
    table.AddCell2(row, "Service", 0xFF2563EB).SetBold(True).SetTextColor(0xFFFFFFFF).ApplyStyle
    table.AddCell2(row, "Qty", 0xFF2563EB).SetBold(True).SetTextColor(0xFFFFFFFF).ApplyStyle
    table.AddCell2(row, "Amount", 0xFF2563EB).SetBold(True).SetTextColor(0xFFFFFFFF).ApplyStyle

    row = table.AddRow
    table.AddCell(row, "Consulting").ApplyStyle
    table.AddCell(row, "2").ApplyStyle
    table.AddCell(row, "$ 200.00").ApplyStyle
    pdf.AddParagraph("TOTAL: $ 200.00").SetBold(True).SetFontSize(14).ApplyStyle
    pdf.Save(File.DirApp, "quotation.pdf")
End Sub

Private Sub MyTemplate_PageCreated
    TemplateTheme.DrawRectHeader(27)
    TemplateTheme.DrawRectFooter(17)
End Sub

Private Sub MyTemplate_SaveCompleted(Success As Boolean)
    Log("Quotation saved: " & Success)
End Sub

PDFManager handles the document, PDFTemplateManager draws decorative page elements and PDFTable handles rows and columns. Draw the theme's header/footer in PageCreated because pages are generated during Save. Alternatives include DrawWaveHeader/Footer, DrawDiagonalHeader/Footer and DrawSweepHeader/Footer.

2. Tables, headings, shading and merged columns

Requirement Public API
Create table and row pdf.AddTable, table.AddRow
Text cell table.AddCell(row, text)
Colored cell table.AddCell2(row, text, backgroundColor)
Span multiple columns table.AddCellSpan(row, text, backgroundColor, ColSpan)
Column widths and borders SetColWidths, SetBorderColor, SetBorderWidth
Header rows SetHeaderRowsCount(Count)

Three-column table with a merged heading

Sub CreateMergedTable
    Dim pdf As PDFManager
pdf.Initialize(Me, "TableDemo", "mm")
pdf.SetMargins(18, 18, 24, 22)
Dim t As PDFTable = pdf.AddTable
t.SetColWidths(Array As Double(60, 50, 50))
t.SetHeaderRowsCount(2)
t.SetBorderColor(0xFFD1D5DB)

Dim r As PDFTableRow = t.AddRow
t.AddCellSpan(r, "INVENTORY SUMMARY", 0xFF1D4ED8, 3).SetBold(True).SetTextColor(0xFFFFFFFF).ApplyStyle
r = t.AddRow
t.AddCell2(r, "Item", 0xFFDBEAFE).SetBold(True).ApplyStyle
t.AddCell2(r, "Quantity", 0xFFDBEAFE).SetBold(True).ApplyStyle
t.AddCell2(r, "Status", 0xFFDBEAFE).SetBold(True).ApplyStyle
r = t.AddRow
t.AddCell(r, "A4 paper").ApplyStyle
t.AddCell(r, "420").ApplyStyle
t.AddCell2(r, "Available", 0xFFDCFCE7).ApplyStyle
r = t.AddRow
t.AddCellSpan(r, "References: 1", 0xFFF1F5F9, 2).SetBold(True).ApplyStyle
t.AddCell(r, "OK").ApplyStyle

    pdf.Save(File.DirApp, "merged-table.pdf")
End Sub

Private Sub TableDemo_SaveCompleted(Success As Boolean)
    Log("Saved: " & Success)
End Sub

ColSpan is the number of columns occupied, not a physical width. If a row has three configured columns, a two-column cell leaves space for one normal cell.

Do not confuse horizontal and vertical merging. The audited PDFTable public API exposes AddCellSpan for columns, but no public RowSpan or MergeRows for vertical row merging. Build complex vertical arrangements with separate tables or positioned graphics using PDFTemplateManager.DrawCard.

3. Colored alert boxes with text, image or icon

Sub CreateNotices
    Dim pdf As PDFManager
pdf.Initialize(Me, "Notices", "mm")
pdf.SetMargins(18, 18, 24, 22)
pdf.AddAlertBox(0xFF2563EB, True).AddText("INFO: Check the entered details.").SetBold(True).ApplyStyle
pdf.AddAlertBoxWithImage(0xFFD97706, True, File.DirAssets, "warning.png").AddText("WARNING: Demonstration document.").SetFontSize(10).ApplyStyle
pdf.AddAlertBoxWithIcon(0xFF059669, True, "i", "Helvetica", 0xFFFFFFFF).AddText("Completed.").SetBold(True).ApplyStyle
    pdf.Save(File.DirApp, "notices.pdf")
End Sub

Private Sub Notices_SaveCompleted(Success As Boolean)
    Log("Saved: " & Success)
End Sub

The image must exist in the B4J Files tab when using File.DirAssets. The icon requires a font that actually contains the glyph.

Can an alert contain a chart?

AddAlertBoxWithImage accepts directory + filename, not a chart object. First create the chart as a bitmap using PDFChartHelper, write it with SaveTempBitmap, then pass its temporary image path to the alert. This is an image-based composition, not a native live chart embedded into the alert.

Dim charts As PDFChartHelper
charts.Initialize
Dim labels As List = Array As String("Jan", "Feb", "Mar")
Dim values As List = Array As Object(12, 18, 23)
Dim chartImage As String = charts.SaveTempBitmap(charts.CreateBarChart("Sales", labels, values, "k"), "sales-alert")
If chartImage <> "" Then
    pdf.AddAlertBoxWithImage(0xFF0F766E, True, File.DirTemp, chartImage).AddText("Sales summary").SetBold(True).ApplyStyle
End If

For large charts, pdf.AddImage(File.DirTemp, chartImage, 155, 92) is usually clearer. Clean up temporary charts after the PDF save completes.

4. Reuse and adapt the 12 bundled examples

The sample project groups templates in CreateTemplate(Kind As Int), with helpers AddTemplatePartyDetails, AddTemplateTable, Template_PageCreated and Template_SaveCompleted. Start by changing the accent color, header/footer, party data, table widths and filenames. Then replace sample values with your application's data. Check the output and SaveCompleted, including multi-page layouts and non-Latin fonts.

Verified signing scope

The supplied examples use file-based PKCS#12 certificates (.p12/.pfx). Hardware tokens, USB signing devices, smartcards and PKCS#11 workflows have not been tested and must not be advertised as verified. Cryptographic signature validity and issuer/legal trust are separate questions.

Continue: First PDF · PDF tools · P12/PFX signing · Public API.