【发布时间】:2014-04-04 07:54:12
【问题描述】:
在我们的项目中,我们需要在 PDF 上打印音标符号 (℗)。在发现我们必须为此使用 Arial(或任何其他字体,但 arial 可以)后,我们将该字体添加到项目中。但是,在尝试打印 pdf 时,不会打印音标符号。
我们使用 iText 5.5.0 并添加带有 IDENTITY_H 编码的 arial.ttf。如果我理解正确,这应该启用字体中的所有字符。谁能指出出了什么问题?可能是我们的字体文件有问题还是 iText 打扰了我们?
此代码创建一个带有字母 f 和音标字符的 pdf。在这个测试类中,音标字符也没有打印出来。
public class TestPdf {
public static void main(String[] args) throws FileNotFoundException, DocumentException {
// Step 1: create document
Document document = new Document();
// Step 2: get instance of PdfWriter
PdfWriter.getInstance(document, new FileOutputStream("myfile.pdf"));
// Step 3: open document
document.open();
// Step 4: prepare paragraph
Font arial12 = ArialFont.ofSize(12);
Paragraph paragraph = new Paragraph("The letter f: \u0066 \nThe phonogram symbol: \u2117");
paragraph.setFont(arial12);
// Step 5: add paragraph
document.add(paragraph);
// Step 6: close document
document.close();
}
public static class ArialFont {
protected static BaseFont arialFont;
public static Font ofSize(int size) {
return new Font(getBaseFont(), size);
}
public static BaseFont getBaseFont() {
try {
if (arialFont == null) {
arialFont = BaseFont.createFont("/fonts/ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
return arialFont;
} catch (DocumentException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
}
【问题讨论】: