Incorporating font styles in HTML to PDF conversion using itextPDF5.3.4.jar

StackOverflow https://stackoverflow.com/questions/22714464

  •  23-06-2023
  •  | 
  •  

Pergunta

I am trying to include multiple font styles in HTML to be reflected on my code using the following snippet. The html text when having font style mentioned in font face tag, is not being picked up to be displayed in PDF.

String htmlText = value.trim();

             ColumnText ct = new ColumnText(over);
             List<AcroFields.FieldPosition> positions1 = form.getFieldPositions(key);
             Rectangle rect = positions1.get(0).position;
             ct.setSimpleColumn(rect.getLeft(), rect.getBottom(),rect.getRight(), rect.getTop());
             Reader htmlreader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(htmlText.getBytes("UTF-8"))));
             StyleSheet styles = new StyleSheet();
             styles.loadTagStyle("body", "font", "Comic Sans MS");
             List<Element> objects = HTMLWorker.parseToList(htmlreader,styles);

             for (Element element : objects) 
             {
                 ct.addElement(element);
           }
            ct.go();

The StyleSheet class is not reflecteing any font on the PDF inspite of mentioning it. It defaults to Arial always.

I require that whatever is the font mentioned in Font tag's face attribute should be reflected in PDF. What are the methods that i must incorporate in such situation.

Foi útil?

Solução

Did you register the font? iTextSharp doesn't scan the system for installed fonts unless you tell it to since it could lead to a performance/caching nightmare.

FontFactory.Register("C:\\Windows\\Fonts\\comic.ttf", "Comic Sans MS");

You can also tell iTextSharp to scan your font folder but I'd recommend against it:

FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top