2025 Year of Code: November - PDF

:rocket: Xojo FPDF Update - PDF Import & Arabic Text Shaping

What’s New in v1.0.0

Two major features just landed in Xojo FPDF:

:page_facing_up: PDF Import - Import Existing PDFs as Templates

NEW: Example 20 demonstrates full PDF Import functionality

You can now import pages from existing PDF files and use them as XObject templates in your generated PDFs:

// Create new document
Dim pdf As New VNSPDFDocument(VNSPDFModule.ePageOrientation.Portrait, _
                              VNSPDFModule.ePageUnit.Millimeters, _
                              VNSPDFModule.ePageFormat.A4)

// Import pages from existing PDF
Dim sourcePDF As FolderItem = New FolderItem("/path/to/source.pdf", FolderItem.PathModes.Native)
Dim templateID1 As Integer = pdf.ImportPage(sourcePDF, 1)  // Import page 1
Dim templateID2 As Integer = pdf.ImportPage(sourcePDF, 2)  // Import page 2

// Use imported pages as templates
pdf.AddPage()
pdf.UseTemplate(templateID1, 0, 0, 210, 297)  // Place at full A4 size

pdf.AddPage()
pdf.UseTemplate(templateID2, 10, 10, 100, 0)  // Place at 100mm width, auto height

What Works:

  • :white_check_mark: Multi-page PDF parsing - Full page tree traversal
  • :white_check_mark: Content extraction - Stream decompression (FlateDecode, LZWDecode)
  • :white_check_mark: Resource copying - Fonts, images, XObjects automatically copied
  • :white_check_mark: Nested XObjects - Pages referencing other XObjects work correctly
  • :white_check_mark: Template placement - Scale and position imported pages anywhere

Platform Support:

  • :desktop_computer: Desktop: :white_check_mark: Full support
  • :globe_with_meridians: Web: :white_check_mark: Full support with WebDialog file upload
  • :mobile_phone: iOS: :white_check_mark: Works with proper font files
  • :gear: Console: :white_check_mark: Full support

:warning: IMPORTANT: Premium Zlib Module Recommended

Most modern PDFs require the Premium Zlib module for import.

  • Why? Modern PDFs use FlateDecode with PNG Predictors (Predictor 15) for efficient compression
  • Free version: Only supports basic FlateDecode (simple deflate without predictors)
  • Premium module: Includes PNG Predictor reversal algorithms (Predictors 2, 10-15)
  • Without premium: Can parse PDF structure but cannot decompress predictor-encoded streams
  • With premium: Full support for all modern PDF compression formats

Example: /Filter /FlateDecode /DecodeParms << /Predictor 15 /Colors 3 /Columns 1859 >>



:globe_showing_europe_africa: Arabic Text Shaping - Proper Contextual Forms Implementation

NEW: Arabic text now displays with proper joined/cursive letters

Arabic script requires contextual analysis to select the correct glyph form (isolated, initial, medial, final) based on surrounding letters. We’ve implemented full Arabic text shaping:

pdf.AddUTF8Font("Arial", "", "/System/Library/Fonts/Supplemental/Arial Unicode.ttf")
pdf.SetFont("Arial", "", 14)
pdf.Cell(0, 10, "مرحبا بالعالم!")  // Displays correctly with joined letters

What Was Implemented:

  • :white_check_mark: Contextual Form Selection - Automatic detection of isolated, initial, medial, and final letter positions
  • :white_check_mark: Unicode Presentation Forms - Maps base Arabic letters (U+0600-U+06FF) to presentation forms (U+FE70-U+FEFF)
  • :white_check_mark: Proper UTF-8 Handling - Correct multi-byte character processing for shaped text
  • :white_check_mark: RTL Block Reversal - Correct right-to-left word order

Result:
Arabic letters now display in proper joined/cursive style with automatic contextual forms, just like native Arabic typography.

