PdfPig 0.1.9-alpha-20231119-4537e

This is a prerelease version of PdfPig.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package PdfPig --version 0.1.9-alpha-20231119-4537e
NuGet\Install-Package PdfPig -Version 0.1.9-alpha-20231119-4537e
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20231119-4537e" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PdfPig --version 0.1.9-alpha-20231119-4537e
#r "nuget: PdfPig, 0.1.9-alpha-20231119-4537e"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PdfPig as a Cake Addin
#addin nuget:?package=PdfPig&version=0.1.9-alpha-20231119-4537e&prerelease

// Install PdfPig as a Cake Tool
#tool nuget:?package=PdfPig&version=0.1.9-alpha-20231119-4537e&prerelease

<image src="https://raw.githubusercontent.com/UglyToad/Pdf/master/documentation/pdfpig.png" width="128px" height="128px"/>


codecov Gitter nuget

This project allows users to read and extract text and other content from PDF files. In addition the library can be used to create simple PDF documents containing text and geometrical shapes.

This project aims to port PDFBox to C#.

Migrating to 0.1.6 from 0.1.x? Use this guide: migration to 0.1.6.


The package is available via the releases tab or from Nuget:


Or from the package manager console:

> Install-Package PdfPig

While the version is below 1.0.0 minor versions will change the public API without warning (SemVer will not be followed until 1.0.0 is reached).

Get Started

The simplest usage at this stage is to open a document, reading the words from every page:

using (PdfDocument document = PdfDocument.Open(@"C:\Documents\document.pdf"))
    foreach (Page page in document.GetPages())
        string pageText = page.Text;

        foreach (Word word in page.GetWords())

An example of the output of this is shown below:

Image shows three words 'Write something in' in 2 sections, the top section is the normal PDF output, the bottom section is the same text with 3 word bounding boxes in pink and letter bounding boxes in blue-green

Where for the PDF text ("Write something in") shown at the top the 3 words (in pink) are detected and each word contains the individual letters with glyph bounding boxes.

To create documents use the class PdfDocumentBuilder. The Standard 14 fonts provide a quick way to get started:

PdfDocumentBuilder builder = new PdfDocumentBuilder();

PdfPageBuilder page = builder.AddPage(PageSize.A4);

// Fonts must be registered with the document builder prior to use to prevent duplication.
PdfDocumentBuilder.AddedFont font = builder.AddStandard14Font(Standard14Font.Helvetica);

page.AddText("Hello World!", 12, new PdfPoint(25, 700), font);

byte[] documentBytes = builder.Build();

File.WriteAllBytes(@"C:\git\newPdf.pdf", documentBytes);

The output is a 1 page PDF document with the text "Hello World!" in Helvetica near the top of the page:

Image shows a PDF document in Google Chrome's PDF viewer. The text "Hello World!" is visible

Each font must be registered with the PdfDocumentBuilder prior to use enable pages to share the font resources. Only Standard 14 fonts and TrueType fonts (.ttf) are supported.


The PdfDocument class provides access to the contents of a document loaded either from file or passed in as bytes. To open from a file use the PdfDocument.Open static method:

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

using (PdfDocument document = PdfDocument.Open(@"C:\my-file.pdf"))
    int pageCount = document.NumberOfPages;

    // Page number starts from 1, not 0.
    Page page = document.GetPage(1);

    decimal widthInPoints = page.Width;
    decimal heightInPoints = page.Height;

    string text = page.Text;

PdfDocument should only be used in a using statement since it implements IDisposable (unless the consumer disposes of it elsewhere).

Encrypted documents can be opened by PdfPig. To provide an owner or user password provide the optional ParsingOptions when calling Open with the Password property defined. For example:

using (PdfDocument document = PdfDocument.Open(@"C:\my-file.pdf",  new ParsingOptions { Password = "password here" }))

You can also provide a list of passwords to try:

