Skip to content

PDF Libraries

Marc Jones edited this page Dec 30, 2025 · 1 revision

PDF Libraries

This page documents the third-party PDF libraries used by PdfEditor and how we use them.

Overview

PdfEditor uses multiple PDF libraries, each with different strengths:

Library Purpose License Why We Use It
PdfPig Text extraction Apache 2.0 Accurate letter positions
PDFsharp PDF manipulation MIT Content stream modification
PDFtoImage PDF rendering MIT PDFium-based page display
SkiaSharp 2D graphics MIT Drawing on rendered pages

PdfPig

Repository: https://github.com/UglyToad/PdfPig

What It Does

PdfPig is a PDF reading library that extracts:

  • Text content with exact positions
  • Individual letters with bounding boxes
  • Font information
  • Page dimensions

Why We Use It

PdfPig provides accurate letter-level positioning that PDFsharp lacks. This is critical for glyph-level redaction.

using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;

// Extract letters with positions
using var document = PdfDocument.Open(pdfPath);
var page = document.GetPage(1);

foreach (Letter letter in page.Letters)
{
    Console.WriteLine($"'{letter.Value}' at ({letter.GlyphRectangle.Left}, {letter.GlyphRectangle.Bottom})");
}

Key Classes We Use

Class Purpose
PdfDocument Open and read PDFs
Page Access page content
Letter Individual character with position
Word Word grouping
GlyphRectangle Bounding box for a letter

Limitations

  • Read-only: Cannot modify PDFs
  • No rendering: Cannot display pages
  • Font extraction: May miss some embedded fonts

Our Usage

  • PdfTextExtractionService.cs - Extract text for search
  • LetterFinder.cs - Match letters to redaction targets
  • TextRedactor.cs - Get letter positions for removal

PDFsharp

Repository: https://github.com/empira/PDFsharp

What It Does

PDFsharp is a PDF creation and modification library:

  • Create new PDFs
  • Modify existing PDFs
  • Draw graphics and text
  • Manipulate content streams

Why We Use It

PDFsharp allows us to modify content streams - essential for TRUE glyph removal.

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Content;

// Open and modify PDF
var document = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Modify);
var page = document.Pages[0];

// Access content stream
var content = page.Contents;
byte[] streamBytes = content.CreateSingleContent().GetBytes();

// Modify and replace
byte[] newBytes = ModifyContentStream(streamBytes);
content.ReplaceContent(newBytes);

document.Save(outputPath);

Key Classes We Use

Class Purpose
PdfDocument Document container
PdfPage Page access
PdfContent Content stream access
XGraphics Drawing API
XBrush, XPen Drawing primitives

Limitations

  • No text extraction: Cannot get accurate letter positions
  • Content stream parsing: Basic support, we extend it
  • Font handling: Limited compared to PdfPig

Our Usage

  • RedactionService.cs - Document modification
  • ContentStreamBuilder.cs - Rebuild content streams
  • Drawing black rectangles over redacted areas

PDFtoImage

Repository: https://github.com/sungaila/PDFtoImage

What It Does

PDFtoImage renders PDF pages to images using the PDFium engine (same as Chrome).

using PDFtoImage;

// Render page to SKBitmap
var bitmap = Conversion.ToImage(pdfPath, page: 0, dpi: 150);

Why We Use It

  • High-quality rendering: PDFium handles complex PDFs well
  • Cross-platform: Works on Windows, Linux, macOS
  • Fast: Native code performance

Key Methods

Method Purpose
ToImage(path, page) Render page to bitmap
ToImages(path) Render all pages
GetPageCount(path) Get total pages
GetPageSize(path, page) Get page dimensions

Limitations

  • Rendering only: Cannot extract text or modify PDFs
  • Memory usage: Large pages at high DPI use significant memory
  • Native dependency: Requires PDFium native library

Our Usage

  • PdfRenderService.cs - Display pages in GUI
  • Thumbnail generation

SkiaSharp

Repository: https://github.com/mono/SkiaSharp

What It Does

SkiaSharp is a cross-platform 2D graphics library (wrapper around Google's Skia).

Why We Use It

  • Draw redaction rectangles on rendered pages
  • Image manipulation
  • Cross-platform graphics
using SkiaSharp;

// Draw black rectangle
using var canvas = new SKCanvas(bitmap);
using var paint = new SKPaint { Color = SKColors.Black };
canvas.DrawRect(x, y, width, height, paint);

Our Usage

  • Drawing black rectangles over redacted areas
  • Image processing for page display

How They Work Together

┌─────────────────────────────────────────────────────────────┐
│                     REDACTION WORKFLOW                       │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  1. Open PDF                                                 │
│     ├── PdfPig: Extract letters with positions               │
│     └── PDFtoImage: Render for display                       │
│                                                              │
│  2. User selects area                                        │
│     └── Find letters in area using PdfPig data               │
│                                                              │
│  3. Remove glyphs                                            │
│     ├── PDFsharp: Open document for modification             │
│     ├── Our parser: Parse content stream                     │
│     ├── Filter: Remove operations for target letters         │
│     └── PDFsharp: Write modified content stream              │
│                                                              │
│  4. Draw black box                                           │
│     ├── PDFsharp: Add rectangle to page                      │
│     └── SkiaSharp: Update display                            │
│                                                              │
│  5. Save                                                     │
│     └── PDFsharp: Write modified document                    │
│                                                              │
└─────────────────────────────────────────────────────────────┘

Version Requirements

Library Version .NET
PdfPig 0.1.11+ .NET Standard 2.0+
PDFsharp 6.2.2+ .NET 6.0+
PDFtoImage 4.0.2+ .NET 6.0+
SkiaSharp 2.88.8+ .NET Standard 1.3+

Installation

All libraries are available via NuGet:

<PackageReference Include="PdfPig" Version="0.1.11" />
<PackageReference Include="PDFsharp" Version="6.2.2" />
<PackageReference Include="PDFtoImage" Version="4.0.2" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />

License Compatibility

All libraries use permissive licenses compatible with commercial use:

Library License
PdfPig Apache 2.0
PDFsharp MIT
PDFtoImage MIT
SkiaSharp MIT
PDFium (via PDFtoImage) BSD-3

Troubleshooting

Linux: PDF rendering fails

# Install required native libraries
sudo apt-get install libgdiplus

Memory issues with large PDFs

  • Render pages at lower DPI
  • Process pages one at a time
  • Dispose documents properly

Font issues

  • PdfPig may not extract all fonts correctly
  • Some Type 3 fonts are problematic
  • CJK fonts may need special handling

See Also

Clone this wiki locally