我设法通过结合编码How to check that all used fonts are embedded in PDF with Java iText?和http://itextpdf.com/examples/iia.php?id=288得到了一些结果。 最初它并不像font.getAsName(PdfName.BASEFONT).toString();在我的情况下不起作用,但我做了一些小改动并获得了一些结果。
下面是我的编码:
/**
* 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 {
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.FONTNAME) != null) {
System.out.println("fontname " + font.get(PdfName.FONTNAME));
processFont(font,set);
}
}
}
/**
* 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.get(PdfName.FONTNAME).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));
}
}
}
因此,而不是使用字符串名称= font.getAsName(PdfName.BASEFONT)的ToString();我将它改为String name = font.get(PdfName.FONTNAME).toString();
这肯定会得到一些更好的结果,因为它给了我不同的字体。但是,我没有得到fontdescriptor和descendantfonts的结果。或者他们根本不在我的pdf中,或者因为我改变了编码,我永远不会在那里结束。 我可以假设是否发现一个子集被嵌入的字体,如果没有子集availbale在字体名称我可以假定字体没有嵌入?
正确的链接到PDF的https:/ /www.dropbox.com/s/anvm49vh87d8yqs/000024944.pdf?dl=0 –
我使用callas pdfToolbox检查了您的文件(谨慎,我与此工具有关),并声明Verdana和Verdana粗体已嵌入(并且已设置子集)但Helvetica不是嵌入式的;这与Adobe Acrobat报告相同。 –
还有一点“旁边的话题”评论 - 你意识到嵌入标准字体是一件危险的事情吗?不能保证您的字体副本与原始PDF文件创建者使用的字体相同,并且在嵌入字体时您可能会得到不同的宽度或编码问题。 –