如何嵌入没有字体描述符的字体?

How to embed fonts which don't have a font descriptor?

我正在尝试将 PDF 转换为 PDFA-2B。 1 of my test files 包含没有字体描述符的 Type1 字体:

我需要嵌入这些字体。

如何嵌入这些字体?

我尝试创建字体描述符,但没有成功。

private static PdfDictionary addFontDesc(PdfDictionary font){
        PdfDictionary fontDescriptor;
        fontDescriptor = new PdfDictionary(PdfName.FONTDESCRIPTOR);
        PRIndirectReference fd = fontDescriptor.getIndRef();

        if (fd != null){
            font.put(PdfName.FONTDESCRIPTOR, fd);
            System.out.println("fontDescriptor was added to font " + font.getAsName(PdfName.BASEFONT).toString());
        }else {
            System.out.println("Error while trying to add fontDescriptor to " + font.getAsName(PdfName.BASEFONT).toString());
        }
        return font;
}

我总是得到 NullPointerException

正如@Bruno 在评论中所说,您的问题在当前状态下过于宽泛。因此,此答案仅关注您提出的有形问题部分:

always get a NullPointerException.

您似乎认为

中的 getIndRef
PRIndirectReference fd = fontDescriptor.getIndRef();

使 fontDescriptor 成为间接对象(因为只有间接对象可以有间接引用)如果它还不是一个的话。

这个假设是错误的。如果你在一个还不是间接的对象上调用 getIndRef,你会得到 null 正如你观察到的那样。


您可以使用 PdfWriter 方法 addToBody 使任何 PdfObject 间接化,其中 returns 一个 PdfIndirectObject 您可以使用 getIndirectReference,

public PdfIndirectObject addToBody(final PdfObject object) throws IOException

public PdfIndirectObject addToBody(final PdfObject object, final boolean inObjStm) throws IOException