0

我正在尝试在 HTML 中包含多种字体样式,以使用以下代码段反映在我的代码中。字体标签中提到的字体样式时的 html 文本不会被拾取以显示在 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();

尽管提到了 StyleSheet 类,但它并未反映 PDF 上的任何字体。它始终默认为 Arial。

我要求 Font 标签的 face 属性中提到的字体应该反映在 PDF 中。在这种情况下我必须采用哪些方法。

4

1 回答 1

1

你注册字体了吗?iTextSharp 不会扫描系统以查找已安装的字体,除非您告诉它这样做,因为它可能导致性能/缓存噩梦。

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

您也可以告诉 iTextSharp 扫描您的字体文件夹,但我建议不要这样做:

FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts")
于 2014-03-28T14:55:11.887 回答