PdfPig 0.1.0-beta001
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
<PackageReference Include="PdfPig" Version="0.1.0-beta001" />
paket add PdfPig --version 0.1.0-beta001
#r "nuget: PdfPig, 0.1.0-beta001"
// Install PdfPig as a Cake Addin #addin nuget:?package=PdfPig&version=0.1.0-beta001&prerelease // Install PdfPig as a Cake Tool #tool nuget:?package=PdfPig&version=0.1.0-beta001&prerelease
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.
Hyperlinks (0.1.0)
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 | Versions 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. |
-
.NETFramework 4.5
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.5.1
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.5.2
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.6
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.6.1
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.6.2
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.7
- System.ValueTuple (>= 4.5.0)
-
.NETStandard 2.0
- No dependencies.
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 (16)
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.
|