如何使用 iText 检查 PDF 中的嵌入字体

How to Check Embedded font in PDF using iText

我想使用 Itext/Itextsharp

检查 PDF 中的嵌入字体

我已经使用Itexthsarp从PDF中获取字体,下面的代码获取PDF的字体集合 bool embededFont = 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);
                    }
                }
            }
        }

如评论中所述,OP 想知道如何检查嵌入的字体

请查看 PDF 规范中的 嵌入式字体程序 部分,例如在旧版 ISO 32000-1 的第 9.9 节:

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). ...

因此,您应该在相应字体的 FontDescriptor 字典中查找这些键,即基本上您在代码中使用 desc.