本文介绍了如何使用Java iText检查所有使用的字体是否嵌入在PDF中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查PDF文件中使用的所有字体是否嵌入到使用Java和iText的文件中?我有一些现有的PDF文件,我想验证他们只使用嵌入字体。
这将需要检查没有PDF标准字体和其他使用的字体被嵌入在文件中。
如何检查PDF文件中使用的所有字体是否嵌入到使用Java和iText的文件中?我有一些现有的PDF文件,我想验证他们只使用嵌入字体。
这将需要检查没有PDF标准字体和其他使用的字体被嵌入在文件中。
/ p>
看起来这样会打印PDF中使用的字体,并且嵌入了它们。
/ *
*这个类是iText in Action - 2nd Edition一书的一部分Bruno Lowagie(ISBN:9781935182610)
*有关更多信息,请访问:http://itextpdf.com/examples/
*本示例仅适用于iText的AGPL版本。
* /
package part4.chapter16;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import java.util.TreeSet;
导入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 {
/ **生成的PDF文件。 * /
public static String RESULT
=results / part4 / chapter16 / fonts.txt;
/ **
*创建一个包含src PDF文件中字体信息的Set。
* @param src PDF文件的路径
* @throws IOException
* /
public Set< String> listFonts(String src)抛出IOException {
Set< String> set = new TreeSet< String>();
PdfReader reader = new PdfReader(src);
PdfDictionary资源;
for(int k = 1; k< = reader.getNumberOfPages(); ++ k){
resources = reader.getPageN(k).getAsDict(PdfName.RESOURCES);
processResource(set,resources);
}
reader.close();
返回集;
}
/ **
*从页面或XObject资源中提取字体名称。
* @param设置字体名称的集合
* @param resources资源字典
* /
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字体;
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)+)嵌入式;
}
set.add(name);
}
}
/ **
*主要方法。
*
* @param args不需要参数
* @throws DocumentException
* @throws IOException
* /
public static void main(String []] args)抛出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();
}
}
How to check that all fonts that are used in a PDF file are embedded in the file with Java and iText? I have some existing PDF documents, and I'd like to validate that they use only embedded fonts.
This would require checking that no PDF standard fonts are used and other used fonts are embedded in the file.
Look at the ListUsedFonts example from iText in Action.
http://itextpdf.com/examples/iia.php?id=287
Looks like this will print out the fonts used in a pdf and if they are embedded.
/*
* 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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!