0

我正在使用此示例将 java swing JFrame 转换为 pdf:export jcomponent to pdf with itext。问题是当我的框架包含像 čěř... 这样的 UTF-8 字符时。导出的文档中缺少这些字符。如何在我的 PDF 中打开这些字符?

更新:

我尝试您的示例,但出现异常:

ExceptionConverter: com.itextpdf.text.DocumentException: Font 'fonts/verdana.ttf,1' with 'Identity-H' is not recognized.
 at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:699)
 at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:606)
 at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:549)
 at com.itextpdf.text.pdf.DefaultFontMapper.awtToPdf(DefaultFontMapper.java:107)
 at com.itextpdf.text.pdf.PdfGraphics2D.getCachedBaseFont(PdfGraphics2D.java:1082)
 at com.itextpdf.text.pdf.PdfGraphics2D.setFont(PdfGraphics2D.java:1075)
 at javax.swing.JComponent.paintChildren(JComponent.java:855)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at javax.swing.JComponent.paintChildren(JComponent.java:874)
 at javax.swing.JComponent.printChildren(JComponent.java:1214)
 at javax.swing.JComponent.paint(JComponent.java:1049)
 at javax.swing.JComponent.print(JComponent.java:1183)
 at GUI.PlayOffPanel.export(PlayOffPanel.java:322)

看起来导入 .ttf 字体(如 verdana 或 times)存在一些问题,但举例来说,您添加的字体是有效的。但我真的很想使用 Verdana 字体所以还在寻找答案

4

1 回答 1

3

由于您没有显示自己的任何代码,我认为您使用了从export jcomponent 到 PDF 的代码和 itext(您提到的问题)。因此,您检索Graphics2D实例的代码是:

Graphics2D g2d = tp.createGraphics(bill.getWidth(), bill.getHeight(), new DefaultFontMapper());

iText in Action — 2nd Edition中的第 14.5.2 节将文本绘制到 PdfGraphics2D 中所述,DefaultFontMapper您使用的假设每种字体都使用编码 Cp1252 (WinAnsi)。(不幸的是,JavaDoc 标头中没有记录,DefaultFontMapper,仅在其方法的 JavaDoc 中insertDirectory。)这将解释您丢失的字符。

您可以通过向映射器添加自定义BaseFontParameters对象来解决此问题,就像在示例Text1ToPdf2.java中所做的那样c:/windows/fonts/msgothic.ttc:

// create a font mapper
DefaultFontMapper mapper = new DefaultFontMapper();
// map MS Gothic to the corresponding font program
BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1");
parameters.encoding = BaseFont.IDENTITY_H;
mapper.putName("MS PGothic", parameters );
// Create a Graphics2D object
Graphics2D g2 = new PdfGraphics2D(canvas, 600, 60, mapper);

根据您的用例,对所有使用的字体(可能)执行此操作可能太乏味了,但本书的同一部分解释了替代方案:您可以

  • 创建FontMapper您自己的实现(例如,通过子类化DefaultFontMapper),
  • 使用AsianFontMapper(如果缺少某些亚洲字符集),或
  • 将字形添加为形状,而不是存储在字体中的字符。

这些选项涵盖了书中的多个页面,在这里无法给出一个答案。您可以通过查看示例Text2ToPdf1.javaText2ToPdf2.javaText2ToPdf3.java来获得第一印象。

于 2013-02-12T10:44:40.687 回答