![]() | Welcome to the Pdfium.NET SDK |
Pdfium.NET SDK it's a class library based on the PDFium project for viewing, navigating, editing and extracting texts from PDF files in your .NET projects.
Pdfium.Net SDK is available for .Net Framework 2.0 - 4.8, .Net 5.0 - .Net 7.0, .Net Standard, Core and Universal Windows Platform (UWP) on 32- and 64-bit operating systems.
SDK has been tested with Windows XP, Vista, 7, 8, 8.1, 10 and 11, MacOS on intel and M1/M2 processors, and is fully compatible with all of them. The native pdfium.dll and libpdfium.dylib libraries included to this project are supplied in both 32-bit and 64-bit versions, so your .NET application can be "Any CPU".
The easiest way to configure your project is to install the NuGet package. The easiest way to configure your project is to install the NuGet package. Just type in the PackageManager Console:
PM> install-package pdfium.net.sdk
Here are detailed instructions.
Alternatively, download the zip package and follow these instructions.
Source code for WinForms and Wpf controls can be loaded from github: https://github.com/Patagames/
![]() |
---|
You must call Initialize function at some point before using any PDF processing functions.
|
The following code example demonstrates how to load a PDF document into the PdfViewer control, select all text on some page and extract the selected text.
This example requires that you have added the PdfViewer control to a Form and called the method created in the example from the constructor or from another method of the form. The example also requires a pdf document named Test001.pdf to be located in the root directory of the drive C.
public void LoadDocument() { //Initialize the SDK library //You have to call this function before you can call any PDF processing functions. PdfCommon.Initialize(); //Open and load a PDF document from a file. pdfViewer1.LoadDocument(@"c:\test001.pdf"); //Selects all text contained in a first page. pdfViewer1.SelectText(0, 0, 1, 0); //Gets selected text from PdfViewer control string selectedText = pdfViewer1.SelectedText; }
If you use WPF then you should place PdfViewer control on a window like shown below
<Window xmlns:Wpf="clr-namespace:Patagames.Pdf.Net.Controls.Wpf;assembly=Patagames.Pdf.Wpf" xmlns:Toolbars="clr-namespace:Patagames.Pdf.Net.Controls.Wpf.ToolBars;assembly=Patagames.Pdf.Wpf" x:Class="ExamplesWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="mainWindow" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <ToolBarTray Grid.Row="0"> <Toolbars:PdfToolBarMain Window="{Binding ElementName= mainWindow}" PdfViewer="{Binding ElementName=pdfViewer1}" Name="toolbarMain1"></Toolbars:PdfToolBarMain> <Toolbars:PdfToolBarClipboard PdfViewer="{Binding ElementName=pdfViewer1}" Name="toolbarClipboard1"></Toolbars:PdfToolBarClipboard> </ToolBarTray> <ScrollViewer Grid.Row="1" CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <Wpf:PdfViewer Name="pdfViewer1" /> </ScrollViewer> </Grid> </Window>
You can work with pdf documents without using any GUI elements.
The following code example demonstrates how to extract all the text from any pdf page without using the PdfViewer control.
This example requires a pdf document named Test001.pdf to be located in the root directory of the drive C.
public void ExtractTextExample() { //Initialize the SDK library //You have to call this function before you can call any PDF processing functions. PdfCommon.Initialize(); //Open and load a PDF document from a file. var doc = PdfDocument.Load(@"c:\test001.pdf"); //Gets second page from document; var page = doc.Pages[1]; //Gets number of characters in a page or -1 for error. //Generated characters, like additional space characters, new line characters, are also counted. int totalCharCount = page.Text.CountChars; //Extract text string from the page string text = page.Text.GetText(0, totalCharCount); }
You can render a pdf page as an image to use it in graphical mode.
public void RenderPage() { //Initialize the SDK library //You have to call this function before you can call any PDF processing functions. PdfCommon.Initialize(); //Open and load a PDF document from a file. using (var doc = PdfDocument.Load(@"c:\test001.pdf")) { int i = 0; //Iterate all pages; foreach (var page in doc.Pages) { //Gets page width and height measured in points. One point is 1/72 inch (around 0.3528 mm) int width = (int)page.Width; int height = (int)page.Height; //Create a bitmap using (var bmp = new PdfBitmap(width, height, true)) { //Fill background bmp.FillRect(0, 0, width, height, FS_COLOR.White); //Render contents in a page to a drawing surface specified by a coordinate pair, a width, and a height. page.Render(bmp, 0, 0, width, height, Patagames.Pdf.Enums.PageRotate.Normal, Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT); //Get .Net image and save it into file bmp.GetImage().Save(string.Format(@"c:\test001_pdf_page_{0}.png", i++), ImageFormat.Png); } } } }
The following code example demonstrates how to create a new PDF document, import some pages from the existing PDF document and save the created document to a file.
This example requires a pdf document named Test001.pdf to be located in the root directory of the drive C.
The example also requires you to have write permissions for the root directory of the drive C.
public void ImportPages() { //Initialize the SDK library //You have to call this function before you can call any PDF processing functions. PdfCommon.Initialize(); //Open and load a PDF document from a file. var docSource = PdfDocument.Load(@"c:\test001.pdf"); //Create a new PDF document. var doc = PdfDocument.CreateNew(); //Import some pages to a PDF document. doc.Pages.ImportPages(docSource, "1,5-7", 0); //Saves a copy of PDF document to the specified file name doc.Save(@"c:\test002.pdf", Patagames.Pdf.Enums.SaveFlags.Incremental); }
And how to get access to acro forms
public void GetAccessToAcroForms() { var forms = new PdfForms(); var doc = PdfDocument.Load(@"c:\test.pdf", forms); //doc.FormFill is equal to forms and can be used to get access to acro forms as well; int i = 0; foreach (var field in forms.InterForm.Fields) { if (field.FieldType == Patagames.Pdf.Enums.FormFieldTypes.FPDF_FORMFIELD_TEXTFIELD) { field.Value = "This is a field #" + (++i); } } } //or if you are using PdfViewer public void GetAccessToAcroFormsFromViewer() { var doc = pdfViewer1.Document; var interactiveForms = doc.FormFill.InterForm; int i = 0; foreach (var field in interactiveForms.Fields) { if (field.FieldType == Patagames.Pdf.Enums.FormFieldTypes.FPDF_FORMFIELD_TEXTFIELD) { field.Value = "This is a field #" + (++i); } } }
More samples can be found at our forum here