using (PdfDocument document = PdfDocument.Open(@"C:\file.pdf", new ParsingOptions
    Passwords = new List<string> { "One", "Two" }

The document contains the version of the PDF specification it complies with, accessed by document.Version:

decimal version = document.Version;

Document Creation (0.0.5)

The PdfDocumentBuilder creates a new document with no pages or content.

For text content, a font must be registered with the builder. This library supports Standard 14 fonts provided by Adobe by default and TrueType format fonts.

To add a Standard 14 font use:

public AddedFont AddStandard14Font(Standard14Font type)

Or for a TrueType font use:

AddedFont AddTrueTypeFont(IReadOnlyList<byte> fontFileBytes)

Passing in the bytes of a TrueType file (.ttf). You can check the suitability of a TrueType file for embedding in a PDF document using:

bool CanUseTrueTypeFont(IReadOnlyList<byte> fontFileBytes, out IReadOnlyList<string> reasons)

Which provides a list of reasons why the font cannot be used if the check fails. You should check the license for a TrueType font prior to use, since the compressed font file is embedded in, and distributed with, the resultant document.

The AddedFont class represents a key to the font stored on the document builder. This must be provided when adding text content to pages. To add a page to a document use:

PdfPageBuilder AddPage(PageSize size, bool isPortrait = true)

This creates a new PdfPageBuilder with the specified size. The first added page is page number 1, then 2, then 3, etc. The page builder supports adding text, drawing lines and rectangles and measuring the size of text prior to drawing.

To draw lines and rectangles use the methods:

void DrawLine(PdfPoint from, PdfPoint to, decimal lineWidth = 1)
void DrawRectangle(PdfPoint position, decimal width, decimal height, decimal lineWidth = 1)

The line width can be varied and defaults to 1. Rectangles are unfilled and the fill color cannot be changed at present.

To write text to the page you must have a reference to an AddedFont from the methods on PdfDocumentBuilder as described above. You can then draw the text to the page using:

IReadOnlyList<Letter> AddText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)

Where position is the baseline of the text to draw. Currently only ASCII text is supported. You can also measure the resulting size of text prior to drawing using the method:

IReadOnlyList<Letter> MeasureText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)

Which does not change the state of the page, unlike AddText.

Changing the RGB color of text, lines and rectangles is supported using:

void SetStrokeColor(byte r, byte g, byte b)
void SetTextAndFillColor(byte r, byte g, byte b)

Which take RGB values between 0 and 255. The color will remain active for all operations called after these methods until reset is called using:

void ResetColor()

Which resets the color for stroke, fill and text drawing to black.

Document Information

The PdfDocument provides access to the document metadata as DocumentInformation defined in the PDF file. These tend not to be provided therefore most of these entries will be null:

PdfDocument document = PdfDocument.Open(fileName);

// The name of the program used to convert this document to PDF.
string producer = document.Information.Producer;

// The title given to the document
string title = document.Information.Title;
// etc...

Document Structure (0.0.3)

The document now has a Structure member:

UglyToad.PdfPig.Structure structure = document.Structure;

This provides access to tokenized PDF document content:

Catalog catalog = structure.Catalog;
DictionaryToken pagesDictionary = catalog.PagesDictionary;

The pages dictionary is the root of the pages tree within a PDF document. The structure also exposes a GetObject(IndirectReference reference) method which allows random access to any object in the PDF as long as its identifier number is known. This is an identifier of the form 69 0 R where 69 is the object number and 0 is the generation.


The Page contains the page width and height in points as well as mapping to the PageSize enum:

PageSize size = Page.Size;

bool isA4 = size == PageSize.A4;

Page provides access to the text of the page:

string text = page.Text;

There is a new (0.0.3) method which provides access to the words. This uses basic heuristics and is not reliable or well-tested:

IEnumerable<Word> words = page.GetWords();

You can also (0.0.6) access the raw operations used in the page's content stream for drawing graphics and content on the page:

IReadOnlyList<IGraphicsStateOperation> operations = page.Operations;

Consult the PDF specification for the meaning of individual operators.

