Ok, I feel like I'm missing something but who knows. I'm trying output formatted HTML (html tags) to PDF but I want it to look like it would as a web page. What I get is the html code instead. Anyone have an example of how to do this? I'm really stuck.
protected void GeneratePDF()
{
strBody = Server.HtmlEncode(strBody.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(strBody), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
pdfDoc.Add((IElement)htmlarraylist[k]);
}
Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
mypara.IndentationLeft = 36;
mypara.InsertRange(0, htmlarraylist);
pdfDoc.Add(new Paragraph(htmlarraylist.ToString()));
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ConflictReport_" + DateTime.Now.ToString("MM-dd-yyyy HH:mm:ss") + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
bbcompent1
All-Star
32978 Points
8502 Posts
Moderator
How to get iTextSharp to output formatted HTML
Nov 08, 2012 05:12 PM|LINK
Ok, I feel like I'm missing something but who knows. I'm trying output formatted HTML (html tags) to PDF but I want it to look like it would as a web page. What I get is the html code instead. Anyone have an example of how to do this? I'm really stuck.
protected void GeneratePDF() { strBody = Server.HtmlEncode(strBody.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(strBody), null); //add the collection to the document for (int k = 0; k < htmlarraylist.Count; k++) { pdfDoc.Add((IElement)htmlarraylist[k]); } Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder" mypara.IndentationLeft = 36; mypara.InsertRange(0, htmlarraylist); pdfDoc.Add(new Paragraph(htmlarraylist.ToString())); pdfDoc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=ConflictReport_" + DateTime.Now.ToString("MM-dd-yyyy HH:mm:ss") + ".pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); }