Friday, September 9, 2011

How to Convert Html to PDF in ASP.NET , C# Using iTextsharp

There are many solutions can convert html to PDF.

However they have different effects. As far as I know, the simplest solution that I ever seen is using iTextsharp and the code as below:

 Sample Code:

         string FilePath = Server.MapPath("") + "/OrderconfirmationMail.htm";
         System.IO.StreamReader myFile =new System.IO.StreamReader(FilePath);
         string myString = myFile.ReadToEnd();
         string ImagePath = Server.MapPath("") + "\\Images\\";
        myString = myString.Replace("src=\"Images/", ImagePath);
         StringReader sr = new StringReader(myString);
         iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 2f, 2f, 2f, 2f);
         HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
         string filename = Server.MapPath("") + "\\WordDoc\\parsetest3.pdf";

         if (File.Exists(filename))
         PdfWriter.GetInstance(pdfDoc, new FileStream(filename, FileMode.Create));

I found some links where you can find details:


1 comment:

Heemanshu Bhalla said...

This is Nice Article....

HTML / aspx web Page TO PDF using iTestsharp C#

This link has Demo App with code availaible to download and it is working successfully



Welcome to Rajesh Prajapati, blog.
Here you can find some useful code and information about, c#,, SQL Server, Web Service, Web Designing etc