PdfPig 0.0.1-alpha-002
See the version list below for details.
dotnet add package PdfPig --version 0.0.1-alpha-002
NuGet\Install-Package PdfPig -Version 0.0.1-alpha-002
<PackageReference Include="PdfPig" Version="0.0.1-alpha-002" />
paket add PdfPig --version 0.0.1-alpha-002
#r "nuget: PdfPig, 0.0.1-alpha-002"
// Install PdfPig as a Cake Addin #addin nuget:?package=PdfPig&version=0.0.1-alpha-002&prerelease // Install PdfPig as a Cake Tool #tool nuget:?package=PdfPig&version=0.0.1-alpha-002&prerelease
This project allows users to read text content from PDF files.
This project is based on PDFBox.
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).
You can pass a stream to PdfDocument.Open
which will avoid loading the entire document into memory but may be slower for reading.
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.
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...
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;
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
.
Letter position is measured in PDF coordinates where the origin is the lower left corner of the page. Therefore an higher Y value means closer to the top of the page.
At this stage letter position is experimental and will change in future versions! Do not rely on letter positions remaining constant between different versions of this package.
Issues
Please do file an issue if you encounter a bug.
Status
Why is class or property X internal? With the exception of letter.Position
internal properties and classes are not stable enough for the end user yet. If you want to access them feel free to use reflection but be aware they may change or disappear between versions.
The initial version of this package aims only to support reading text content from unencrypted PDF files. Due to the legal and dependency consequences of decrypting, handling encrypted documents is not in scope.
An encrypted document will throw a NotSupportedException
.
We plan to eventually support writing PDFs as well as reading images, form objects and graphics from the PDF however these are future enhancements which do not feature in the first version.
Additionally most testing has taken place with Latin character sets. Due to the more complex way the PDF specification handles CJK (Chinese, Japanese and Korean) character sets these will probably not be handled correctly for now.
Please raise an issue (or preferably a pull request) if you're trying to read these documents however we may not get to it for a while depending on the volume of bugs.
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 | 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. |
-
.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 (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.
|