PdfPig 0.1.1-alpha001

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.1-alpha001                
NuGet\Install-Package PdfPig -Version 0.1.1-alpha001                
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.1-alpha001" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PdfPig --version 0.1.1-alpha001                
#r "nuget: PdfPig, 0.1.1-alpha001"                
#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.1-alpha001&prerelease

// Install PdfPig as a Cake Tool
#tool nuget:?package=PdfPig&version=0.1.1-alpha001&prerelease                

PdfPig

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

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();

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

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.

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

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

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;

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. 
.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 (47)

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.

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

Tabula

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

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 (15)

Showing the top 5 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.
Version Downloads Last updated
0.1.10-alpha-20241216-7ec4e 484 12/16/2024
0.1.10-alpha-20241215-7ec4e 119 12/15/2024
0.1.10-alpha-20241121-7db34 3,220 11/21/2024
0.1.10-alpha-20241114-8ca53 1,461 11/14/2024
0.1.10-alpha-20241103-132ad 796 11/3/2024
0.1.10-alpha-20241031-d3bf6 205 10/31/2024
0.1.10-alpha-20241026-40af4 1,422 10/26/2024
0.1.10-alpha-20241019-e1060 904 10/19/2024
0.1.10-alpha-20241018-ea95a 148 10/18/2024
0.1.10-alpha-20241016-e903b 286 10/16/2024
0.1.10-alpha-20241013-f4054 217 10/13/2024
0.1.10-alpha-20241008-a2580 907 10/8/2024
0.1.10-alpha-20241007-c4672 464 10/7/2024
0.1.9 326,789 10/6/2024
0.1.9-alpha-20240930-eb9a1 4,016 9/30/2024
0.1.9-alpha-20240910-4845f 60,427 9/10/2024
0.1.9-alpha-20240909-09bdd 1,079 9/9/2024
0.1.9-alpha-20240904-cd2a8 1,447 9/4/2024
0.1.9-alpha-20240903-f4d14 731 9/3/2024
0.1.9-alpha-20240902-cf45d 605 9/2/2024
0.1.9-alpha-20240901-b824f 145 9/1/2024
0.1.9-alpha-20240821-b4649 5,565 8/21/2024
0.1.9-alpha-20240721-a99c0 25,686 7/21/2024
0.1.9-alpha-20240702-65c64 9,983 7/2/2024
0.1.9-alpha-20240628-bac00 9,114 6/28/2024
0.1.9-alpha-20240626-14e70 955 6/26/2024
0.1.9-alpha-20240625-dc933 640 6/25/2024
0.1.9-alpha-20240612-d2cae 4,425 6/12/2024
0.1.9-alpha-20240609-affc1 1,019 6/9/2024
0.1.9-alpha-20240601-65a18 2,493 6/1/2024
0.1.9-alpha-20240530-d7e43 766 5/30/2024
0.1.9-alpha-20240510-d86c2 10,472 5/10/2024
0.1.9-alpha-20240509-5a8e6 199 5/9/2024
0.1.9-alpha-20240508-995f2 281 5/8/2024
0.1.9-alpha-20240507-93779 267 5/7/2024
0.1.9-alpha-20240506-b6e03 200 5/6/2024
0.1.9-alpha-20240504-da44e 167 5/4/2024
0.1.9-alpha-20240429-7f42a 4,098 4/29/2024
0.1.9-alpha-20240419-1ef2e 7,483 4/19/2024
0.1.9-alpha-20240413-0f707 11,186 4/13/2024
0.1.9-alpha-20240406-2d6cb 3,612 4/6/2024
0.1.9-alpha-20240402-f6292 7,336 4/2/2024
0.1.9-alpha-20240324-e7896 4,848 3/24/2024
0.1.9-alpha-20240318-69e2b 11,965 3/18/2024
0.1.9-alpha-20240312-845e3 3,123 3/12/2024
0.1.9-alpha-20240307-ac027 1,825 3/7/2024
0.1.9-alpha-20240219-c2536 36,440 2/19/2024
0.1.9-alpha-20240217-f4e75 443 2/17/2024
0.1.9-alpha-20240216-f78b1 348 2/16/2024
0.1.9-alpha-20240215-3bdc9 1,972 2/15/2024
0.1.9-alpha-20240208-19734 4,620 2/8/2024
0.1.9-alpha-20240207-23445 1,383 2/7/2024
0.1.9-alpha-20240128-f886e 7,374 1/28/2024
0.1.9-alpha-20240121-04fc8 10,500 1/21/2024
0.1.9-alpha-20240117-096eb 5,686 1/17/2024
0.1.9-alpha-20240116-4e63e 849 1/16/2024
0.1.9-alpha-20240115-0da7b 881 1/15/2024
0.1.9-alpha-20240114-5953c 460 1/14/2024
0.1.9-alpha-20240112-83519 1,675 1/12/2024
0.1.9-alpha-20240111-88a14 841 1/11/2024
0.1.9-alpha-20240109-8cfaa 9,829 1/9/2024
0.1.9-alpha-20240108-18144 616 1/8/2024
0.1.9-alpha-20231119-4537e 17,468 11/19/2023
0.1.9-alpha-20231113-1bc0e 9,794 11/13/2023
0.1.9-alpha-20231029-17d50 5,547 10/29/2023
0.1.9-alpha-20231026-63096 5,799 10/26/2023
0.1.9-alpha-20231023-ba865 1,991 10/23/2023
0.1.9-alpha-20231019-c6e2d 4,415 10/19/2023
0.1.9-alpha-20230930-06ac8 7,481 9/30/2023
0.1.9-alpha-20230914-d59d2 7,068 9/14/2023
0.1.9-alpha-20230827-ee756 12,032 8/27/2023
0.1.9-alpha-20230806-4a480 9,052 8/6/2023
0.1.8 2,994,718 6/5/2023
0.1.8-alpha-20230605-7fe5f 1,005 6/5/2023
0.1.8-alpha-20230529-6daa2 6,655 5/29/2023
0.1.8-alpha-20230528-5126d 948 5/28/2023
0.1.8-alpha-20230524-20d3c 4,394 5/24/2023
0.1.8-alpha-20230523-11df5 960 5/23/2023
0.1.8-alpha-20230522-c3dd6 1,368 5/22/2023
0.1.8-alpha-20230423-3898f 38,335 4/23/2023
0.1.8-alpha-20230420-147b8 1,150 4/20/2023
0.1.8-alpha-20230419-2d72d 1,276 4/19/2023
0.1.8-alpha-20230417-cdc3d 1,361 4/17/2023
0.1.8-alpha-20230415-9eb79 1,090 4/15/2023
0.1.8-alpha-20230414-42e41 989 4/14/2023
0.1.8-alpha-20230413-46a04 1,082 4/13/2023
0.1.8-alpha-20230412-db058 1,721 4/12/2023
0.1.8-alpha-20230411-0e39b 1,105 4/11/2023
0.1.8-alpha-20230403-2e062 10,576 4/3/2023
0.1.8-alpha-20230331-bd4ee 18,833 3/31/2023
0.1.8-alpha-20230327-2daba 7,306 3/27/2023
0.1.8-alpha-20230326-58b33 1,111 3/26/2023
0.1.8-alpha-20230324-a3a9d 1,337 3/24/2023
0.1.8-alpha-20230323-a4861 1,127 3/23/2023
0.1.8-alpha-20230320-c024e 1,511 3/20/2023
0.1.8-alpha-20230318-a5c91 1,086 3/18/2023
0.1.8-alpha-20230219-999f9 2,784 2/19/2023
0.1.8-alpha-20230117-88aad 5,454 1/17/2023
0.1.8-alpha-20230109-65bc7 1,522 1/9/2023
0.1.7 801,118 12/13/2022
0.1.7-alpha-20221212-c8874 72,460 12/12/2022
0.1.7-alpha-20221210-2aed9 1,043 12/10/2022
0.1.7-alpha-20220814-2f9a9 6,421 8/14/2022
0.1.7-alpha-20220703-545d1 3,388 7/3/2022
0.1.7-alpha-20220622-fc71a 1,206 6/22/2022
0.1.7-alpha-20220618-f2188 1,067 6/18/2022
0.1.7-alpha-20220525-559f3 4,805 5/25/2022
0.1.7-alpha-20220511-ddab5 2,346 5/11/2022
0.1.7-alpha-20220503-4e490 1,897 5/3/2022
0.1.7-alpha-20220426-03692 1,197 4/26/2022
0.1.6 1,190,111 4/25/2022
0.1.6-alpha-20220425-2576c 1,135 4/25/2022
0.1.6-alpha-20220423-801a3 1,094 4/23/2022
0.1.6-alpha-20220415-cbd02 1,735 4/15/2022
0.1.6-alpha-20220411-09a62 1,226 4/11/2022
0.1.6-alpha-20220405-c2ecb 1,784 4/5/2022
0.1.6-alpha-20220404-6b085 1,084 4/4/2022
0.1.6-alpha-20220315-9c83e 4,273 3/15/2022
0.1.6-alpha-20220220-b0a5f 3,515 2/20/2022
0.1.6-alpha-20220116-e54cd 2,284 1/16/2022
0.1.6-alpha-20220113-5b66e 1,059 1/13/2022
0.1.6-alpha-20220112-b89c8 1,112 1/12/2022
0.1.6-alpha-20220111-41bfa 1,949 1/11/2022
0.1.5 989,514 9/17/2021
0.1.5-alpha002 5,325 5/9/2021
0.1.5-alpha001 26,082 2/28/2021
0.1.5-alpha-20211231-a57e5 2,557 12/31/2021
0.1.5-alpha-20211026-55244 1,123 10/26/2021
0.1.5-alpha-20210929-615e8 1,160 9/29/2021
0.1.5-alpha-20210918-4c36f 1,136 9/18/2021
0.1.5-alpha-20210828-e8f91 1,134 8/28/2021
0.1.5-alpha-20210827-e8f91 1,159 8/27/2021
0.1.5-alpha-20210817-b1f88 1,157 8/17/2021
0.1.4 631,996 11/29/2020
0.1.3 51,512 11/15/2020
0.1.3-alpha001 2,802 9/4/2020
0.1.2 251,388 7/4/2020
0.1.2-alpha003 1,345 6/20/2020
0.1.2-alpha002 3,613 5/10/2020
0.1.2-alpha001 1,383 4/25/2020
0.1.1 135,058 3/18/2020
0.1.1-alpha001 1,377 3/15/2020
0.1.0 202,909 1/13/2020
0.1.0-beta002 1,265 1/8/2020
0.1.0-beta001 1,264 1/6/2020
0.0.11 2,147 12/17/2019
0.0.10 1,806 12/9/2019
0.0.9 109,403 8/13/2019
0.0.7 1,741 8/3/2019
0.0.6 2,769 5/19/2019
0.0.5 18,998 12/30/2018
0.0.3 1,577 11/27/2018
0.0.1 11,109 2/26/2018
0.0.1-alpha-002 1,591 1/21/2018
0.0.1-alpha-001 1,580 1/10/2018