The C# PDF Library

  • Create PDFs from scratch, or from a bunch of scanned images
  • Edit, merge, split and manipulate PDFs, extract text and images
  • Embed standalone Winforms or WPF PDF Viewer
  • Supports: .Net 2.0+, .Net 6, Standard, Core, Mono, Azure
  • And it also works on Windows XP and Mac OS
Download Pdfium.Net SDK Install with NuGet

Advanced PDF Library for total control over your PDF creation workflow

Pdfium.Net SDK is the leading .Net library for generating, manipulating and viewing files in the portable document format. We are offering a high level c# / VB.Net API for dynamic pdf creation on a WEB server or any other server system, and to implement «Save as PDF» feature in existing desktop or WEB applications.

Get started: C# code examples

How to Create a PDF Dynamically With C#

/// <summary>
/// Create PDF Document on The Fly in C# using Pdfium.Net SDK Library
/// </summary>
public void CreatePdf()
{
    // The PDF coordinate system origin is at the bottom left corner of the page. 
    // The X-axis is pointing to the right. The Y-axis is pointing in upward direction.
    // The sizes and coordinates in this method are given in the inches.

    // Step 1: Initialize PDF library and create empty document
    // Return value: PdfDocument main class
    PdfCommon.Initialize();
    var doc = PdfDocument.CreateNew();  // Create a PDF document

    // Step 2: Add new page
    // Arguments: page width: 8.27", page height: 11.69", Unit of measure: inches
    //  The PDF unit of measure is point. There are 72 points in one inch.
    var page = doc.Pages.InsertPageAt(doc.Pages.Count, 8.27f * 72, 11.69f * 72);

    // Step 3: Add graphics and text contents to the page
    // Insert image from file using standart System.Drawing.Bitmap class
    using (PdfBitmap logo = PdfBitmap.FromFile(@"e:\63\logo_square.png"))
    {
        PdfImageObject imageObject = PdfImageObject.Create(doc, logo, 0, 0);
        //image resolution is 300 DPI and location is 1.69 x 10.0 inches.
        imageObject.Matrix = new FS_MATRIX(logo.Width * 72 / 300, 0, 0, logo.Height * 72 / 300, 1.69 * 72, 10.0 * 72);
        page.PageObjects.Add(imageObject);
    }

    // Create fonts used for text objects
    PdfFont calibryBold = PdfFont.CreateFont(doc, "CalibriBold");
    // Insert text objects at 7.69"; 11.02" and font size is 25
    PdfTextObject textObject = PdfTextObject.Create("Sample text", 1.69f * 72, 11.02f * 72, calibryBold, 25);
    textObject.FillColor = FS_COLOR.Black;
    page.PageObjects.Add(textObject);

    // Step 5: Generate page content and save pdf file
    // argument: PDF file name
    page.GenerateContent();
    doc.Save(@"e:\63\sample_document.pdf", SaveFlags.NoIncremental);
}
 

Pdfium.Net SDK Library allows developers to create PDF documents easily in C#. This example shows that a PDF document can be dynamically created using page objects.

You can create multiple page objects and place them anywhere on the page. There are several types of page objects: paths, forms, images and text objects.

How to Generate PDF Programmatically from Set of Images With C#

/// <summary>
/// Generate PDF document From Multiple Images in C# using PDF Library
/// </summary>
public void GeneratePdf()
{
    //Initialize C# PDF Library
    PdfCommon.Initialize();
    //Create a PDF document
    using (var doc = PdfDocument.CreateNew())
    {
        //Read images
        var files = System.IO.Directory.GetFiles(@"c:\Images\", "*.*", 
                    System.IO.SearchOption.AllDirectories);
        foreach (var file in files)
        {
            //Create empty PdfBitmap
            using (PdfBitmap pdfBitmap = PdfBitmap.FromFile(file))
            {
                //Create Image object
                var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);
                //Calculate size of image in PDF points
                var size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height);
                //Add empty page to PDF document
                var page = doc.Pages.InsertPageAt(doc.Pages.Count, size);
                //Insert image to newly created page
                page.PageObjects.Add(imageObject);
                //set image matrix
                imageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);
                //Generate PDF page content to content stream
                page.GenerateContent();
            }
        }
        // Save  PDF document as "saved.pdf" in no incremental mode
        doc.Save(@"c:\test.pdf", SaveFlags.NoIncremental);
    }
}
/// <summary>
/// The function takes width and height of the bitmap in pixels as well as 
/// horizontal and vertical DPI and calculates the size of the PDF page. 
/// To understand the conversion you should know the following:
///     One inch contains exactly 72 PDF points;
///     DPI of the scanned image may vфry and depends on scanning resolution
/// <summary>
private FS_SIZEF CalculateSize(int width, int height, float dpiX=300, float dpiY=300)
{
    return new FS_SIZEF()
    {
        Width = width * 72 / dpiX,
        Height = height * 72 / dpiY
    };
}
 