There is also an early access (0.0.3) API for retrieving the raw bytes of PDF image objects per page:

IEnumerable<XObjectImage> images = page.ExperimentalAccess.GetRawImages();

This API will be changed in future releases.


Due to the way a PDF is structured internally the page text may not be a readable representation of the text as it appears in the document. Since PDF is a presentation format, text can be drawn in any order, not necessarily reading order. This means spaces may be missing or words may be in unexpected positions in the text.

To help users resolve actual text order on the page, the Page file provides access to a list of the letters:

IReadOnlyList<Letter> letters = page.Letters;

These letters contain:

  • The text of the letter: letter.Value.
  • The location of the lower left of the letter: letter.Location.
  • The width of the letter: letter.Width.
  • The font size in unscaled relative text units (these sizes are internal to the PDF and do not correspond to sizes in pixels, points or other units): letter.FontSize.
  • The name of the font used to render the letter if available: letter.FontName.
  • A rectangle which is the smallest rectangle that completely contains the visible region of the letter/glyph: letter.GlyphRectangle.
  • The points at the start and end of the baseline StartBaseLine and EndBaseLine which indicate if the letter is rotated. The TextDirection indicates if this is a commonly used rotation or a custom rotation.

Letter position is measured in PDF coordinates where the origin is the lower left corner of the page. Therefore a higher Y value means closer to the top of the page.

Annotations (0.0.5)

Early support for retrieving annotations on each page is provided using the method:


This call is not cached and the document must not have been disposed prior to use. The annotations API may change in future.

Bookmarks (0.0.10)

The bookmarks (outlines) of a document may be retrieved at the document level:

bool hasBookmarks = document.TryGetBookmarks(out Bookmarks bookmarks);

This will return false if the document does not define any bookmarks.

Forms (0.0.10)

Form fields for interactive forms (AcroForms) can be retrieved using:

bool hasForm = document.TryGetForm(out AcroForm form);

This will return false if the document does not contain a form.

The fields can be accessed using the AcroForm's Fields property. Since the form is defined at the document level this will return fields from all pages in the document. Fields are of the types defined by the enum AcroFieldType, for example PushButton, Checkbox, Text, etc.

A page has a method to extract hyperlinks (annotations of link type):

IReadOnlyList<UglyToad.PdfPig.Content.Hyperlink> hyperlinks = page.GetHyperlinks();

TrueType (0.1.0)

The classes used to work with TrueType fonts in the PDF file are now available for public consumption. Given an input file:

using UglyToad.PdfPig.Fonts.TrueType;
using UglyToad.PdfPig.Fonts.TrueType.Parser;

byte[] fontBytes = System.IO.File.ReadAllBytes(@"C:\font.ttf");
TrueTypeDataBytes input = new TrueTypeDataBytes(fontBytes);
TrueTypeFont font = TrueTypeFontParser.Parse(input);

The parsed font can then be inspected.

Embedded Files (0.1.0)

PDF files may contain other files entirely embedded inside them for document annotations. The list of embedded files and their byte content may be accessed:

if (document.Advanced.TryGetEmbeddedFiles(out IReadOnlyList<EmbeddedFile> files)
    && files.Count > 0)
    var firstFile = files[0];
    string name = firstFile.Name;
    IReadOnlyList<byte> bytes = firstFile.Bytes;

Merging (0.1.2)

You can merge 2 or more existing PDF files using the PdfMerger class:

var resultFileBytes = PdfMerger.Merge(filePath1, filePath2);
File.WriteAllBytes(@"C:\pdfs\outputfilename.pdf", resultFileBytes);

API Reference

If you wish to generate doxygen documentation, run doxygen doxygen-docs and open docs/doxygen/html/index.html.


Please do file an issue if you encounter a bug.

However in order for us to assist you, you must provide the file which causes your issue. Please host this in a publically available place.


This project wouldn't be possible without the work done by the PDFBox team and the Apache Foundation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (32)

Showing the top 5 NuGet packages that depend on PdfPig:

Package Downloads
OrchardCore.Media The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. The media module adds media management support.


Extract tables from PDF files (port of tabula-java using PdfPig).

Microsoft.DocAsCode.HtmlToPdf The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

UmbracoCms.UmbracoExamine.PDF The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Umbraco indexing for PDF files using UmbracoExamine

Verify.PdfPig The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Extends Verify (https://github.com/VerifyTests/Verify) to allow verification via PdfPig.

GitHub repositories (11)

Showing the top 5 popular GitHub repositories that depend on PdfPig:

Repository Stars
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
Static site generator for .NET API documentation.
The AI Agent Framework in .NET
Index and query any data using LLM and natural language, tracking sources and showing citations.
Mass processing data with a complete ETL for .net developers
Version Downloads Last updated
0.1.9-alpha-20240219-c2536 1,095 2/19/2024
0.1.9-alpha-20240217-f4e75 72 2/17/2024
0.1.9-alpha-20240216-f78b1 72 2/16/2024
0.1.9-alpha-20240215-3bdc9 1,117 2/15/2024
0.1.9-alpha-20240208-19734 1,753 2/8/2024
0.1.9-alpha-20240207-23445 638 2/7/2024
0.1.9-alpha-20240128-f886e 1,212 1/28/2024
0.1.9-alpha-20240121-04fc8 1,416 1/21/2024
0.1.9-alpha-20240117-096eb 2,334 1/17/2024
0.1.9-alpha-20240116-4e63e 578 1/16/2024
0.1.9-alpha-20240115-0da7b 441 1/15/2024
0.1.9-alpha-20240114-5953c 190 1/14/2024
0.1.9-alpha-20240112-83519 1,407 1/12/2024
0.1.9-alpha-20240111-88a14 436 1/11/2024
0.1.9-alpha-20240109-8cfaa 2,440 1/9/2024
0.1.9-alpha-20240108-18144 319 1/8/2024
0.1.9-alpha-20231119-4537e 11,923 11/19/2023
0.1.9-alpha-20231113-1bc0e 3,411 11/13/2023
0.1.9-alpha-20231029-17d50 3,660 10/29/2023
0.1.9-alpha-20231026-63096 2,615 10/26/2023
0.1.9-alpha-20231023-ba865 938 10/23/2023
0.1.9-alpha-20231019-c6e2d 1,341 10/19/2023
0.1.9-alpha-20230930-06ac8 4,994 9/30/2023
0.1.9-alpha-20230914-d59d2 3,285 9/14/2023
0.1.9-alpha-20230827-ee756 11,093 8/27/2023
0.1.9-alpha-20230806-4a480 5,683 8/6/2023
0.1.8 790,685 6/5/2023
0.1.8-alpha-20230605-7fe5f 747 6/5/2023
0.1.8-alpha-20230529-6daa2 6,076 5/29/2023
0.1.8-alpha-20230528-5126d 714 5/28/2023
0.1.8-alpha-20230524-20d3c 2,114 5/24/2023
0.1.8-alpha-20230523-11df5 712 5/23/2023
0.1.8-alpha-20230522-c3dd6 1,078 5/22/2023
0.1.8-alpha-20230423-3898f 34,399 4/23/2023
0.1.8-alpha-20230420-147b8 881 4/20/2023
0.1.8-alpha-20230419-2d72d 978 4/19/2023
0.1.8-alpha-20230417-cdc3d 1,092 4/17/2023
0.1.8-alpha-20230415-9eb79 866 4/15/2023
0.1.8-alpha-20230414-42e41 737 4/14/2023
0.1.8-alpha-20230413-46a04 810 4/13/2023
0.1.8-alpha-20230412-db058 1,448 4/12/2023
0.1.8-alpha-20230411-0e39b 814 4/11/2023
0.1.8-alpha-20230403-2e062 6,113 4/3/2023
0.1.8-alpha-20230331-bd4ee 18,268 3/31/2023
0.1.8-alpha-20230327-2daba 4,045 3/27/2023
0.1.8-alpha-20230326-58b33 841 3/26/2023
0.1.8-alpha-20230324-a3a9d 960 3/24/2023
0.1.8-alpha-20230323-a4861 872 3/23/2023
0.1.8-alpha-20230320-c024e 1,220 3/20/2023
0.1.8-alpha-20230318-a5c91 853 3/18/2023
0.1.8-alpha-20230219-999f9 2,338 2/19/2023
0.1.8-alpha-20230117-88aad 3,337 1/17/2023
0.1.8-alpha-20230109-65bc7 1,180 1/9/2023
0.1.7 541,719 12/13/2022
0.1.7-alpha-20221212-c8874 64,849 12/12/2022
0.1.7-alpha-20221210-2aed9 749 12/10/2022
0.1.7-alpha-20220814-2f9a9 5,499 8/14/2022
0.1.7-alpha-20220703-545d1 2,038 7/3/2022
0.1.7-alpha-20220622-fc71a 922 6/22/2022
0.1.7-alpha-20220618-f2188 776 6/18/2022
0.1.7-alpha-20220525-559f3 3,528 5/25/2022
0.1.7-alpha-20220511-ddab5 2,041 5/11/2022
0.1.7-alpha-20220503-4e490 1,579 5/3/2022
0.1.7-alpha-20220426-03692 907 4/26/2022
0.1.6 923,643 4/25/2022
0.1.6-alpha-20220425-2576c 851 4/25/2022
0.1.6-alpha-20220423-801a3 814 4/23/2022
0.1.6-alpha-20220415-cbd02 1,334 4/15/2022
0.1.6-alpha-20220411-09a62 977 4/11/2022
0.1.6-alpha-20220405-c2ecb 1,473 4/5/2022
0.1.6-alpha-20220404-6b085 830 4/4/2022
0.1.6-alpha-20220315-9c83e 3,432 3/15/2022
0.1.6-alpha-20220220-b0a5f 2,939 2/20/2022
0.1.6-alpha-20220116-e54cd 1,889 1/16/2022
0.1.6-alpha-20220113-5b66e 804 1/13/2022
0.1.6-alpha-20220112-b89c8 866 1/12/2022
0.1.6-alpha-20220111-41bfa 1,159 1/11/2022
0.1.5 845,158 9/17/2021
0.1.5-alpha002 4,514 5/9/2021
0.1.5-alpha001 23,391 2/28/2021
0.1.5-alpha-20211231-a57e5 2,007 12/31/2021
0.1.5-alpha-20211026-55244 833 10/26/2021
0.1.5-alpha-20210929-615e8 872 9/29/2021
0.1.5-alpha-20210918-4c36f 844 9/18/2021
0.1.5-alpha-20210828-e8f91 878 8/28/2021
0.1.5-alpha-20210827-e8f91 859 8/27/2021
0.1.5-alpha-20210817-b1f88 891 8/17/2021
0.1.4 537,580 11/29/2020
0.1.3 42,594 11/15/2020
0.1.3-alpha001 2,358 9/4/2020
0.1.2 213,963 7/4/2020
0.1.2-alpha003 1,100 6/20/2020
0.1.2-alpha002 3,373 5/10/2020
0.1.2-alpha001 1,128 4/25/2020
0.1.1 119,442 3/18/2020
0.1.1-alpha001 1,114 3/15/2020
0.1.0 194,284 1/13/2020
0.1.0-beta002 1,035 1/8/2020
0.1.0-beta001 1,016 1/6/2020
0.0.11 1,832 12/17/2019
0.0.10 1,458 12/9/2019
0.0.9 108,359 8/13/2019
0.0.7 1,434 8/3/2019
0.0.6 2,432 5/19/2019
0.0.5 10,195 12/30/2018
0.0.3 1,347 11/27/2018
0.0.1 9,124 2/26/2018
0.0.1-alpha-002 1,368 1/21/2018
0.0.1-alpha-001 1,369 1/10/2018