I'm using a HTML Rich-Text Editor to help me create a template for dynamic PDF reports, and it's working fine except that it doesn't change the font-face.
This editor is using the font tag instead of CSS styles, and I would welcome any way to programmatically change the font tags to equivalent tags using styles instead.
HTML (yes its messy, its from a WYSIWYG editor):
<div>
<br>
<div align="center">
<font size="5">
<b>
<br>
<div align="center">
<font font-face="Times New Roman" size="5">
<b>Example
<font size="6">Chamber
<font size="5">
<font size="4">Website</font>
</font></font>Quotes</b>
</font>
<font face="Times New Roman">
<br>
<font face="Times New Roman">
<br>~
<font face="Times New Roman" color="#0000FF">
<b>
<u> [!PlanName] </u></b>
</font>
<font face="Times New Roman">
<br>~
<font face="Times New Roman" color="#0000FF">
<b> </b>
</font>
<font face="Times New Roman" color="#0000FF">
<b>
<font color="#000000">Deductible
$[!PlanDeductible]: </font></b>
</font>
<font face="Times New Roman" color="#B0B0FF">
[!PlanRate]
<br>
<font face="Times New Roman">/~/~</font>
<br>
<br>
<br>
<font face="Courier New" size="1">
Copyright Example.com</font>
<br>
<br>
<font face="Arial">test</font>
<br></br>
</br>
</br>
</br>
</br>
</br>
</br></font></br>
</font></br>
</font>
</br>
</font>
</div>
</br>
</b>
</font>
</div>
</br></div>
C#:
public static byte[] ConvertHtmlToPdf(string html)
{
html = HtmlPostProcessor.Process(html);
byte[] fileData = null;
string tempPath = ConfigurationManager.AppSettings["TempDirectory"];
string tempPDFFile = Path.Combine(tempPath, Guid.NewGuid() + ".pdf");
Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
{
PdfWriter.GetInstance(document, fs);
using (StringReader stringReader = new StringReader(html))
{
List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null );
document.Open();
foreach (IElement item in parsedList)
{
document.Add(item);
}
document.Close();
}
}
FileStream generatedPDF = File.Open(tempPDFFile, FileMode.Open);
fileData = new byte[(int)generatedPDF.Length];
int result = generatedPDF.Read(fileData, 0, (int)generatedPDF.Length);
generatedPDF.Close();
File.Delete(tempPDFFile);
return fileData;
}
EDIT
I've been using iTextSharp version 5.1.1.0.
The simplest, brute-force way is to call FontFactory.RegisterDirectories() before calling HTMLWorker.ParseToList()
. Be warned, however - the method attempts to register/map all fonts on the running system.
So if you're running this in ASP.NET, for example, you probably want to put the call in global.asax.
EDIT : Working example using FontFactory.RegisterDirectories()
and the HTML you provided above:
FontFactory.RegisterDirectories();
using (Document document = new Document()) {
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
List<IElement> objects = HTMLWorker.ParseToList(
new StringReader(html), null);
foreach (IElement element in objects) {
document.Add(element);
}
}
Just substitute Response.OutputStream
with the Stream
of your choice. Result PDF file from above .
HTMLWorker is obsolete. You need to switch to the new XMLWorker class. But if you want to use the HTMLWorker, you can define a global style for it as well:
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
//...
//and then
var parsedHtmlElements = HTMLWorker.ParseToList(data, styles);
there you can get the last version of iTextSharp(5.4.1) Library
and here is the XML worker... it only works with the same version of iTextSharp (5.4.1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.