PdfPig 0.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package PdfPig --version 0.0.3
NuGet\Install-Package PdfPig -Version 0.0.3
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.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PdfPig --version 0.0.3
#r "nuget: PdfPig, 0.0.3"
#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.0.3

// Install PdfPig as a Cake Tool
#tool nuget:?package=PdfPig&version=0.0.3

PdfPig

Build status codecov

This project allows users to read text content from PDF files.

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

Installation

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

https://www.nuget.org/packages/PdfPig/

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

API Changes

  • 0.0.3 - Changes to position data for Letter. Letter has a Location, Width and GlyphRectangle property. Consult the Wiki for details of the new API. Adds PdfDocument.Structure property allowing access to raw data.

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

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

Since this is alpha software the consumer should wrap all access in a try catch block since it is extremely likely to throw exceptions. As a fallback you can try running PDFBox using IKVM or using PDFsharp or by a native library wrapper using docnet.

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;

// 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 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 and is not reliable or well-tested:

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

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;

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.

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.

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 net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (35)

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.

Tabula

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

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

The package contains all the core logic of Kernel Memory, to index and query any data and documents, using LLM and natural language, tracking sources and showing citations.

GitHub repositories (12)

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

Repository Stars
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
The AI Agent Framework in .NET
microsoft/kernel-memory
Index and query any data using LLM and natural language, tracking sources and showing citations.
paillave/Etl.Net
Mass processing data with a complete ETL for .net developers
Version Downloads Last updated
0.1.9-alpha-20240419-1ef2e 796 4/19/2024
0.1.9-alpha-20240413-0f707 864 4/13/2024
0.1.9-alpha-20240406-2d6cb 1,865 4/6/2024
0.1.9-alpha-20240402-f6292 1,013 4/2/2024
0.1.9-alpha-20240324-e7896 2,418 3/24/2024
0.1.9-alpha-20240318-69e2b 2,209 3/18/2024
0.1.9-alpha-20240312-845e3 2,657 3/12/2024
0.1.9-alpha-20240307-ac027 1,691 3/7/2024
0.1.9-alpha-20240219-c2536 8,930 2/19/2024
0.1.9-alpha-20240217-f4e75 327 2/17/2024
0.1.9-alpha-20240216-f78b1 273 2/16/2024
0.1.9-alpha-20240215-3bdc9 1,571 2/15/2024
0.1.9-alpha-20240208-19734 2,702 2/8/2024
0.1.9-alpha-20240207-23445 958 2/7/2024
0.1.9-alpha-20240128-f886e 4,401 1/28/2024
0.1.9-alpha-20240121-04fc8 3,431 1/21/2024
0.1.9-alpha-20240117-096eb 2,995 1/17/2024
0.1.9-alpha-20240116-4e63e 776 1/16/2024
0.1.9-alpha-20240115-0da7b 742 1/15/2024
0.1.9-alpha-20240114-5953c 390 1/14/2024
0.1.9-alpha-20240112-83519 1,605 1/12/2024
0.1.9-alpha-20240111-88a14 633 1/11/2024
0.1.9-alpha-20240109-8cfaa 5,515 1/9/2024
0.1.9-alpha-20240108-18144 539 1/8/2024
0.1.9-alpha-20231119-4537e 13,838 11/19/2023
0.1.9-alpha-20231113-1bc0e 5,049 11/13/2023
0.1.9-alpha-20231029-17d50 4,391 10/29/2023
0.1.9-alpha-20231026-63096 3,926 10/26/2023
0.1.9-alpha-20231023-ba865 1,256 10/23/2023
0.1.9-alpha-20231019-c6e2d 1,964 10/19/2023
0.1.9-alpha-20230930-06ac8 6,423 9/30/2023
0.1.9-alpha-20230914-d59d2 4,479 9/14/2023
0.1.9-alpha-20230827-ee756 11,496 8/27/2023
0.1.9-alpha-20230806-4a480 7,363 8/6/2023
0.1.8 1,152,530 6/5/2023
0.1.8-alpha-20230605-7fe5f 936 6/5/2023
0.1.8-alpha-20230529-6daa2 6,450 5/29/2023
0.1.8-alpha-20230528-5126d 898 5/28/2023
0.1.8-alpha-20230524-20d3c 2,673 5/24/2023
0.1.8-alpha-20230523-11df5 910 5/23/2023
0.1.8-alpha-20230522-c3dd6 1,288 5/22/2023
0.1.8-alpha-20230423-3898f 35,715 4/23/2023
0.1.8-alpha-20230420-147b8 1,095 4/20/2023
0.1.8-alpha-20230419-2d72d 1,213 4/19/2023
0.1.8-alpha-20230417-cdc3d 1,311 4/17/2023
0.1.8-alpha-20230415-9eb79 1,048 4/15/2023
0.1.8-alpha-20230414-42e41 926 4/14/2023
0.1.8-alpha-20230413-46a04 1,029 4/13/2023
0.1.8-alpha-20230412-db058 1,665 4/12/2023
0.1.8-alpha-20230411-0e39b 1,031 4/11/2023
0.1.8-alpha-20230403-2e062 7,163 4/3/2023
0.1.8-alpha-20230331-bd4ee 18,777 3/31/2023
0.1.8-alpha-20230327-2daba 5,152 3/27/2023
0.1.8-alpha-20230326-58b33 1,052 3/26/2023
0.1.8-alpha-20230324-a3a9d 1,203 3/24/2023
0.1.8-alpha-20230323-a4861 1,073 3/23/2023
0.1.8-alpha-20230320-c024e 1,445 3/20/2023
0.1.8-alpha-20230318-a5c91 1,036 3/18/2023
0.1.8-alpha-20230219-999f9 2,555 2/19/2023
0.1.8-alpha-20230117-88aad 4,121 1/17/2023
0.1.8-alpha-20230109-65bc7 1,414 1/9/2023
0.1.7 610,412 12/13/2022
0.1.7-alpha-20221212-c8874 72,382 12/12/2022
0.1.7-alpha-20221210-2aed9 957 12/10/2022
0.1.7-alpha-20220814-2f9a9 5,892 8/14/2022
0.1.7-alpha-20220703-545d1 2,347 7/3/2022
0.1.7-alpha-20220622-fc71a 1,129 6/22/2022
0.1.7-alpha-20220618-f2188 960 6/18/2022
0.1.7-alpha-20220525-559f3 3,849 5/25/2022
0.1.7-alpha-20220511-ddab5 2,275 5/11/2022
0.1.7-alpha-20220503-4e490 1,792 5/3/2022
0.1.7-alpha-20220426-03692 1,112 4/26/2022
0.1.6 977,317 4/25/2022
0.1.6-alpha-20220425-2576c 1,076 4/25/2022
0.1.6-alpha-20220423-801a3 1,029 4/23/2022
0.1.6-alpha-20220415-cbd02 1,554 4/15/2022
0.1.6-alpha-20220411-09a62 1,163 4/11/2022
0.1.6-alpha-20220405-c2ecb 1,718 4/5/2022
0.1.6-alpha-20220404-6b085 1,025 4/4/2022
0.1.6-alpha-20220315-9c83e 3,663 3/15/2022
0.1.6-alpha-20220220-b0a5f 3,152 2/20/2022
0.1.6-alpha-20220116-e54cd 2,098 1/16/2022
0.1.6-alpha-20220113-5b66e 1,000 1/13/2022
0.1.6-alpha-20220112-b89c8 1,059 1/12/2022
0.1.6-alpha-20220111-41bfa 1,422 1/11/2022
0.1.5 879,375 9/17/2021
0.1.5-alpha002 4,784 5/9/2021
0.1.5-alpha001 24,202 2/28/2021
0.1.5-alpha-20211231-a57e5 2,274 12/31/2021
0.1.5-alpha-20211026-55244 1,013 10/26/2021
0.1.5-alpha-20210929-615e8 1,095 9/29/2021
0.1.5-alpha-20210918-4c36f 1,070 9/18/2021
0.1.5-alpha-20210828-e8f91 1,072 8/28/2021
0.1.5-alpha-20210827-e8f91 1,070 8/27/2021
0.1.5-alpha-20210817-b1f88 1,106 8/17/2021
0.1.4 560,596 11/29/2020
0.1.3 45,232 11/15/2020
0.1.3-alpha001 2,595 9/4/2020
0.1.2 222,031 7/4/2020
0.1.2-alpha003 1,292 6/20/2020
0.1.2-alpha002 3,558 5/10/2020
0.1.2-alpha001 1,324 4/25/2020
0.1.1 123,839 3/18/2020
0.1.1-alpha001 1,309 3/15/2020
0.1.0 196,323 1/13/2020
0.1.0-beta002 1,211 1/8/2020
0.1.0-beta001 1,215 1/6/2020
0.0.11 2,052 12/17/2019
0.0.10 1,664 12/9/2019
0.0.9 108,605 8/13/2019
0.0.7 1,637 8/3/2019
0.0.6 2,662 5/19/2019
0.0.5 11,786 12/30/2018
0.0.3 1,508 11/27/2018
0.0.1 9,527 2/26/2018
0.0.1-alpha-002 1,540 1/21/2018
0.0.1-alpha-001 1,530 1/10/2018