All RTL Scripts Work:

  • :white_check_mark: Arabic - مرحبا بالعالم
  • :white_check_mark: Hebrew - שלום עולם
  • :white_check_mark: Urdu, Persian, Pashto - Any Arabic-script language

Updated Examples

Example 5: UTF-8 & TrueType Fonts

  • Now includes comprehensive Arabic text testing
  • Demonstrates automatic contextual form selection
  • Shows proper right-to-left rendering

Example 20: PDF Import (NEW)

  • Import multi-page PDFs
  • Display thumbnails in 2x2 grid
  • Demonstrates resource dependency copying
  • Shows template scaling and positioning

Platform-Specific UI

Desktop

  • Example 14 (Encryption): Uses SecurityDialog for password/permissions input
  • Example 20 (PDF Import): Uses OpenDialog for file selection

Web

  • Example 14: Uses WebDialogSecurity for encryption settings
  • Example 20: Uses WebDialogPDFUpload with file upload flow
    • Select PDF → Enable OK → Upload → Process

Architecture Highlights

PDF Import Implementation

Complete PDF parsing system in pure Xojo:

  • VNSPDFReader - Multi-page PDF parser with page tree traversal
  • VNSPDFParser - Object parsing and type system
  • VNSPDFTokenizer - Lexical analysis of PDF syntax
  • VNSPDFStreamReader - Binary stream handling
  • VNSPDFStreamDecoder - FlateDecode/LZW decompression
  • VNSPDFLZWDecoder - Pure Xojo LZW implementation (no declares)

Arabic Text Shaping

  • Automatic contextual form selection (isolated, initial, medial, final)
  • Unicode Normalization Form C (NFC) for canonical representation
  • Proper UTF-8 code point tracking in ToUnicode CMap
  • RTL block reversal for correct word order

Quick Example - PDF Import with Arabic Text

// Create document
Dim pdf As New VNSPDFDocument(VNSPDFModule.ePageOrientation.Portrait, _
                              VNSPDFModule.ePageUnit.Millimeters, _
                              VNSPDFModule.ePageFormat.A4)

// Import existing PDF as template
Dim sourcePDF As FolderItem = New FolderItem("/path/to/invoice.pdf", FolderItem.PathModes.Native)
Dim templateID As Integer = pdf.ImportPage(sourcePDF, 1)

// Add page with imported background
pdf.AddPage()
pdf.UseTemplate(templateID, 0, 0, 210, 297)  // Full page background

// Add Arabic text overlay
pdf.AddUTF8Font("Arial", "", "/System/Library/Fonts/Supplemental/Arial Unicode.ttf")
pdf.SetFont("Arial", "", 14)
pdf.SetTextColor(0, 0, 255)
pdf.Text(20, 20, "مرحبا بالعالم!")  // Arabic text overlaid on imported PDF

// Save
If pdf.Ok() Then
  Dim f As FolderItem = SpecialFolder.Desktop.Child("output.pdf")
  Call pdf.SaveToFile(f)
End If

Current Status

Xojo FPDF v1.0.0 is out with:

  • :white_check_mark: 20 working examples - All features demonstrated
  • :white_check_mark: Cross-platform - Desktop, Web, iOS, Console
  • :white_check_mark: 100% Open Source - MIT license, full source code
  • :white_check_mark: Full Unicode - TrueType fonts with automatic font subsetting
  • :white_check_mark: PDF Import - Import existing PDFs as templates
  • :white_check_mark: Arabic/RTL Support - Automatic contextual forms and right-to-left rendering

Premium Modules Ready:

  • :locked_with_key: Encryption Premium - AES-128/256 (RC4-40 in free version)
  • :bar_chart: Table Premium - Professional tables with pagination
  • :clamp: Compression Premium - Pure Xojo zlib (iOS support + PNG Predictor reversal for PDF Import)

Try it today! All source code and examples available at [VNS Link]

5 Likes