PdfPig 0.1.0-beta001

This is a prerelease version of PdfPig.
There is a newer version of this package available.
See the version list below for details.
dotnet add package PdfPig --version 0.1.0-beta001
                    
NuGet\Install-Package PdfPig -Version 0.1.0-beta001
                    
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.0-beta001" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PdfPig" Version="0.1.0-beta001" />
                    
Directory.Packages.props
<PackageReference Include="PdfPig" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PdfPig --version 0.1.0-beta001
                    
#r "nuget: PdfPig, 0.1.0-beta001"
                    
#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.
#addin nuget:?package=PdfPig&version=0.1.0-beta001&prerelease
                    
Install PdfPig as a Cake Addin
#tool nuget:?package=PdfPig&version=0.1.0-beta001&prerelease
                    
Install PdfPig as a Cake Tool

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.

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

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())
        {
            Console.WriteLine(word.Text);
        }
    }
}

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, 520), font);

byte[] documentBytes = builder.Build();

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

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.

Usage

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 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;
// 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.

Page

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:

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

Letter

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;

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:

page.ExperimentalAccess.GetAnnotations()

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.

Credit

This project wouldn't be possible without the work done by the PDFBox team and the Apache Foundation. Any bugs in the code are entirely my fault.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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 net45 is compatible.  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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (58)

Showing the top 5 NuGet packages that depend on PdfPig:

Package Downloads
Microsoft.KernelMemory.Core

The package contains the the core logic and abstractions of Kernel Memory, not including extensions.

OrchardCore.Application.Cms.Core.Targets

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with TheAdmin theme but without any front-end Themes.

Tabula

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

OrchardCore.Application.Cms.Targets

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with following themes. - TheAdmin Theme - SafeMode Theme - TheAgency Theme - TheBlog Theme - TheComingSoon Theme - TheTheme theme

FileCurator

FileCurator is a simple manager for your files. It tries to give them a common interface to deal with files whether on your system or other locations.