This example shows how you can generate a PDF document from a bunch of scanned images using a simple C# code and PDF library.

How to Print PDF Files in C#

/// <summary>
/// Printing PDF Files in C# using PDF Library
/// </summary>
public void PrintPdf()
{
    var doc = PdfDocument.Load("c:\test.pdf");  // Read PDF file
    var printDoc = new PdfPrintDocument(doc);
    printDoc.Print();
}

The code above prints a PDF document to the default printer. Standard printing dialog with printing progress is also shown. If you want to suppress progress window please modify the code like shown below.

public void PrintPdf()
{
    var doc = PdfDocument.Load("c:\test.pdf");
    var printDoc = new PdfPrintDocument(doc);
    PrintController printController = new StandardPrintController();
    printDoc.PrintController = printController;
    printDoc.Print(); // C# Print PDF document
}

The PdfPrintDocument is derived from standard PrintDocument class, so that you can use .Net Framework's print dialog box (PrinterDialog) that configures a PrintDocument according to user input.

public void OnPrintClick()
{
	if (PdfViewer.Document.FormFill != null)
		PdfViewer.Document.FormFill.ForceToKillFocus();

	//create an instance of PrintDocument class
	var printDoc = new PdfPrintDocument(PdfViewer.Document); // create an instance of Print document class that is used for printing PDF document.

	//Create a standard print dialog box
	var dlg = new PrintDialog();
	dlg.AllowCurrentPage = true;
	dlg.AllowSomePages = true;
	dlg.UseEXDialog = true;
	//sets the PrintDocument used to obtain PrinterSettings.
	dlg.Document = printDoc;
	//show PrinterDialog and print pdf document
	if (dlg.ShowDialog() == DialogResult.OK)
		printDoc.Print();   // C# Print PDF
}
 
 

Read PDF File and Extract Text From it in C#

/// <summary>
/// Read PDF File and Extract Text From it in C#
/// </summary>public void ExtractText()
{
    //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")) // C# Read PDF File
    {
        foreach (var page in doc.Pages)
        {
            //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 from page to the string
            string text = page.Text.GetText(0, totalCharCount);

            page.Dispose();
        }
    }
}

Pdfium.Net SDK allows developers to easily extract the full text from almost any PDF file.

How to Search for a Text in a PDF File

/// <summary>
/// Search for a Text in a PDF File in C# With Pdfium.Net SDK Library
/// </summary>
public void Search()
{
    //Open PDF document
    using (var doc = PdfDocument.Load(@"d:\0\test_big.pdf")) // Read PDF document and enumerate pages
    {
	    //Enumerate pages
	    foreach(var page in doc.Pages)
	    {
		    var found = page.Text.Find("text for search", FindFlags.MatchWholeWord, 0);
		    if (found == null)
			    return; //nothing found
		    do
		    {
			    var textInfo = found.FoundText;
			    foreach(var rect in textInfo.Rects)
			    {
				    float x = rect.left;
				    float y = rect.top;
				    //...
			    }
		    } while (found.FindNext());

		    page.Dispose();
	    }
    }
}

This example shows how you can search for a text in PDF document using a simple C# code and the PDF library.

How to Search for a Text Asynchronously

