Click or drag to resize

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.

Compatibility

Pdfium.Net SDK is available for .Net Framework 2.0 - 4.7 on 32- and 64-bit operating systems.

SDK has been tested with Windows XP, Vista, 7, 8, 8.1 and 10, and is fully compatible with all of them. The native PDFium.dll library included to this project is supplied in both 32-bit and 64-bit versions, so your .NET application can be "Any CPU".

Getting Started

Download the installation package, unpack it and copy the following files into your project directory

netXX\
    Patagames.Pdf.dll
    Patagames.Pdf.xml
    Patagames.Pdf.WinForms.dll
    Patagames.Pdf.WinForms.xml
    Patagames.Pdf.Wpf.dll
    Patagames.Pdf.Wpf.xml
x64\
    pdfium.dll
x86\
    pdfium.dll

Pdfium.NET SDK provides a number of components to work with PDF files:

  • Patagames.Pdf.dll contains the class library used to load, manipulate and render PDF documents;
  • Patagames.Pdf.WinForms.dll contains a WinForms controls that can render a PdfDocument (Follow the steps on this page to add PdfViewer control into Toolbox);
  • Patagames.Pdf.Wpf.dll contains a Wpf controls that can render a PdfDocument;
  • Pdfium.dll it's a native pdfium engine;
Note Note

Source code for WinForms and Wpf controls can be loaded from github: https://github.com/Patagames/

  1. To use the library, you must first add a reference to Patagames.Pdf.dll and/or Patagames.Pdf.WinForms.dll or Patagames.Pdf.Wpf.dll files into your project.

    Reference
  2. After you've added this reference, you need to add two files to your project:

    • x86\pdfium.dll is the 32-bit version of the Pdfium library;
    • x64\pdfium.dll is the 64-bit version of the Pdfium library;
    native
    Note Note

    You have two options. If your application is 32-bit only or 64-bit only, you can remove the DLL that won't be used. You can leave this file in the x86 or x64 directory, or move it to the root of your project. The library will find the DLL in both cases.

  3. When building your project, the pdfium.dll library(s) must be placed next to your application, either in the root or the x86 or x64 sub directory. The easiest way to accomplish this is by changing the properties of that file, changing the Copy to Output Directory setting to Copy always.

    properies
  4. That's all. Your project is ready to use Pdfium.Net SDK

    Important note Important

    You must call PdfCommon.Initialize function at some point before using any PDF processing functions.
    If you have problems with initialization (if it says something like "Unable to load DLL 'pdfium.dll'"), try to specify the full path to Pdfium.dll through the specificPath parameter. Read here for more details.

    Tip Tip

    You can find VIDEO instructions on how to configure your project here.

Examples

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.

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

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

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.

C#
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, 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.Image.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.

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

C#
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

See Also