【问题标题】:iText Java: Not adding phonogram symboliText Java:不添加音标符号
【发布时间】: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);
            }
        }
    }
}

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    尝试这样做:

    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 文件中嵌入或引用的字体。

    【讨论】:

    • +1 setFont() 方法的名称选择得不是很好(mea culpa)。该方法可用于更改 Paragraph 的字体,用于添加Paragraph 的内容;它不会更改 Paragraph 中已经 存在 的内容的字体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2018-03-31
    • 2022-07-31
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多