如何使用Java iText检查所有使用的字体是否已嵌入PDF?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用Java iText检查所有使用的字体是否已嵌入PDF?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3156字,纯文字阅读大概需要5分钟。
内容图文
![如何使用Java iText检查所有使用的字体是否已嵌入PDF?](/upload/InfoBanner/zyjiaocheng/653/600ecab5029b4e38bbb4499ba8132ac7.jpg)
如何使用Java和iText检查PDF文件中使用的所有字体是否已嵌入文件中?我有一些现有的PDF文档,并且我想验证它们仅使用嵌入式字体.
这将需要检查是否未使用PDF标准字体,以及是否将其他使用的字体嵌入文件中.
解决方法:
看一下iText in Action中的ListUsedFonts示例.
http://itextpdf.com/examples/iia.php?id=287
看起来这将打印出pdf中使用的字体以及嵌入的字体.
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
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 {
/** The resulting PDF file. */
public static String RESULT
= "results/part4/chapter16/fonts.txt";
/**
* Creates a Set containing information about the fonts in the src PDF file.
* @param src the path to a PDF file
* @throws IOException
*/
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;
}
/**
* Extracts the font names from page or XObject resources.
* @param set the set with the font names
* @param resources the resources dictionary
*/
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);
}
}
/**
* Main method.
*
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
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();
}
}
内容总结
以上是互联网集市为您收集整理的如何使用Java iText检查所有使用的字体是否已嵌入PDF?全部内容,希望文章能够帮你解决如何使用Java iText检查所有使用的字体是否已嵌入PDF?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。