/// <summary>
/// Search for a Text Asynchronously in C# using PDF Library
/// </summary>
public void SearchAsync()
{
    //Open PDF document
    var doc = PdfDocument.Load(@"c:\test_big.pdf"); // C# Read PDF File
    PdfSearch search = new PdfSearch(doc);
    search.FoundTextAdded += (s, e) =>
	    {
		    var textInfo = doc.Pages[e.FoundText.PageIndex].Text.GetTextInfo(e.FoundText.CharIndex, e.FoundText.CharsCount);
		    foreach (var rect in textInfo.Rects)
		    {
			    float x = rect.left;
			    float y = rect.top;
			    Console.WriteLine(string.Format("Found text: {0}, Page = {1}, x= {2}, y={3}", textInfo.Text, e.FoundText.PageIndex, x, y));
			    //...
		    }
	    };
    search.SearchCompleted += (s, e) =>
	    {
		    doc.Dispose();
	    };
    search.SearchProgressChanged += (s, e) =>
	    {
		    Console.WriteLine(string.Format("Progress: {0}%", e.ProgressPercentage));
	    };
    search.Start("document", FindFlags.MatchWholeWord);
    Console.ReadLine();
}
 

This example shows how you can use C# PDF Library for search for a text Asynchronously.

How to Extract Text Coordinates from PDF

/// <summary>
/// Extract Text Coordinates from Pdf in C# using PDF Library
/// </summary>
public void ExtractTextInfo()
{
    //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")) // C# Read PDF File
    {
        //Get second page from document
        using (var page = doc.Pages[1])
        {
            //Extract text information structure from the page
            // 10 - Index for the start characters
            // 25 - Number of characters to be extracted
            var textInfo = page.Text.GetTextInfo(10, 25);

            //Gets text from textInfo strtucture
            string text = textInfo.Text;

            //Gets a collection of rectangular areas bounding specified text.
            var rects = textInfo.Rects;
        }
    }
}

Pdfium.Net SDK also allows developers to easily extract the text coordinates from any PDF file.

How to Extract Images from PDF document

/// <summary>
/// Extracting Images from Pdf File With .Net C# and PDF Library
/// </summary>
private int _writedImageIndex = 0;

public void ExtractAllImages()
{
    //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")) // C# Read PDF File
    {
        //Enumerate all pages sequentially in a given document
        foreach (var page in doc.Pages)
        {
            //Extract and save images
            ExtractImagesFromPage(page);

            //dipose page object to unload it from memory
            page.Dispose();
        }
    }
}

private void ExtractImagesFromPage(PdfPage page)
{
    //Enumerate all objects on a page
    foreach (var obj in page.PageObjects)
    {
        var imageObject = obj as PdfImageObject;
        if (imageObject == null)
            continue; //if not an image object then nothing do

        //Save image to disk
        var path = string.Format(@"c:\\Images\\image_{0}.png", ++_writedImageIndex);
        imageObject.Bitmap.Image.Save(path, ImageFormat.Png);
    }
}
 

The example demonstrates extracting different format images from a pdf file, and saves them to disk.

How to split PDF document into small ones

/// <summary>
/// Split PDF in C# using PDF Library
/// </summary>
public void SplitDocument()
{
    //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 sourceDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read PDF File
    {
        //Create one PDF document for pages 1-5.
        using (var doc = PdfDocument.CreateNew())
        {
            //Import pages from source document
            doc.Pages.ImportPages(sourceDoc, "1-5", 0);
            //And save it to doc1.pdf
            doc.Save(@"c:\doc1.pdf", SaveFlags.Incremental);
        }

        //Create another PDF document for pages 5-10.
        using (var doc = PdfDocument.CreateNew())
        {
            //Also import pages
            doc.Pages.ImportPages(sourceDoc, "5-10", 0);
            //And save them too
            doc.Save(@"c:\doc2.pdf", SaveFlags.Incremental);
        }
    }
}
 

The following code example demonstrates how to use C# PDF Library to split a PDF document.

Merge Selected Pages from Multiple PDF Files into One in C#

/// <summary>
/// Merge PDFs in C# using PDF Library
/// </summary>
public void MergePdf()
{
    //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 in which will be merged other files
    using (var mainDoc = PdfDocument.Load(@"c:\test001.pdf")) // C# Read source PDF File #1
    {
        //Open one PDF document.
        using (var doc = PdfDocument.Load(@"c:\doc1.pdf")) //Read PDF File #2
        {
            //Import all pages from document
            mainDoc.Pages.ImportPages(
                doc,
                string.Format("1-{0}", doc.Pages.Count),
                mainDoc.Pages.Count
                );
        }

        //Open another PDF document.
        using (var doc = PdfDocument.Load(@"c:\doc2.pdf"))
        {
            //Import all pages from document
            mainDoc.Pages.ImportPages(
                doc,
                string.Format("1-{0}", doc.Pages.Count),
                mainDoc.Pages.Count
                );
        }

        mainDoc.Save(@"c:\ResultDocument.pdf", SaveFlags.NoIncremental);


    }
}
 

