Last post Sep 08, 2013 06:32 PM by jats_ptl
Sep 05, 2013 06:39 PM|2bitcoder|LINK
I have html pages that are designed specifically for printing (Reports), they work well and look good.
I would like to be able to output these to pdf also, I've previously tried with print drivers, however the results are varied and not easy for users.
Hence, I would like to be able to output the page directly to pdf using itextsharp and the htmlparser. I've been looking at
http://www.mikesdotnetting.com/Article/205/Exporting-The-Razor-WebGrid-To-PDF-Using-iTextSharp and itextsharp articals, which I intend on using to write other reports.
I've also looked at this
http://aspnettutorialonline.blogspot.com/2012/04/converting-html-to-pdf-using-itextsharp.html however I'm really not comfortable with c# yet, and would much prefer the razor approach.
I would prefer not to have to re-code my html print optimsed pages if I could easily use the htmlparser, which I'm not sure how to implement.
Any tips would be helpful...
Sep 05, 2013 08:58 PM|jats_ptl|LINK
Please follow the Steps for converting html to pdf using iTextSharp:
1. Take iTextsharp dll from here. under bin folder.
2. in the below code, CreatePDFFromHTMLFile method will convert html to pdf conversion.
3. TestPDF Namespace, will get from here.under app_code folder.
4. Here you can set password to protect your pdf document.
5. After your execution you will get generated pdf document under files folder.
protected void Page_Load(object sender, EventArgs e)
string strHtml = string.Empty;
//HTML File path -http://aspnettutorialonline.blogspot.com/
string htmlFileName = Server.MapPath("~") + "\\files\\" + "ConvertHTMLToPDF.htm";
//pdf file path. -http://aspnettutorialonline.blogspot.com/
string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "ConvertHTMLToPDF.pdf";
//reading html code from html file
FileStream fsHTMLDocument = new FileStream(htmlFileName, FileMode.Open, FileAccess.Read);
StreamReader srHTMLDocument = new StreamReader(fsHTMLDocument);
strHtml = srHTMLDocument.ReadToEnd();
strHtml = strHtml.Replace("\r\n", "");
strHtml = strHtml.Replace("\0", "");
Response.Write("pdf creation successfully with password -http://aspnettutorialonline.blogspot.com/");
catch (Exception ex)
public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
object TargetFile = FileName;
string ModifiedFileName = string.Empty;
string FinalFileName = string.Empty;
/* To add a Password to PDF -http://aspnettutorialonline.blogspot.com/ */
TestPDF.HtmlToPdfBuilder builder = new TestPDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
TestPDF.HtmlPdfPage first = builder.AddPage();
byte file = builder.RenderPdf();
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
ModifiedFileName = TargetFile.ToString();
ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");
string password = "password";
iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, password, "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
catch (Exception ex)
Source : Amy Peng - MSFT Solution http://forums.asp.net/t/1886131.aspx?Convert+HTML+to+PDF+using+iTextSharp
Hope this helps.
Sep 08, 2013 06:29 PM|2bitcoder|LINK
Follow up - I implemented the above solution, however I got errors as I think, my .CSHTML may have been to complex for the htmlparser, simplified html worked fine.
In the end I spent a day learning iTextSharp, then wrote my reports.
The outcome was better than I'd hoped.
Sep 08, 2013 06:32 PM|jats_ptl|LINK
Glad that I could help you!! Cheers