paint-brush
Creating a PDF File Programmatically in C#by@tayyabcoder
4,865 reads
4,865 reads

Creating a PDF File Programmatically in C#

by Tayyab AliApril 4th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

IronPDF is a C library that creates PDF documents without the headache of advanced programming. It supports C and VB.NET and runs on Windows, Linux, and macOS operating systems. IronPDF uses HTML tags to create and format the PDF file. It is very easy to add headers and footers using HTML tags and almost all image types are supported for insertion in PDF documents. The IronPDF library is made with developers in mind so everything is abstracted away from them so they don't need to deal with any of the complexity.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Creating a PDF File Programmatically in C#
Tayyab Ali HackerNoon profile picture

Introduction

Creating a PDF file programmatically is tricky for many reasons. For starters, a PDF file is more than just a text document, it needs to be prepared in a certain way and it also can’t be created by using the same methods that you use to create a word document. The main problem with creating PDFs is that they often need to have unique formatting, headers, and metadata. An additional challenge is the fact that most PDFs nowadays contain shared styling which means that the font and spacing need to change at specific parts of the document.



Here I come with the solution to this problem.

IronPDF: C# PDF Library

IronPDF is a C# library that creates PDF documents without the headache of advanced programming. It supports C# and VB.NET and runs on Windows, Linux, and macOS operating systems. This library is made with developers in mind so everything is abstracted away from them so they don't need to deal with any of the complexity of generating a document that they can't see the final result until it's been fully rendered to its final destination - ready for printing.

Let's create PDF files using IronPDF in Visual Studio to understand how easy is to generate PDFs using IronPDF:

Create or Open C# Visual Studio Project

Open the latest version of Visual Studio and create a new C# project. It can be a console project or a GUI project. Choose the latest .NET core framework version. You can choose the .NET core framework according to your requirements. IronPDF can be integrated with the existing C# .net project. So, you can open your existing C# project too.

Install IronPDF

The next step is to install the IronPDF library in your project. Installing IronPDF is very easy. No hectic and lengthy procedures to follow. Just a simple line of command to install and you are good to go. Follow these steps to install the IronPDF library:

  1. Open Package Manager Console in Visual Studio.

  2. Write the following command in the console and press enter:

    • Install-Package IronPDF Writing installation Command After installing the IronPDF library
  3. It will start installing the IronPDF library in the project. After completion of installation, you can see the IronPDF files in the dependencies list section.

    IronPDF package files in the Dependencies section

There are two more methods to install IronPDF as NuGet Package Manager and the other method is to download the DLL file from the IronPDF website and import it into your project. You can visit this link for more detailed installation instructions on different OS and different methods.

After installing the IronPDF library, it's time to write the code to create a PDF file.

Generate PDF document from HTML String

IronPDF uses HTML to create PDF files. If you are using IronPDF then you must have basic knowledge of HTML tags to create and format the PDF file. IronPDF supports PDF formatting using HTML tags. It is very easy to add headers and footers. Almost all image types are supported for insertion in PDF documents. IronPDF also has functions to set page numbers, page break, custom paper size, orientation and rotation, resolution, and many other formatting requirements.

To start using IronPDF in the code, the IronPDF namespace must be imported at the top. Write the following line of code on top of the main program file:

using IronPDF;

Now, add the following code in the main method:

IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
            // add a header to very page easily
            Renderer.RenderingOptions.FirstPageNumber = 1;
            Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
            Renderer.RenderingOptions.TextHeader.CenterText = "Header!";
            Renderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
            Renderer.RenderingOptions.TextHeader.FontSize = 24;
            // add a footer too
            Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
            Renderer.RenderingOptions.TextFooter.FontFamily = "Arial";
            Renderer.RenderingOptions.TextFooter.FontSize = 16;
            Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
            Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
	string htmlText = "<h1> This is Sample Pdf file</h1> <p> This is the demo for Csharp Created Pdf using IronPdf </p> <p> IronPdf is a library which provides build in functions for creating, reading <br> and manuplating pdf files with just few lines of code. </p>";
            Renderer.RenderHtmlAsPdf(htmlText).SaveAs("Created PDF.pdf");

Output:

Output File

With the help of a given example, it is very easy to understand how easy is to manage the header, footer, and other formattings of the PDF file which has to be created. The output file is the same according to the expected output. Headers and footers are placed very perfectly and the HTML string works great as the formatting of the text. RenderHtmlAsPdf function creating the PDF using HTML string. In the SaveAs function, you can pass the path where you have to save the generated PDF file.

IronPDF also supports CSS, so you can add different colors and styles to make generated PDF beautiful. IronPDF can be used as HTML to pdf converter as it has a function to generate PDF from the HTML file. I will suggest you see this detailed c# create pdf tutorial if you want to understand it more deeply. IronPDF has well-written documentation too.

Conclusion

In this article, I have introduced a good PDF library that is very helpful in pdf generation using C# programming language. Creating PDF programmatically becomes a piece of cake due to IronPDF. There is no need to have very core knowledge of programming to understand the library. One who has basic knowledge can easily use the IronPDF library perfectly. But it is not open source. Although, you can avail it's 30 days free trial key to use it in production without the watermark. You can download the software product from this link.