PdfPig 0.1.5

Install-Package PdfPig -Version 0.1.5
dotnet add package PdfPig --version 0.1.5
<PackageReference Include="PdfPig" Version="0.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PdfPig --version 0.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PdfPig, 0.1.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.5

// Install PdfPig as a Cake Tool
#tool nuget:?package=PdfPig&version=0.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


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#.

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"))
    for (var i = 0; i < document.NumberOfPages; i++)
        // This starts at 1 rather than 0.
        var page = document.GetPage(i + 1);

        foreach (var word in page.GetWords())

New in v0.0.5 - To create documents use the class PdfDocumentBuilder. Though they are deprecated within the PDF specification 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, 520), font);

byte[] documentBytes = builder.Build();


Each font must be registered with the PdfDocumentBuilder prior to use enable pages to share the font resources. Currently 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 page = document.GetPage(1);

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

    string text = page.Text;

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

decimal version = document.Version;

Document Creation

New in v0.0.5 - The PdfDocumentBuilder creates a new document with no pages or content. First, for text content, a font must be registered with the builder. Currently this 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

New in 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 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;

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;


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

NuGet packages (7)

Showing the top 5 NuGet packages that depend on PdfPig:

Package Downloads

Umbraco indexing for PDF files using UmbracoExamine


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


Convert your pdf annotations into a literature review


Headless CMS based on ASP.NET Core and Blazor


Umbraco indexing for PDF files using UmbracoExamine

GitHub repositories (1)

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

Repository Stars
Document Layout Analysis resources repos for development with PdfPig.
Version Downloads Last updated
0.1.5 20,484 9/17/2021
0.1.5-alpha002 1,855 5/9/2021
0.1.5-alpha001 6,790 2/28/2021
0.1.5-alpha-20211026-55244 80 10/26/2021
0.1.5-alpha-20210929-615e8 93 9/29/2021
0.1.5-alpha-20210918-4c36f 102 9/18/2021
0.1.5-alpha-20210828-e8f91 112 8/28/2021
0.1.5-alpha-20210827-e8f91 83 8/27/2021
0.1.5-alpha-20210817-b1f88 131 8/17/2021
0.1.4 207,895 11/29/2020
0.1.3 12,631 11/15/2020
0.1.3-alpha001 1,183 9/4/2020
0.1.2 88,884 7/4/2020
0.1.2-alpha003 347 6/20/2020
0.1.2-alpha002 698 5/10/2020
0.1.2-alpha001 370 4/25/2020
0.1.1 65,433 3/18/2020
0.1.1-alpha001 320 3/15/2020
0.1.0 150,865 1/13/2020
0.1.0-beta002 296 1/8/2020
0.1.0-beta001 298 1/6/2020
0.0.11 781 12/17/2019
0.0.10 431 12/9/2019
0.0.9 57,202 8/13/2019
0.0.7 524 8/3/2019
0.0.6 1,483 5/19/2019
0.0.5 2,329 12/30/2018
0.0.3 548 11/27/2018
0.0.1 5,844 2/26/2018
0.0.1-alpha-002 630 1/21/2018
0.0.1-alpha-001 664 1/10/2018