PDF File Format APIs

PDFsharp

 
 

Open Source .NET API for PDF Processing

Create, Manipulate, Convert & Process PDF Documents via Free C# .NET Library.

 

Open Source .NET library that can be used to create, render, merge, split, modify, print and extract text or meta-data of PDF files. The PDFsharp API supports creating PDF documents on the fly from any .NET language.  It also supports importing data from various sources via XML files or direct interfaces. It supports numerous options for page layout, text formatting, and document design.

PDFsharp provides graphical implementation based either on GDI+ or WPF. The API makes developer’s job easy by providing features for using one source code for drawing on a PDF page as well as in a window or on the printer. It supports several important features for processing PDF files such as modifying PDF, merge or split PDFs, XPS to PDF conversion, PDF rendering, PDF data extraction, Font embedding and subsetting, Unicode support and many more.

Getting Started with PDFsharp

PDFsharp is dual-licensed as AGPL/Commercial software. AGPL is a free/open-source software license.

It is highly recommended using NuGet to add PDFsharp to your project,

NuGet command

 Install-Package PdfSharp

With Visual Studio you can install the NuGet Package Manager to easily access NuGet packages. It works with VS 2012 Express as well as with the community editions of VS 2013 and VS 2015. In Visual Studio go to "Tools" => "Extensions and Updates..." to install the NuGet Package Manager if you do not have it yet. The NuGet Package Manager will download the package for you, install it, add a reference to your project.

You can also install it manually; download the latest release files directly from the GitHub repository.

Generate & Modify PDF Documents via Free .NET API

Software developers can use PDFsharp API to create a new PDF document inside their own .NET applications. Once the document is created you can add an empty page as well insert graphics or text with ease. It also facilitates developers to modify the existing document according to their needs and save it with the name of their choice.

Create PDF using C#

// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
// Create an empty page
PdfPage pdfPage = pdfDocument.AddPage();
// Get an XGraphics object for drawing
XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);
// Create a font
XFont xFont = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
xGraphics.DrawString("File Format Developer Guide", xFont, XBrushes.Black,
  new XRect(0, 0, pdfPage.Width, pdfPage.Height),
  XStringFormats.Center);
// Save the document...
pdfDocument.Save("fileformat.pdf");

Create PDF Annotations

Annotations allow users to add custom content on PDF pages. PDF applications normally allow creating & modifying various types of annotations, such as text, lines, notes or shapes, etc. PDFsharp enables programmers to create various types of PDF annotations inside their own applications. The library supports the creation of text annotations, link and rubber stamp annotations.

Add Annotation in PDF Free using C#

// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
// Create an empty page
PdfPage pdfPage = pdfDocument.AddPage();
// Get an XGraphics object for drawing
XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);
// Create a font
XFont xFont = new XFont("Verdana", 12, XFontStyle.Regular);
// Create a PDF text annotation
PdfTextAnnotation pdfTextAnnotation = new PdfTextAnnotation();
pdfTextAnnotation.Title = "File Format Developer Guide";
pdfTextAnnotation.Subject = "Text Annotation";
pdfTextAnnotation.Contents = "This is a text annotation created using PDF Sharp";
pdfTextAnnotation.Icon = PdfTextAnnotationIcon.Note;

xGraphics.DrawString("The first text annotation", xFont, XBrushes.Black, 30, 50, XStringFormats.Default);
XRect rect = xGraphics.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(30, 30)));
pdfTextAnnotation.Rectangle = new PdfRectangle(rect);
// Add the annotation to the page
pdfPage.Annotations.Add(pdfTextAnnotation);
// Save the document
pdfDocument.Save("annotation.pdf");

Combine Multiple PDF Documents

Do you have numerous PDF documents that need to be combined into one big document? PDFsharp API provides you the functionality for combining multiple PDF files into a single one with just a few lines of code. Developers can easily create a new document from existing PDF files. This may be useful for visual comparison or several other important tasks.

Combine Two PDF Documents using C#

// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
// Open two existing documents
PdfDocument pdfDocument1 = PdfReader.Open("fileformat.pdf", PdfDocumentOpenMode.Import);
PdfDocument pdfDocument2 = PdfReader.Open("annotation.pdf", PdfDocumentOpenMode.Import);
// Add pages in the output document
foreach(var page in pdfDocument1.Pages)
{
pdfDocument.AddPage(page);
}
foreach(var page in pdfDocument2.Pages)
{
pdfDocument.AddPage(page);
}
// Save the output document
pdfDocument.Save("combine.pdf");