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

Create a PDF Document on the Fly

Generate PDFs From Multiple Images

Printing PDF Files With C#

Extract Text From PDF in C#

Extract Text Coordinates from Pdf With C#

Extracting Images from Pdf File With .Net C#

Search for a Text in a PDF File

Search for a Text Asynchronously

Split PDF in C#

Merge PDFs With C#

Render a PDF to Images

Filling Editable PDF Fields and Extracting Data From Them

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. doc.Pages.InsertPageAt(doc.Pages.Count, 8.27f * 72, 11.69f * 72); var page = doc.Pages[doc.Pages.Count - 1]; // Step 3: Create fonts used for text objects // Return value: PdfFont class PdfFont calibryBold = PdfFont.CreateFont(doc, "CalibriBold", true, 0, 0, 0, FontCharSet.ANSI_CHARSET, false); // Step 4: Add graphics and text contents to the page // Insert image from file using standart System.Drawing.Bitmap class using (Bitmap logo = Bitmap.FromFile(@"d:\Sample\logo_square.png") as Bitmap) { PdfBitmap bitmap = new PdfBitmap(logo.Width, logo.Height, true); using (var g = Graphics.FromImage(bitmap.Image)) { g.DrawImage(logo, 0, 0, logo.Width, logo.Height); } PdfImageObject imageObject = PdfImageObject.Create(doc, bitmap, 0, 0); imageObject.Matrix = new FS_MATRIX(logo.Width, 0, 0, logo.Height * 72, 0, 0); page.PageObjects.Add(imageObject); } // Insert text objects at 7.69"; 11.02" and font size = 12 PdfTextObject textObject = PdfTextObject.Create("Sample text", 7.69f * 72, 11.02f * 72, calibryBold, 12); textObject.FillColor = Color.Black; page.PageObjects.Add(textObject); // Step 5: Generate page content and save pdf file // argument: PDF file name page.GenerateContent(); doc.Save(@"d:\Sample\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() { int pageIndex = 0; //Initialize C# PDF Library PdfCommon.Initialize(); //Create a PDF document using (var doc = PdfDocument.CreateNew()) { //Read images var files = System.IO.Directory.GetFiles(@"SourceImages", "*.*", System.IO.SearchOption.AllDirectories); foreach (var file in files) { // Creates an System.Drawing.Image from the specified file. using (var image = Bitmap.FromFile(file, true) as Bitmap) { //Create empty PdfBitmap using (PdfBitmap pdfBitmap = new PdfBitmap(image.Width, image.Height, true)) { using (var g = Graphics.FromImage(pdfBitmap.Image)) { //Draw image to PdfBitmap g.DrawImage(image, 0, 0, image.Width, image.Height); } //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, image.HorizontalResolution, image.VerticalResolution); //Add empty page to PDF document doc.Pages.InsertPageAt(pageIndex, size); //Insert image to newly created page doc.Pages[pageIndex].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 doc.Pages[pageIndex].GenerateContent(); pageIndex++; } } } // Save PDF document as "saved.pdf" in no incremental mode doc.Save(@"saved.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 very and depends on scanning resolution /// </summary> private static SizeF CalculateSize(int width, int height, float dpiX, float dpiY) { return new 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.

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.

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); } } }