0

I am unable to set font and font size while export data into PDF using c#.net

I've tried many different ways of doing this, each time failed. please help.

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
htw.WriteBeginTag("p");
htw.Write(HtmlTextWriter.TagRightChar);
htw.Write("Summary Details");
htw.WriteEndTag("p");
htw.WriteBreak();

htw.WriteBeginTag("p");
htw.Write(HtmlTextWriter.TagRightChar);
htw.Write("Scrip Details");
htw.WriteEndTag("p");

gridCapitalGD.RenderControl(htw);

htw.WriteBreak();
htw.WriteBeginTag("p");
htw.Write(HtmlTextWriter.TagRightChar);
htw.Write("Transaction Details");
htw.WriteEndTag("p");
grdViewTranDetail.RenderControl(htw);

StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

1 Answers1

2

Don't use HTMLWorker. It's deprecated, and was superseded by XMLWorker. Which is now also on its way to being deprecated, and is superseded by pdfHTML.

HTMLWorker was ever only intended to convert small snippets into pdf syntax. And the issues most people run into are not 'bugs' but simply scope limitations.

small example of pdfHTML and iText7:

// IO
File htmlSource = new File("input.html");
File pdfDest = new File("output.pdf");

// pdfHTML specific code
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(new FileInputStream(htmlSource), new FileOutputStream(pdfDest), converterProperties);

You can use CSS to set the font and font size, alternatively, you can play around with the ConverterProperties.

For information on pdfHTML, check out https://itextpdf.com/itext7/pdfHTML

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54