PDF API C# (Code Example Tutorial)

1. Introduction

To make it easier to share documents with text and graphics, Adobe created the Portable Document Format (PDF). However, you need a different program to view a PDF file. The PDF document is essential in today's society, with many different types of companies using it for operations such as generating documents and invoices. Developers also rely on the PDF format to create documents that meet client requirements. Fortunately, creating PDFs has become easier than ever, thanks to libraries that simplify the process. When choosing a library for your project, you should consider aspects such as build, read, and conversion capabilities to find the best one on the market.

2. IronPDF Features

IronPDF is a powerful PDF converter and API library for PDF processing that enables developers to easily generate, read, and manipulate PDF documents. The IronPDF .NET library uses the Chrome engine to transform HTML into PDF format. The library supports a wide range of web components, including Xamarin, Blazor, Unity, and HoloLens apps, Windows forms, HTML, ASPX, Razor HTML, NET Core, ASP.NET, Windows Forms, and WPF. Microsoft.NET and .NET Core apps are also supported, both for ASP.NET web applications and traditional Windows applications. With IronPDF, you can create visually appealing PDFs using HTML5, JavaScript, CSS, and pictures, complete with a title and footer. The API library also includes a powerful HTML-to-PDF translator that works seamlessly with PDF and an independent PDF conversion engine that is not reliant on any outside sources.

  • The IronPDF library supports various sources for PDF generation, including HTML, HTML5, ASPX, Razor/MVC View, and image files.
  • The library offers tools to create interactive PDF documents, fill out and submit interactive forms, merge multiple PDF files into one, divide PDF files, extract text and images, search text, and rasterize PDF pages to images.
  • The library allows the use of user agents, proxies, cookies, HTTP headers, and form variables for authentication behind HTML login forms and permits the use of a link as the foundation of a PDF document.
  • IronPDF provides the ability to access secured PDF documents by providing user names and passwords.
  • With the IronPDF API, pre-existing PDF files can be read and edited.
  • The library can extract pictures from PDF files and add text, graphics, bookmarks, watermarks, headers, and footers to PDF documents.
  • IronPDF enables users to divide and combine pages in a new or existing PDF document, and it can create PDF objects from text without using Acrobat Reader.
  • CSS files can be transformed into PDF files, and CSS media files can be converted into PDF documents.
  • IronPDF enables users to fill out existing PDF forms and add new ones.

3. Create PDF Documents from URLs

With the assistance of the IronPDF API library, we can quickly generate PDF files by creating an HTML file from a URL and converting it to a PDF file using the built-in Chrome browser in IronPDF.

The methods listed below make it simple to create PDF documents with just a few lines of code:

IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer(); 
var Pdf = Renderer.RenderUrlAsPdf("https://www.google.co.in/"); 
Pdf.SaveAs("result.pdf");
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer(); 
var Pdf = Renderer.RenderUrlAsPdf("https://www.google.co.in/"); 
Pdf.SaveAs("result.pdf");
Dim Renderer As New IronPdf.ChromePdfRenderer()
Dim Pdf = Renderer.RenderUrlAsPdf("https://www.google.co.in/")
Pdf.SaveAs("result.pdf")
VB   C#

According to the information above, we can use RenderUrlAsPdf to convert the link into a document by providing the link and the save location.

4. Create a PDF from HTML Strings

The IronPDF API library makes it easy to convert HTML strings into PDF files. Below is an example code for converting HTML strings into documents, and it allows the translation of any HTML tag into PDF files.

IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Hello world!!").SaveAs("result.pdf");  
IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Hello world!!").SaveAs("result.pdf");  
IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Hello world!!").SaveAs("result.pdf")
VB   C#

This example code demonstrates how to convert an HTML text using RenderHtmlAsPdf. We can provide as many HTML codes as we want to the method that converts HTML into a string. Once we have the string, we can save the document using the "save as" method and the whole process is completed in just a matter of seconds.

5. Reading PDF Documents

With the help of the IronPDF API library, it is possible to read and extract data from existing PDF documents. The code below shows an example of how to do this:

var pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf");
string AllText = pdfDocument.ExtractAllText();  
var pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf");
string AllText = pdfDocument.ExtractAllText();  
Dim pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf")
Dim AllText As String = pdfDocument.ExtractAllText()
VB   C#

In this code, the FromFile function is used to read the PDF from an existing file and convert it into a PdfDocument object. This object can be used to access the text and images present on the PDF pages. The ExtractAllText method can be used to create a string containing every word on the PDF pages.

For more information on how to use the IronPDF API library, refer to the code tutorials provided here.

6. Conclusion

The IronPDF library provides a free license for development, and different licenses are available for purchase for use in a production environment based on the developer's needs. The Lite package has a starting price of $499 and does not require any ongoing fees. The licenses offer redistribution options for SaaS and OEM and come with a permanent license, a 30-day money-back guarantee, and a year of product support and updates. They are suitable for development, staging, and production and require a one-time purchase. Moreover, IronPDF provides additional complimentary time-limited licenses. IronPDF also offers free licenses for redistribution coverage.

For more information on IronPDF's complete pricing and licensing details, please click here.