看一下iText in Action中的ListUsedFonts示例。
http://itextpdf.com/examples/iia.php?id=287
看起来这将打印出pdf中使用的字体以及嵌入的字体。
package part4.chapter16;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import java.util.Set;import java.util.TreeSet;import part3.chapter11.FontTypes;import com.itextpdf.text.documentException;import com.itextpdf.text.pdf.PdfDictionary;import com.itextpdf.text.pdf.PdfName;import com.itextpdf.text.pdf.PdfReader;public class ListUsedFonts { public static String RESULT = "results/part4/chapter16/fonts.txt"; public Set<String> listFonts(String src) throws IOException { Set<String> set = new TreeSet<String>(); PdfReader reader = new PdfReader(src); PdfDictionary resources; for (int k = 1; k <= reader.getNumberOfPages(); ++k) { resources = reader.getPageN(k).getAsDict(PdfName.RESOURCES); processResource(set, resources); } reader.close(); return set; } public static void processResource(Set<String> set, PdfDictionary resource) { if (resource == null) return; PdfDictionary xobjects = resource.getAsDict(PdfName.XOBJECT); if (xobjects != null) { for (PdfName key : xobjects.getKeys()) { processResource(set, xobjects.getAsDict(key)); } } PdfDictionary fonts = resource.getAsDict(PdfName.FONT); if (fonts == null) return; PdfDictionary font; for (PdfName key : fonts.getKeys()) { font = fonts.getAsDict(key); String name = font.getAsName(PdfName.baseFONT).toString(); if (name.length() > 8 && name.charAt(7) == '+') { name = String.format("%s subset (%s)", name.substring(8), name.substring(1, 7)); } else { name = name.substring(1); PdfDictionary desc = font.getAsDict(PdfName.FONTDEscriptOR); if (desc == null) name += " nofontdescriptor"; else if (desc.get(PdfName.FONTFILE) != null) name += " (Type 1) embedded"; else if (desc.get(PdfName.FONTFILE2) != null) name += " (TrueType) embedded"; else if (desc.get(PdfName.FONTFILE3) != null) name += " (" + font.getAsName(PdfName.SUBTYPE).toString().substring(1) + ") embedded"; } set.add(name); } } public static void main(String[] args) throws IOException, documentException { new FontTypes().createPdf(FontTypes.RESULT); Set<String> set = new ListUsedFonts().listFonts(FontTypes.RESULT); PrintWriter out = new PrintWriter(new FileOutputStream(RESULT)); for (String fontname : set) out.println(fontname); out.flush(); out.close(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)