Tuesday 7 November 2017

Create Pdf Using ItextSharp in C#.

In this article I will explain about how to Create a new Pdf file using ItextSharp.

Before we Start Creating Pdf, in the whole article I will describe about how to create a new Pdf file easily.

ITextSharp is a .NET PDF library and for pdf conversion we are using it. Take a new solution in Solution Explorer and either you can add itextsharp dll by using NuGet package or by using
Package Manager Console or you can directly add dll in References. In the below picture I show you an example that how to install itextsharp using NuGet package.

















After you installed itextsharp in your solution, Please check in your References to make sure that it exists.
After that add a new Folder Named called as “Output” where we will store the new Pdf File and whose structure looks like this,


After that create your UI like this,












When I click on Create Pdf button then at that time it will create a New Pdf and which will bestored in “Output” folder and whose code is described below,







In the first line I created the object of Document Class and the first parameter is the pagesize and here I set it as A4 and the other parameters are nothing but margin left, margin right, margin top and margin bottom.

In the next line I created a table which is called as “PdfPTable” in iTextSharp and here the table contains 4 columns and also I set the properties for tables.















After that I am going to add a new cell and that cell contains certain properties like Border, Horizontal Alignment etc and at last we will add that cell into table.







We can add as many numbers of cells and tables as we want.

After that I set the outputpath and I will open the document object and will add the table and at last will close the document object.

The ouput looks like this which is shown in the given image,



















Here I have showed you a simple example but you can add any number of tables, cells, images etc and you can design your pdf as per your requirement.

0 comments:

Post a Comment