Reputation: 23
I would like to check the Embedded fonts in the PDF using Itext/Itextsharp
i have used the Itexthsarp to get the fonts from the PDF,below code to get the font collection of the PDF bool embeededFont = 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);
}
}
}
}
Upvotes: 1
Views: 1487
Reputation: 95898
As clarified in a comment, the OP wants to know how to check the font is embedded.
Please have a look at the section Embedded Font Programs in the PDF specifications, e.g. at section 9.9 in the older ISO 32000-1:
Table 126 summarizes the ways in which font programs shall be embedded in a PDF file, depending on the representation of the font program. The key shall be the name used in the font descriptor to refer to the font file stream; the subtype shall be the value of the Subtype key, if present, in the font file stream dictionary. Further details of specific font program representations are given below.
Table 126 – Embedded font organization for various font types
Key - Subtype - Description
FontFile - — - Type 1 font program, in the original (noncompact) format described in Adobe Type 1 Font Format. This entry may appear in the font descriptor for a Type1 or MMType1 font dictionary.
FontFile2 - — - (PDF 1.1) TrueType font program, as described in the TrueType Reference Manual. This entry may appear in the font descriptor for a TrueType font dictionary or (PDF 1.3) for a CIDFontType2CIDFont dictionary.
FontFile3 - Type1C - (PDF 1.2) Type 1–equivalent font program represented in the Compact Font Format (CFF), as described in Adobe Technical Note #5176, The Compact Font Format Specification. This entry may appear in the font descriptor for a Type1 or MMType1 font dictionary.
FontFile3 - CIDFontType0C - (PDF 1.3) Type 0 CIDFont program represented in the Compact Font Format (CFF), as described in Adobe Technical Note #5176, The Compact Font Format Specification. This entry may appear in the font descriptor for a CIDFontType0 CIDFont dictionary.
FontFile3 - OpenType - (PDF 1.6) OpenType® font program, as described in the OpenType Specification v.1.4 (see the Bibliography). OpenType is an extension of TrueType that allows inclusion of font programs that use the Compact Font Format (CFF). ...
Thus, you should look for these keys in the FontDescriptor dictionary of the respective font, i.e. essentially what you did in your code with desc
.
Upvotes: 1