My StringBuilder contains some HTML Code like a h1 element:
StringBuilder html = new StringBuilder();
html.Append("<h1 style='color:red;'>" + title + "</h1>");
Then i build a Table
dynamically with different cell background-colors as a Table
Object.
I need to append this table object to my StringBuilder to export the StringBuilder as PDF document with iTextSharp
.
How can I append the Table to my StringBuilder and keep the background-colors of the cells?
This is the way how I generate the pdf document using iTextsharp:
bool isPrint = true; //tell the generate method that we will use this for print
generate gen = new generate(); //custom class that contains some functions
Object[] genObjects = new Object[2];
genObjects = gen.generateTable(currentTable._tableName, currentTable._tableID.ToString(), currentTable, isPrint);
//cast back to StringBuilder because object[] contains different types
StringBuilder sb = (StringBuilder)genObjects[1];
//Export HTML String as PDF.
StringReader sr = new StringReader(sb.ToString());
//need the pdf in landscape mode
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + friendlyName + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();