java - iText Java : Not adding phonogram symbol

标签 java pdf itext

在我们的项目中,我们需要在 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);
            }
        }
    }
}

最佳答案

尝试这样做:

Font arial12 = ArialFont.ofSize(12);
Paragraph paragraph = new Paragraph("The letter f: \u0066 \nThe phonogram symbol: \u2117", arial12);
为什么?老实说我不知道​​。设置段落文本后设置字体似乎是一个错误。我运行了你的代码,发现你的 pdf 没有嵌入 Arial 字体。

为了调试 PDF 文件,我使用了一组名为 XPdf 的命令行工具:

http://www.foolabs.com/xpdf/

您可以使用pdffonts <filename>调用以检查 pdf 文件中嵌入或引用的字体。

关于java - iText Java : Not adding phonogram symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856782/

相关文章:

java - 从 intelliJ 中的 libgdx 项目生成可运行的 .jar 文件

java - Java套接字将多个对象发送到同一服务器

javascript - 使用 Angular 2 下载 Node 流

java - 无法提取 NotoSansCJKsc-adobe reader 中的常规字体错误?

java - 在不读取类文件的情况下找出类是否具有类初始值设定项

java - android 6 中 android webview youtube 视频全屏模式下的问题

vba - 用于将 Excel 文档导出为 PDF 供所有用户使用的宏

python - 使用离线绘图将 jupyter notebook 导出为 pdf;缺少图表

c# - 如何将 .pdf 和 .jpg 文件合并为一个 pdf

Java iText 和自定义单选按钮行为