How to format PDF or succeed with itextsharper from Asp.net Control
I want to create a pdf, excel report. I have used asp.net relay for data binding. I am using iTextSharp. I have displayed, pdf and excel page control.
code:
StringWriter sw2 = new StringWriter();
HtmlTextWriter hw2 = new HtmlTextWriter(sw2);
this.rptBillReport.RenderControl(hw2);
Paragraph report = new Paragraph();
using (StringReader sr2 = new StringReader(sw2.ToString()))
{
//Parse and get a collection of elements
List<IElement> elements2 = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr2, null);
foreach (IElement val2 in elements2)
{
//Add those elements to the paragraph
report.Add(val2);
}
}
Document pdfDoc = new Document(iTextSharp.text.PageSize.A4,
30f, 30f, 30f, 0.0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//htmlparser.Parse(sr);
pdfDoc.Add(report);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
My question is here:
How do I format my report by adding a border and other style in pdf and excel format? It doesn't show up in my current report.
+3
source to share
1 answer
Thanks, I added the stylesheet like this:
StyleSheet styles= new StyleSheet();
styles.LoadTagStyle("#rptBillReport", "height", "30px");
styles.LoadTagStyle("#rptBillReport", "font-weight", "bold");
styles.LoadTagStyle("#rptBillReport", "font-family", "Cambria");
styles.LoadTagStyle("#rptBillReport", "font-size", "20px");
styles.LoadTagStyle("#rptBillReport", "background-color", "white");
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.SetStyleSheet(styles);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
It worked.
+1
source to share