我想使用 Itext/Itextsharp 检查 PDF 中的嵌入字体
我已经使用 Itexthsarp 从 PDF 中获取字体,下面的代码来获取 PDF bool embeeedFont = false 的字体集合;
iTextSharpLGPV.PdfReader reader = new iTextSharpLGPV.PdfReader(fileName);
HashSet<String> names = new HashSet<string>();
iTextSharpLGPV.PdfDictionary resources;
for (int p = 1; p <= reader.NumberOfPages; p++)
{
iTextSharpLGPV.PdfDictionary dic = reader.GetPageN(p);
resources = dic.GetAsDict(iTextSharpLGPV.PdfName.Resources);
if (resources != null)
{
//gets fonts dictionary
iTextSharpLGPV.PdfDictionary fonts = resources.GetAsDict(iTextSharpLGPV.PdfName.Font);
if (fonts != null)
{
iTextSharpLGPV.PdfDictionary font;
foreach (iTextSharpLGPV.PdfName key in fonts.Keys)
{
font = fonts.GetAsDict(key);
string name = font.GetAsName(iTextSharpLGPV.PdfName.Basefont).ToString();
//check for prefix subsetted font
if (name.Length > 8 && name.ToCharArray()[7] == '+')
{
name = String.Format("{0} subset ({1})", name.Substring(8), name.Substring(1, 7));
}
else
{
//get type of fully embedded fonts
name = name.Substring(1);
iTextSharpLGPV.PdfDictionary desc = font.GetAsDict(iTextSharpLGPV.PdfName.Fontdescriptor);
if (desc == null)
name += "no font descriptor";
else if (desc.Get(iTextSharpLGPV.PdfName.Fontfile) != null)
name += "(Type1) embedded";
else if (desc.Get(iTextSharpLGPV.PdfName.Fontfile2) != null)
name += "(TrueType) embedded ";
else if (desc.Get(iTextSharpLGPV.PdfName.Fontfile3) != null)
name += name;//("+font.GetASName(PdfName.SUBTYPE).ToString().SubSTring(1)+")embedded';
}
names.Add(name);
}
}
}
}