GitHub repositories (22)

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

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
OrchardCMS/OrchardCore
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.
dotnet/docfx
Static site generator for .NET API documentation.
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
microsoft/kernel-memory
RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
microsoft/ai-dev-gallery
An open-source project for Windows developers to learn how to add AI with local models and APIs to Windows apps.
getcellm/cellm
Use LLMs in Excel formulas
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
paillave/Etl.Net
Mass processing data with a complete ETL for .net developers
revenz/FileFlows
FileFlows is a file processing application that can execute actions against a file in a tree flow structure.
dotnet/ai-samples
BobLd/DocumentLayoutAnalysis
Document Layout Analysis resources repos for development with PdfPig.
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
majorsilence/My-FyiReporting
Majorsilence Reporting, .NET report designer, viewer, and pdf creation. Fork of fyireporting,
KrystynaSlusarczykLearning/UltimateCSharpMasterclass
microsoft/project-oagents
Experimental AI Agents Framework
microsoft/hidtools
Human Interface Device (HID) Tools for Windows and Devices
SaboZhang/EasyTidy
EasyTidy A simple file auto-classification tool makes it easy to create automatic workflows with files. / EasyTidy 一个简单的文件自动分类整理工具 轻松创建文件的自动工作流程
BobLd/tabula-sharp
Extract tables from PDF files (port of tabula-java)
SteveSandersonMS/dotnet-ai-workshop
Version Downloads Last updated
0.1.11-alpha-20250407-24902 316 4 days ago
0.1.11-alpha-20250331-4fbcc 683 11 days ago
0.1.11-alpha-20250330-5fb36 189 12 days ago
0.1.11-alpha-20250330-4fbcc 253 11 days ago
0.1.11-alpha-20250327-74d61 454 15 days ago
0.1.11-alpha-20250324-0754e 542 18 days ago
0.1.11-alpha-20250310-204f4 2,106 a month ago
0.1.11-alpha-20250309-a4a0f 163 a month ago
0.1.10 94,062 a month ago
0.1.10-alpha-20250303-1b3c7 903 a month ago
0.1.10-alpha-20250224-f26e7 812 2 months ago
0.1.10-alpha-20250223-f26e7 333 2 months ago
0.1.10-alpha-20250223-d973e 112 2 months ago
0.1.10-alpha-20250222-d973e 155 2 months ago
0.1.10-alpha-20250222-c4a23 260 2 months ago
0.1.10-alpha-20250220-5a06e 779 2 months ago
0.1.10-alpha-20250209-1660c 1,432 2 months ago
0.1.10-alpha-20250208-1660c 271 2 months ago
0.1.10-alpha-20250203-fdb88 3,111 2 months ago
0.1.10-alpha-20250122-52098 2,223 3 months ago
0.1.10-alpha-20250120-b7e22 431 3 months ago
0.1.10-alpha-20250115-92d34 929 3 months ago
0.1.10-alpha-20250106-f86cc 893 3 months ago
0.1.10-alpha-20250105-d1779 145 3 months ago
0.1.10-alpha-20250101-2b14a 464 3 months ago
0.1.10-alpha-20241229-50dca 575 3 months ago
0.1.10-alpha-20241216-7ec4e 2,159 4 months ago
0.1.10-alpha-20241215-7ec4e 167 4 months ago
0.1.10-alpha-20241121-7db34 8,463 5 months ago
0.1.10-alpha-20241114-8ca53 3,463 5 months ago
0.1.10-alpha-20241103-132ad 1,047 5 months ago
0.1.10-alpha-20241031-d3bf6 235 5 months ago
0.1.10-alpha-20241026-40af4 2,549 6 months ago
0.1.10-alpha-20241019-e1060 1,729 6 months ago
0.1.10-alpha-20241018-ea95a 249 6 months ago
0.1.10-alpha-20241016-e903b 307 6 months ago
0.1.10-alpha-20241013-f4054 282 6 months ago
0.1.10-alpha-20241008-a2580 1,361 6 months ago
0.1.10-alpha-20241007-c4672 734 6 months ago
0.1.9 1,078,151 6 months ago
0.1.9-alpha-20240930-eb9a1 5,613 6 months ago
0.1.9-alpha-20240910-4845f 171,618 7 months ago
0.1.9-alpha-20240909-09bdd 1,108 7 months ago
0.1.9-alpha-20240904-cd2a8 1,549 7 months ago
0.1.9-alpha-20240903-f4d14 749 7 months ago
0.1.9-alpha-20240902-cf45d 623 7 months ago
0.1.9-alpha-20240901-b824f 162 7 months ago
0.1.9-alpha-20240821-b4649 6,065 8 months ago
0.1.9-alpha-20240721-a99c0 39,314 9 months ago
0.1.9-alpha-20240702-65c64 10,983 9 months ago
0.1.9-alpha-20240628-bac00 11,656 9 months ago
0.1.9-alpha-20240626-14e70 969 10 months ago
0.1.9-alpha-20240625-dc933 758 10 months ago
0.1.9-alpha-20240612-d2cae 4,533 10 months ago
0.1.9-alpha-20240609-affc1 1,107 6/9/2024
0.1.9-alpha-20240601-65a18 2,582 6/1/2024
0.1.9-alpha-20240530-d7e43 787 5/30/2024
0.1.9-alpha-20240510-d86c2 12,159 5/10/2024
0.1.9-alpha-20240509-5a8e6 214 5/9/2024
0.1.9-alpha-20240508-995f2 294 5/8/2024
0.1.9-alpha-20240507-93779 310 5/7/2024
0.1.9-alpha-20240506-b6e03 236 5/6/2024
0.1.9-alpha-20240504-da44e 178 5/4/2024
0.1.9-alpha-20240429-7f42a 4,121 4/29/2024
0.1.9-alpha-20240419-1ef2e 7,617 4/19/2024
0.1.9-alpha-20240413-0f707 12,190 4/13/2024
0.1.9-alpha-20240406-2d6cb 3,848 4/6/2024
0.1.9-alpha-20240402-f6292 9,067 4/2/2024
0.1.9-alpha-20240324-e7896 5,158 3/24/2024
0.1.9-alpha-20240318-69e2b 13,656 3/18/2024
0.1.9-alpha-20240312-845e3 3,201 3/12/2024
0.1.9-alpha-20240307-ac027 1,852 3/7/2024
0.1.9-alpha-20240219-c2536 36,637 2/19/2024
0.1.9-alpha-20240217-f4e75 463 2/17/2024
0.1.9-alpha-20240216-f78b1 365 2/16/2024
0.1.9-alpha-20240215-3bdc9 2,001 2/15/2024
0.1.9-alpha-20240208-19734 4,690 2/8/2024
0.1.9-alpha-20240207-23445 1,393 2/7/2024
0.1.9-alpha-20240128-f886e 8,207 1/28/2024
0.1.9-alpha-20240121-04fc8 13,471 1/21/2024
0.1.9-alpha-20240117-096eb 6,715 1/17/2024
0.1.9-alpha-20240116-4e63e 863 1/16/2024
0.1.9-alpha-20240115-0da7b 892 1/15/2024
0.1.9-alpha-20240114-5953c 501 1/14/2024
0.1.9-alpha-20240112-83519 1,727 1/12/2024
0.1.9-alpha-20240111-88a14 859 1/11/2024
0.1.9-alpha-20240109-8cfaa 12,351 1/9/2024
0.1.9-alpha-20240108-18144 630 1/8/2024
0.1.9-alpha-20231119-4537e 18,119 11/19/2023
0.1.9-alpha-20231113-1bc0e 11,223 11/13/2023
0.1.9-alpha-20231029-17d50 5,805 10/29/2023
0.1.9-alpha-20231026-63096 6,763 10/26/2023
0.1.9-alpha-20231023-ba865 2,047 10/23/2023
0.1.9-alpha-20231019-c6e2d 4,557 10/19/2023
0.1.9-alpha-20230930-06ac8 7,734 9/30/2023
0.1.9-alpha-20230914-d59d2 8,762 9/14/2023
0.1.9-alpha-20230827-ee756 12,358 8/27/2023
0.1.9-alpha-20230806-4a480 9,310 8/6/2023
0.1.8 3,690,580 6/5/2023
0.1.8-alpha-20230605-7fe5f 1,021 6/5/2023
0.1.8-alpha-20230529-6daa2 6,739 5/29/2023
0.1.8-alpha-20230528-5126d 966 5/28/2023
0.1.8-alpha-20230524-20d3c 5,287 5/24/2023
0.1.8-alpha-20230523-11df5 974 5/23/2023
0.1.8-alpha-20230522-c3dd6 1,395 5/22/2023
0.1.8-alpha-20230423-3898f 39,104 4/23/2023
0.1.8-alpha-20230420-147b8 1,179 4/20/2023
0.1.8-alpha-20230419-2d72d 1,304 4/19/2023
0.1.8-alpha-20230417-cdc3d 1,389 4/17/2023
0.1.8-alpha-20230415-9eb79 1,141 4/15/2023
0.1.8-alpha-20230414-42e41 1,010 4/14/2023
0.1.8-alpha-20230413-46a04 1,105 4/13/2023
0.1.8-alpha-20230412-db058 1,774 4/12/2023
0.1.8-alpha-20230411-0e39b 1,127 4/11/2023
0.1.8-alpha-20230403-2e062 13,080 4/3/2023
0.1.8-alpha-20230331-bd4ee 18,898 3/31/2023
0.1.8-alpha-20230327-2daba 8,075 3/27/2023
0.1.8-alpha-20230326-58b33 1,137 3/26/2023
0.1.8-alpha-20230324-a3a9d 1,384 3/24/2023
0.1.8-alpha-20230323-a4861 1,179 3/23/2023
0.1.8-alpha-20230320-c024e 1,530 3/20/2023
0.1.8-alpha-20230318-a5c91 1,102 3/18/2023
0.1.8-alpha-20230219-999f9 2,853 2/19/2023
0.1.8-alpha-20230117-88aad 5,968 1/17/2023
0.1.8-alpha-20230109-65bc7 1,549 1/9/2023
0.1.7 873,397 12/13/2022
0.1.7-alpha-20221212-c8874 72,482 12/12/2022
0.1.7-alpha-20221210-2aed9 1,068 12/10/2022
0.1.7-alpha-20220814-2f9a9 6,690 8/14/2022
0.1.7-alpha-20220703-545d1 3,689 7/3/2022
0.1.7-alpha-20220622-fc71a 1,246 6/22/2022
0.1.7-alpha-20220618-f2188 1,093 6/18/2022
0.1.7-alpha-20220525-559f3 5,673 5/25/2022
0.1.7-alpha-20220511-ddab5 2,371 5/11/2022
0.1.7-alpha-20220503-4e490 1,925 5/3/2022
0.1.7-alpha-20220426-03692 1,249 4/26/2022
0.1.6 1,275,008 4/25/2022
0.1.6-alpha-20220425-2576c 1,161 4/25/2022
0.1.6-alpha-20220423-801a3 1,122 4/23/2022
0.1.6-alpha-20220415-cbd02 1,772 4/15/2022
0.1.6-alpha-20220411-09a62 1,249 4/11/2022
0.1.6-alpha-20220405-c2ecb 1,838 4/5/2022
0.1.6-alpha-20220404-6b085 1,113 4/4/2022
0.1.6-alpha-20220315-9c83e 6,247 3/15/2022
0.1.6-alpha-20220220-b0a5f 3,653 2/20/2022
0.1.6-alpha-20220116-e54cd 2,457 1/16/2022
0.1.6-alpha-20220113-5b66e 1,086 1/13/2022
0.1.6-alpha-20220112-b89c8 1,146 1/12/2022
0.1.6-alpha-20220111-41bfa 2,308 1/11/2022
0.1.5 1,050,495 9/17/2021
0.1.5-alpha002 5,574 5/9/2021
0.1.5-alpha001 27,132 2/28/2021
0.1.5-alpha-20211231-a57e5 3,049 12/31/2021
0.1.5-alpha-20211026-55244 1,151 10/26/2021
0.1.5-alpha-20210929-615e8 1,198 9/29/2021
0.1.5-alpha-20210918-4c36f 1,164 9/18/2021
0.1.5-alpha-20210828-e8f91 1,163 8/28/2021
0.1.5-alpha-20210827-e8f91 1,184 8/27/2021
0.1.5-alpha-20210817-b1f88 1,212 8/17/2021
0.1.4 661,894 11/29/2020
0.1.3 55,010 11/15/2020
0.1.3-alpha001 2,876 9/4/2020
0.1.2 262,951 7/4/2020
0.1.2-alpha003 1,377 6/20/2020
0.1.2-alpha002 3,649 5/10/2020
0.1.2-alpha001 1,417 4/25/2020
0.1.1 140,043 3/18/2020
0.1.1-alpha001 1,412 3/15/2020
0.1.0 206,485 1/13/2020
0.1.0-beta002 1,296 1/8/2020
0.1.0-beta001 1,295 1/6/2020
0.0.11 2,213 12/17/2019
0.0.10 1,892 12/9/2019
0.0.9 109,582 8/13/2019
0.0.7 1,779 8/3/2019
0.0.6 2,814 5/19/2019
0.0.5 22,473 12/30/2018
0.0.3 1,623 11/27/2018
0.0.1 12,615 2/26/2018
0.0.1-alpha-002 1,754 1/21/2018
0.0.1-alpha-001 1,738 1/10/2018