Using C# PDF Library, you can not only merge multiple PDF files into a single file, but also select specific pages from the source files and combine them in one PDF document.

The code above shows how it can be done using ImportPages operation.

Source Code10

Source Code11

How to Convert Each Page of PDF Document to Set of Images

/// <summary>
/// Render whole PDF document using C# PDF Library
/// </summary>
using (var doc = PdfDocument.Load(@"d:\0\test_big.pdf")) // C# Read PDF Document
{
    foreach (var page in doc.Pages)
    {
        int width = (int)(page.Width / 72.0 * 96);
        int height = (int)(page.Height / 72.0 * 96);
        using (var bitmap = new PdfBitmap(width, height, true))
        {
            bitmap.FillRect(0, 0, width, height, Color.White);
            page.Render(bitmap, 0, 0, width, height, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
            bitmap.Image.Save("...", ImageFormat.Png);
        }
    }
}

In this example we are create a bitmap of each page, so we calculate the required width and height of the bitmap in pixels converted from the dimensions of the PDF page in Points. Each Point is 1/72 of an inch, so we basically take the vertical or horizontal DPI of the image (96 in our example), multiply it to corresponding dimension and divide by 72.

Next we are create a new PdfBitmap using the dimensions we just computed. The last parameter of the constructor tells to use the true color mode.

Then we fill the entire bitmap with white and render the page to it. Thats all!

How to Programmatically Populate and Eextract Data from PDF Fields

/// <summary>
/// Filling Editable PDF Fields and Extracting Data From Them using .Net PDF Library
/// </summary>
private void btnTest_Click(object sender, RoutedEventArgs e)
{
	var forms = new PdfForms();
	var doc = PdfDocument.Load(@"c:\test.pdf", forms); // C# Read PDF Document
	//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);
		}
	}
}

This example code demonstrates how programmatically fill all editable forms in pdf document using .Net PDF Library.

 

Affordable Support from Developers to Developers

Patagames support service is what you might call «developers for developers».
When you license product, you receive more than a great library. To get the most from this one, you also receive 12 months of free technical support directly from our .Net development team.

I've tried your latest release and it works perfectly ― solves both my problems! Thanks for meeting my request ―it's much appreciated. We still use [other product] for some legacy projects, but we have dropped it for all new projects. Although the [other product] library itself is good, we found their support to be poor ― without doubt Pdfium.NET support has proven to be far better, both in speed and quality!
Again, many thanks for your first-rate service and excellent product. -Terry
The link is hidden due to ethical concerns.

We focus on simplicity so that you can focus on Your Apps

The primary design goal of Pdfium.Net SDK is to make things simple and intuitive to developers. The class library abstracts all the details of using the underlying pdfium library and provides an interface based on world objects and other intuitive classes.

Furthermore, Pdfium.Net SDK is well documented SDK! We provide a rich documentation which aims to give beginners and advanced users an overview of what is possible with the Pdfium.Net SDK.

 

Optimized for Enterprise and Cloud Services

One of our target is to provide developers with high quality tool for dynamic PDF creation on any server system like Microsoft Azure.

I have verified that your SDK does work when deployed to Azure (several products I tested did not ― you may want to advertise this). And I have made the purchase.
Thanks again! -Cyberg
https://forum.patagames.com/posts/m1105-Workability-in-various-environments#post1105

Embed 100% standalone C# PDF Viewer in Your Apps.

Customize the Look. Handle Events and Enjoy Dramatic Interactivity.

Bestow the ability to view pdf files to your .Net app! With multiple view modes, nuanced customization and complete support for acroforms pdf viewer fits into your application seamlessly and tailored to your design.

  • Fully Customizable UI
  • Smooth PDF-application interaction
  • Flexible text processing
  • Simple integration
  • Open Source
WPF PDF Viewer Winforms PDF Viewer
C# PDF Viewer
Refer to documentationfor more information about appearance of PdfViewer

Pdfium.Net SDK

Download

NuGet package is also available in the official repo at nuget.org

PM> Install-Package Pdfium.Net.SDK

Designed for

Microsoft .Net Framework 2.0+ Microsoft .Net Standard 2.0+ Microsoft Visual Studio NuGet Microsoft Azure