Javatpoint Logo
Javatpoint Logo

HTML to PDF C#

Introduction:

HTML (Hypertext Markup Language) is widely used for creating web pages. On the other hand, PDF (Portable Document Format) is a widely used document format that is used for creating documents that can be shared and printed easily. Converting HTML to PDF is a common requirement in many applications, and C# is a popular programming language that can be used to perform this conversion.

In this article, we will see the process of converting HTML to PDF using C#

Introduction to HTML to PDF Conversion:

HTML to PDF conversion refers to the process of converting an HTML document to a PDF document. HTML documents are typically used to create web pages that can be viewed in a web browser. PDF documents, on the other hand, are used for creating documents that can be shared and printed easily. HTML to PDF conversion is often required in applications where the output needs to be a printable document.

Benefits of HTML to PDF Conversion:

There are several benefits of HTML to PDF conversion. Some of these benefits include:

  • Consistency:

HTML to PDF conversion ensures that the output document looks the same across different platforms and devices.

  • Printability:

PDF documents are designed to be printed, so HTML to PDF conversion ensures that the output document is optimized for printing.

  • Accessibility:

PDF documents can be accessed by users who do not have access to the internet or a web browser.

  • Security:

PDF documents can be password protected, which makes them more secure than HTML documents.

  • Portability:

PDF documents can be easily shared and distributed across different platforms and devices.

Popular HTML to PDF Conversion Libraries:

There are several HTML to PDF conversion libraries available for C#. Some of the popular libraries include:

  • iTextSharp:

iTextSharp is a popular PDF library for C# that can be used to create, manipulate, and convert PDF documents.

  • Pdf:

Select.Pdf is an HTML-to-PDF conversion library designed to be easy to use and integrate with existing applications.

  • PDFSharp:

PDFSharp is an open-source PDF library for C# that can be used to create, edit, and convert PDF documents.

  • EVO HTML to PDF Converter:

EVO HTML to PDF Converter is an HTML to PDF conversion library that supports a wide range of HTML tags and CSS styles.

Converting HTML to PDF Using C#:

In this section, we will see the process of converting HTML to PDF using C# and the iTextSharp library.

Step 1: Install the iTextSharp Library

The first step in converting HTML to PDF using C# is to install the iTextSharp library. This can be done using the NuGet package manager in Visual Studio. To install iTextSharp, open the NuGet package manager and search for "iTextSharp". Select the iTextSharp package and click "Install".

Step 2: Create a New C# Project

Once the iTextSharp library has been installed, create a new C# project in Visual Studio. To create a new project, go to "File > New > Project". Select "Console App (.NET Framework)" as the project template, and choose a name and location for the project.

Step 3: Add a Reference to iTextSharp

The next step is to add a reference to the iTextSharp library in the project. To do this, right-click on the "References" folder in the Solution Explorer and select the "Add Reference" menu. In the "Reference Manager" window, select "Browse" and navigate.

Once you have navigated to the location of the iTextSharp library, select it and click "Add". This will add a reference to the iTextSharp library in your project.

Step 4: Write the Code to Convert HTML to PDF

Now that you have installed the iTextSharp library and added a reference to it in your project, you can begin writing the code to convert HTML to PDF.

First, you need to create an instance of the Document class, which is provided by the iTextSharp library. This class represents a PDF document that you will be creating.

C# Code:

Next, you need to create an instance of the PdfWriter class, which is also provided by the iTextSharp library. This class is responsible for writing the PDF document to a file or stream.

C# Code:

In this example, we are creating a PDF document called "output.pdf" in the current directory.

Next, you need to open the document by calling the Open() method.

C# Code:

Now, you can create an instance of the HTMLWorker class, which is provided by the iTextSharp library. This class is responsible for parsing the HTML document and converting it to a PDF document.

C# Code:

Next, you need to create an instance of the StringReader class, which is provided by the .NET framework. This class is used to read the HTML document that you want to convert to a PDF.

C# Code:

In this example, we are using a simple HTML document that contains a single heading.

Now, you can call the Parse() method of the HTMLWorker class to parse the HTML document and convert it to a PDF.

C# Code:

Finally, you need to close the document by calling the Close() method.

C# Code:

Run the code

Now that you have written the code to convert HTML to PDF, you can run the code by clicking the "Start" button in Visual Studio. This will compile and run the code and create a PDF document in the current directory called "output.pdf".

Conclusion:

In this article, we have discussed the process of converting HTML to PDF using C#. We have covered the benefits of HTML-to-PDF conversion, popular HTML-to-PDF conversion libraries, and the process of converting HTML to PDF using C# and the iTextSharp library. HTML to PDF conversion is a common requirement in many applications, and C# provides a powerful and easy-to-use platform for performing this conversion. With the knowledge gained from this article, you should be able to easily convert HTML to PDF in your own applications.


Next TopicMSDN C#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA