How to create PDF files using C#

To working in the day to day software development, in mostly applications, we need to build the functionally for the creating the PDF files for the end user. To creating a PDF files in the Java is quite easier and simple using the iText utility with having all its documentation and community support available in wide range of web forums.

To working in .Net environment, there is no built-in support or controls which support to create the PDF files but there are third parties components available which provide this functionally. The one component is called iTextSharp which is quite similar to the iText. iTextSharp don’t have any proper documentation but for a average C# programmer, iText documentation could be very helpful. There is also one book available for the creating the PDF files and again this is available for Java. This book is called iText in Action.

Simply, download the iTextSharp component and unzip the folder. Create the new web project and copy the itextsharp.ddl file in the bin folder. If bin folder does not exist then create the new bin folder at the root of the project directory.

Now at the code behind of the file, add two namespace for iTextSharp,
Using iTextSharp.Text;
Using iTextSharp.Text.pdf;

Now create a document object using:

var doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(path +”/mypdf.pdf”, FileMode.Create));;

//now you can add the information or text for your pdf file.

doc.add(new Paragraph(“Hello world, this my pdf file”));

This is quite simple to create a pdf files in the .Net framework using the iTextSharp components. Here is some more links available for further details and examples.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s