Skip to content Skip to sidebar Skip to footer

No Change In Font Face In Html To Pdf Generation Using Itextsharp

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

Solution 1:

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 (Documentdocument = newDocument()) {
  PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  List<IElement> objects = HTMLWorker.ParseToList(
    newStringReader(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.

Solution 2:

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:

StyleSheetstyles=newStyleSheet();
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
//...//and thenvarparsedHtmlElements= HTMLWorker.ParseToList(data, styles);

Solution 3:

http://hivelocity.dl.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.4.1/itextsharp-all-5.4.1.zip

there you can get the last version of iTextSharp(5.4.1) Library

http://hivelocity.dl.sourceforge.net/project/itextsharp/xmlworker/xmlworker-5.4.1/itextsharp.xmlworker-all-5.4.1.zip

and here is the XML worker... it only works with the same version of iTextSharp (5.4.1)

Post a Comment for "No Change In Font Face In Html To Pdf Generation Using Itextsharp"