Html To Pdf C#

Introduction:

HTML (Hypertext Markup Language) is commonly utilized in the development of websites. Conversely, PDF (Portable Document Format) is a prevalent format for creating easily shareable and printable documents. The conversion of HTML to PDF is a frequent necessity in various applications, and C# stands out as a favored programming language for executing this transformation.

In this tutorial, we will explore the steps involved in converting HTML to PDF using C#.

Introduction to HTML to PDF Conversion:

Converting HTML to PDF involves transforming an HTML file into a PDF file. HTML files are commonly utilized to generate web pages for online viewing, while PDFs are preferred for producing easily shareable and printable documents. This conversion is essential in scenarios where a printable document output is necessary.

Benefits of HTML to PDF Conversion:

One of the advantages of converting HTML to PDF is maintaining consistency in the document's formatting.

Converting HTML to PDF guarantees consistent formatting and appearance on various platforms and devices.

  • Ease of printing:

PDF files are formatted for printing purposes; therefore, converting HTML to PDF guarantees that the resulting document is tailored for efficient printing.

  • Accessibility:

PDF files can be opened by individuals without internet connectivity or a web browser.

  • Safety:

PDF files have the capability to be safeguarded with passwords, enhancing their security compared to HTML files.

  • One advantage is their portability:

PDF files can be effortlessly shared and distributed among various platforms and devices.

Popular HTML to PDF Conversion Libraries:

There are numerous C# libraries accessible for converting HTML to PDF. A few of the well-known options are:

  • iTextSharp:

iTextSharp stands out as a well-known PDF toolkit for C# developers, offering capabilities to generate, modify, and transform PDF files.

Select.Pdf is a conversion tool that transforms HTML into PDF documents, created for seamless integration into current software systems.

  • PDFSharp:

PDFSharp is a freely available PDF library for C# programming language, enabling developers to generate, modify, and transform PDF files.

  • EVO HTML to PDF Converter:

EVO HTML to PDF Converter is a library designed for converting HTML to PDF, offering support for various HTML tags and CSS styles.

Converting HTML to PDF Using C#:

In this part, we are going to explore the procedure of transforming HTML into PDF format employing C# along with the iTextSharp framework.

Step 1: Install the iTextSharp Library

The initial stage in transforming HTML to PDF with C# involves setting up the iTextSharp library. This process is accomplished through the NuGet package manager within Visual Studio. To set up iTextSharp, access the NuGet package manager, search for "iTextSharp", choose the iTextSharp package, and then proceed with the "Install" option.

Step 2: Create a New C# Project

After installing the iTextSharp library, initiate a fresh C# project within Visual Studio. Begin by navigating to "File > New > Project". Opt for the "Console App (.NET Framework)" as the project template, then specify a title and directory for the project.

Step 3: Add a Reference to iTextSharp

The subsequent action involves including a link to the iTextSharp library in the project. To achieve this, you should right-click on the "References" directory in the Solution Explorer and opt for the "Add Reference" option. Within the "Reference Manager" dialog, choose "Browse" and browse to locate the necessary file.

Once you locate the iTextSharp library, simply choose it and then select the "Add" option. By doing this, a reference to the iTextSharp library will be included in your project.

Step 4: Write the Code to Convert HTML to PDF

Now that you have integrated the iTextSharp library into your project by adding a reference to it, you are ready to commence coding for the conversion of HTML to PDF.

Initially, you must instantiate the Document class, which is available in the iTextSharp library. This class is responsible for representing the PDF document that you are going to generate.

C# Code:

Example

Document document = new Document();

Next, you should instantiate the PdfWriter class, which is also available in the iTextSharp library. This particular class is in charge of saving the PDF document to either a file or a stream.

C# Code:

Example

PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create));

In this instance, we are generating a PDF file named "output.pdf" within the present working directory.

Next, you are required to access the document by invoking the Open function.

C# Code:

Example

document.Open();

Now, you have the option to instantiate the HTMLWorker class, which is available within the iTextSharp library. This particular class plays a crucial role in parsing HTML content and transforming it into a PDF file.

C# Code:

Example

HTMLWorker htmlWorker = new HTMLWorker(document);

Next, you must instantiate a StringReader class object, which is available within the .NET framework. This particular class is designed for the purpose of parsing through the HTML document that is intended for conversion into a PDF file.

C# Code:

Example

StringReader stringReader = new StringReader("<html><body><h1>Hello World!</h1></body></html>");

In this instance, we are employing a basic HTML file that includes only one header.

Now, you have the option to invoke the Parse function of the HTMLWorker class to analyze the HTML document and transform it into a PDF file.

C# Code:

Example

htmlWorker.Parse(stringReader);

Finally, it is essential to conclude the document by invoking the Close method.

C# Code:

Example

document.Close();

Run the code

Now that you've completed the HTML to PDF conversion code, you can execute it by pressing the "Start" button within Visual Studio. This action will compile the code, run it, and generate a PDF file named "output.pdf" in the present directory.

Conclusion:

In this guide, we have explored the procedure of transforming HTML into PDF using C#. We have outlined the advantages of converting HTML to PDF, well-known libraries for HTML to PDF conversion, and the step-by-step process of converting HTML to PDF with C# and the iTextSharp library. The conversion of HTML to PDF is a prevalent need in numerous applications, and C# offers a robust and user-friendly environment for executing this conversion task. Armed with the insights from this guide, you should now have the capability to seamlessly convert HTML to PDF within your applications.

Input Required

This code uses input(). Please provide values below: