Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In this modern era of digital media, the Portable Document Format (PDF) stands out as one of the most universally used, standard document formats for transferring data without losing formatting. Its versatility, platform independence, and ability to preserve document formatting make it an ideal choice for sharing information.
Creating PDFs programmatically is a common necessity in both desktop and web applications. Programmatically creating PDFs allows developers to generate documents dynamically based on real-time data.
This is particularly valuable for applications that need to generate reports, invoices, or certificates on the fly. The PDF file format is created by Adobe, which offers the capability to create PDFs and you can explore its features at Adobe Create PDF.
In this article, we will explore the process of creating PDF documents programmatically in C# with two popular libraries iTextSharp and IronPDF.
iTextSharp, launched in December 2009, emerged as a revolutionary PDF tool, superseding its predecessor, iText 2. As the .NET version of iText 5, it paved the way for advanced programmable PDF functionalities. iTextSharp, the .NET version of iText 5, has long been a reliable source for creating a programmable PDF document object.
Here are some important key features of iTextsharp dll:
iTextSharp empowers developers to dynamically generate or manipulate PDF documents. This feature is invaluable for creating documents and reports based on data from XML files or databases.
The library facilitates the creation and filling out of interactive forms, enhancing user engagement and interactivity.
iTextSharp allows developers to exploit numerous interactive features, such as adding bookmarks, page numbers, watermarks, and more to existing PDF files. This flexibility is essential for tailoring PDFs to specific requirements.
Developers can leverage iTextSharp to create maps and books, expanding the application of the library beyond basic document generation.
iTextSharp supports various PDF standards, including PDF/A for archiving, PDF/UA for accessibility, ZUGFeRD for electronic invoicing, and PDF 2.0 (though not all functionalities are covered).
While iTextSharp has been a robust choice for PDF manipulation, iText 5/iTextSharp has been superseded by iText Core version 8, which brings forth numerous improvements. Developers are encouraged to consider transitioning existing projects to iText 8 to benefit from the latest releases.
Here are some key features of iText 7 version 8:
iText introduces enhancements in functionality, performance, and extensibility compared to its predecessor.
The latest version provides improved APIs for more intuitive and efficient development.
iText comes with updated documentation and support, ensuring a smoother development experience.
With the iText 7 version 8 Suite, developers can optimize invoicing, ensuring long-term archiving and addressing compliance issues through its powerful PDF SDK.
To create a PDF document with iTextSharp, follow these steps to install the library using the NuGet Package Manager:
Right-click on the project in the Solution Explorer and select "Manage NuGet Packages."
Type "iTextsharp" in the search bar and press Enter.
Now, let's explore a basic example of creating a PDF using iTextSharp:
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream("/myfiles/hello.pdf", FileMode.Create, FileAccess.Write)));
// Create a document object
Document doc = new Document(pdfDocument);
// Add a paragraph to the document
String line = "Hello! Welcome to iTextSharp. Created with latest iText core version 8.";
doc.Add(new Paragraph(line));
// Close the document
doc.Close();
// Print a message to the console
Console.WriteLine("PDF created successfully!");
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream("/myfiles/hello.pdf", FileMode.Create, FileAccess.Write)));
// Create a document object
Document doc = new Document(pdfDocument);
// Add a paragraph to the document
String line = "Hello! Welcome to iTextSharp. Created with latest iText core version 8.";
doc.Add(new Paragraph(line));
// Close the document
doc.Close();
// Print a message to the console
Console.WriteLine("PDF created successfully!");
Imports System
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
' Create a new PDF document
Private pdfDocument As New PdfDocument(New PdfWriter(New FileStream("/myfiles/hello.pdf", FileMode.Create, FileAccess.Write)))
' Create a document object
Private doc As New Document(pdfDocument)
' Add a paragraph to the document
Private line As String = "Hello! Welcome to iTextSharp. Created with latest iText core version 8."
doc.Add(New Paragraph(line))
' Close the document
doc.Close()
' Print a message to the console
Console.WriteLine("PDF created successfully!")
In this source code, we created a new PDF document by passing a PdfWriter class object. In it we pass a complete FileStream object specifying the PDF file name and FileMode to create the PDF document, we then pass this PdfDocument object to the Document class for PDF creation.
Finally, we added a string text as a new Paragraph with the text "Hello! Welcome to iTextSharp. Created with latest iText core version 8.", and closed the document to save memory. The created PDF document is saved as "hello.pdf" in the specific location within the 'myfiles' PDF folder.
IronPDF is a comprehensive C# library that empowers developers to interact with PDFs in a versatile and efficient manner. Whether you're creating PDFs from scratch, converting HTML and CSS to PDF, or adding advanced features like digital signatures and watermarks, IronPDF provides a rich set of tools for all your PDF-related needs.
IronPDF excels in converting HTML and CSS to high-quality PDF documents. This feature is particularly valuable for developers dealing with web content or dynamic HTML-based reports.
Developers can create PDFs from scratch, add content dynamically, and manipulate existing PDFs with ease. This flexibility allows for the generation of customized documents tailored to specific requirements.
IronPDF supports the inclusion of text, images, tables, and forms in your PDF document. This makes it a versatile tool for creating diverse types of content within PDFs.
Security is paramount, and IronPDF enables the addition of digital signatures to PDF documents. This feature is crucial for applications dealing with sensitive information or legal documents.
Enhance your PDFs with watermarks, annotations, and additional metadata. IronPDF provides the means to add visual elements and notes to your documents, improving communication and document clarity.
IronPDF supports compliance with PDF/A standards for archiving and PDF/UA standards for accessibility. This ensures that your documents meet industry standards for long-term archiving and accessibility requirements.
IronPDF is designed to work seamlessly across different platforms, providing a consistent experience for developers working in various environments.
The library comes with a user-friendly API that simplifies complex PDF operations. This allows developers to focus on application logic rather than solving complex PDF functionalities.
To get started with IronPDF, follow these steps to install the library using the NuGet Package Manager:
In the "NuGet Package Manager" window, select the "Browse" tab.
Now, let's look at the following code for creating a PDF from an HTML string using IronPDF:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Create a PDF from a HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
In the above source code, we created a new ChromePdfRenderer, rendered the HTML string content (in this case, a heading) as PDF, and saved the generated PDF document as "output.pdf." IronPDF converts HTML to a pixel-perfect PDF document.
To generate PDF with more control, such as by setting page size, adding image files, meta information, and other elements, please visit this documentation for more information.
Both IronPDF and iTextSharp, now formally known as iText, are formidable libraries for creating PDFs in C#, each with its strengths.
IronPDF stands out for its user-friendly interface and extensive documentation, making it an excellent choice for developers seeking simplicity. iTextSharp, on the other hand, is renowned for its robust feature set, especially in environments where advanced PDF manipulation is crucial.
Both iText and IronPDF are robust solutions for PDF generation and manipulation, but IronPDF tends to outperform iText in terms of speed and efficiency. This advantage is attributed to IronPDF's optimized architecture, leveraging native code for critical tasks and ensuring high performance.
Notably, IronPDF excels in HTML rendering, making the generation of PDFs from HTML faster compared to traditional methods employed by iText. Additionally, IronPDF tends to be more memory-efficient, particularly advantageous in environments with limited resources. Overall, IronPDF's streamlined architecture and proficiency in HTML rendering contribute to its slight edge in performance over iText.
For a full comparison between IronPDF and iTextSharp, please visit this detailed comparison between IronPDF and iTextSharp.
IronPDF provides a free trial license for more information. Download the library from the IronPDF download page.
9 .NET API products for your office documents