Xojo FPDF Update - PDF Import & Arabic Text Shaping
What’s New in v1.0.0
Two major features just landed in Xojo FPDF:
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:
Multi-page PDF parsing - Full page tree traversal
Content extraction - Stream decompression (FlateDecode, LZWDecode)
Resource copying - Fonts, images, XObjects automatically copied
Nested XObjects - Pages referencing other XObjects work correctly
Template placement - Scale and position imported pages anywhere
Platform Support:
Desktop:
Full support
Web:
Full support with WebDialog file upload
iOS:
Works with proper font files
Console:
Full support
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 >>
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:
Contextual Form Selection - Automatic detection of isolated, initial, medial, and final letter positions
Unicode Presentation Forms - Maps base Arabic letters (U+0600-U+06FF) to presentation forms (U+FE70-U+FEFF)
Proper UTF-8 Handling - Correct multi-byte character processing for shaped text
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:
Arabic - مرحبا بالعالم
Hebrew - שלום עולם
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:
20 working examples - All features demonstrated
Cross-platform - Desktop, Web, iOS, Console
100% Open Source - MIT license, full source code
Full Unicode - TrueType fonts with automatic font subsetting
PDF Import - Import existing PDFs as templates
Arabic/RTL Support - Automatic contextual forms and right-to-left rendering
Premium Modules Ready:
Encryption Premium - AES-128/256 (RC4-40 in free version)
Table Premium - Professional tables with pagination
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]