Itext:如何检索pdf

时间:2015-08-24 14:30:09

标签: pdf fonts itext

如果嵌入了所有字体,我想检查PDF。我按照How to check that all used fonts are embedded in PDF with Java iText?中提到的编码进行了操作,但我仍然无法获得正确使用的字体列表。

参见我的示例pdf:https://www.dropbox.com/s/anvm49vh87d8yqs/000024944.pdf?dl=0,编码器根本没有字体,但是acrobat中的文档属性提到了Helvetica + Verdana(嵌入式子集)+ Verdana-Bold(嵌入式子集)。对于其他pdf,我确实得到Verdana Embedded子集,仅针对这些pdf,我无法获得字体列表。

由于我们必须处理来自内部的大量pdf作为外部源,我们需要能够嵌入字体以便打印它们。由于几乎不可能嵌入所有字体,我们只想嵌入常用字体,对于异国情调的字体,我们会忽略printrequest。

有人可以帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:1)

毕竟通过引用BASEFONT而不是FONT来实现它的工作:

/**
 * Creates a Set containing information about the fonts in the src PDF file.
 * @param src the path to a PDF file
 * @throws IOException
 */
public void listFonts(PdfReader reader,  Set<String> set) throws IOException {

    try {

        int n = reader.getXrefSize();
        PdfObject object;
        PdfDictionary font;

        for (int i = 0; i < n; i++) {
            object = reader.getPdfObject(i);
            if (object == null || !object.isDictionary()) {
                 continue;
            }

            font = (PdfDictionary)object;

            if (font.get(PdfName.BASEFONT) != null) {
                System.out.println("fontname " + font.getAsName(PdfName.BASEFONT).toString());
                processFont(font,set);

            }

        }


    } catch (Exception e) {
        System.out.println("error " + e.getMessage());
    }


}

/**
 * Finds out if the font is an embedded subset font
 * @param font name
 * @return true if the name denotes an embedded subset font
 */
private boolean isEmbeddedSubset(String name) {
    //name = String.format("%s subset (%s)", name.substring(8), name.substring(1, 7));
    return name != null && name.length() > 8 && name.charAt(7) == '+';
}

private void processFont(PdfDictionary font, Set<String> set) {

        **String name = font.getAsName(PdfName.BASEFONT).toString();**

        if(isEmbeddedSubset(name)) {
            return;
        }

        PdfDictionary desc = font.getAsDict(PdfName.FONTDESCRIPTOR);

        //nofontdescriptor
        if (desc == null) {
            System.out.println("desc null " );
            PdfArray descendant = font.getAsArray(PdfName.DESCENDANTFONTS);

            if (descendant == null) {
                System.out.println("descendant null " );
                set.add(name.substring(1));             
            }
            else {
                System.out.println("descendant not null " );
                for (int i = 0; i < descendant.size(); i++) {
                    PdfDictionary dic = descendant.getAsDict(i);
                    processFont(dic, set);                    
                  }             
            }            
        }
        /**
         * (Type 1) embedded
         */
        else if (desc.get(PdfName.FONTFILE) != null) {
            System.out.println("(TrueType) embedded ");
        }

        /**
         * (TrueType) embedded 
         */
        else if (desc.get(PdfName.FONTFILE2) != null) {
            System.out.println("(FONTFILE2) embedded ");
        }

        /**
         * " (" + font.getAsName(PdfName.SUBTYPE).toString().substring(1) + ") embedded" 
         */     
        else if (desc.get(PdfName.FONTFILE3) != null) {
            System.out.println("(FONTFILE3) ");
        }

        else {
            set.add(name.substring(1));         
        }


}

这给我的结果与acrobat reader&gt; properties

中的字体列表相同