Repository navigation
PDF Libraries
This page documents the third-party PDF libraries used by PdfEditor and how we use them.
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 |
Repository: https://github.com/UglyToad/PdfPig
PdfPig is a PDF reading library that extracts:
- Text content with exact positions
- Individual letters with bounding boxes
- Font information
- Page dimensions
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})");
}| 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 |
- Read-only: Cannot modify PDFs
- No rendering: Cannot display pages
- Font extraction: May miss some embedded fonts
-
PdfTextExtractionService.cs- Extract text for search -
LetterFinder.cs- Match letters to redaction targets -
TextRedactor.cs- Get letter positions for removal
Repository: https://github.com/empira/PDFsharp
PDFsharp is a PDF creation and modification library:
- Create new PDFs
- Modify existing PDFs
- Draw graphics and text
- Manipulate content streams
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);| Class | Purpose |
|---|---|
PdfDocument |
Document container |
PdfPage |
Page access |
PdfContent |
Content stream access |
XGraphics |
Drawing API |
XBrush, XPen
|
Drawing primitives |
- No text extraction: Cannot get accurate letter positions
- Content stream parsing: Basic support, we extend it
- Font handling: Limited compared to PdfPig
-
RedactionService.cs- Document modification -
ContentStreamBuilder.cs- Rebuild content streams - Drawing black rectangles over redacted areas
Repository: https://github.com/sungaila/PDFtoImage
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);- High-quality rendering: PDFium handles complex PDFs well
- Cross-platform: Works on Windows, Linux, macOS
- Fast: Native code performance
| 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 |
- Rendering only: Cannot extract text or modify PDFs
- Memory usage: Large pages at high DPI use significant memory
- Native dependency: Requires PDFium native library
-
PdfRenderService.cs- Display pages in GUI - Thumbnail generation
Repository: https://github.com/mono/SkiaSharp
SkiaSharp is a cross-platform 2D graphics library (wrapper around Google's Skia).
- 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);- Drawing black rectangles over redacted areas
- Image processing for page display
┌─────────────────────────────────────────────────────────────┐
│ 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 │
│ │
└─────────────────────────────────────────────────────────────┘
| 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+ |
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" />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 |
# Install required native libraries
sudo apt-get install libgdiplus- Render pages at lower DPI
- Process pages one at a time
- Dispose documents properly
- PdfPig may not extract all fonts correctly
- Some Type 3 fonts are problematic
- CJK fonts may need special handling
- Redaction-Engine - How redaction uses these libraries
- Redaction-Library-Implementation - Operator-level implementation
- Project-Architecture - Overall architecture