I have some c# code that takes HTML and converts it to PDF. It is not
converting the HTML exactly as it displays in the browser and this is proving
to
be a real headache to fix. Please, any help that you can provide would be of
huge benefit! Here is the code...
string strSOW = "INSERT HTML HERE";
string html = strSOW.Replace(((Char)9).ToString(),
" ").Replace(((Char)160).ToString(),
" ").Replace(@"\r\n", "<br />").Replace(@"<div> </div>", "<br
/>").Replace(@"<ul>", "<br /><ul>");
html = html.Replace("<html>", "<html><body><span
style=\"font-size: 8pt; font-family: Calibri;\"");
html = html.Replace("</html>", "</span></body></html>");
// ParseToList requires a StreamReader instead of just
a
string so just wrap it
using (StringReader sr = new StringReader(html))
{
// Get the raw PdfContentByte object letting us
draw
"above" existing content
PdfContentByte cb = stamper.GetOverContent(1);
// Set the font to Calibri
BaseFont bf =
BaseFont.CreateFont(@"C:\Windows\Fonts\calibri.ttf", BaseFont.CP1257, true);
cb.SetFontAndSize(bf, 8);
// Create a new ColumnText object bound to the
above
PdfContentByte object
ColumnText ct = new ColumnText(cb);
// Get the dimensions of the first page of our
source document
iTextSharp.text.Rectangle page1size =
reader.GetPageSize(1);
// Create a single column object spanning the
entire
page
ct.SetSimpleColumn(40, 210, page1size.Width - 40,
page1size.Height - 210, 0, Element.ALIGN_LEFT);
// Convert the HTML to iTextSharp elements
List<IElement> elements =
iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
// Loop through each element
foreach (IElement el in elements)
{
//Add the element to the ColumnText
ct.AddElement(el);
}
// IMPORTANT, this actually commits our object to
the PDF
ct.Go();
}
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php