简体   繁体   English

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

[英]How to Check Embedded font in PDF using iText

I would like to check the Embedded fonts in the PDF using Itext/Itextsharp 我想使用Itext / Itextsharp检查PDF中的Embedded字体

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

As clarified in a comment, the OP wants to know how to check the font is embedded . 正如评论中所阐明的,OP希望知道如何检查字体是否嵌入

Please have a look at the section Embedded Font Programs in the PDF specifications, eg at section 9.9 in the older ISO 32000-1: 请查看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. 表126总结了取决于字体程序表示形式的将字体程序嵌入PDF文件的方式。 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 表126 –各种字体类型的嵌入式字体组织

Key - Subtype - Description 键-子类型-说明

FontFile - - Type 1 font program, in the original (noncompact) format described in Adobe Type 1 Font Format . FontFile - - - 1型字体程序,在Adobe类型1字体格式描述的原始(非紧)格式。 This entry may appear in the font descriptor for a Type1 or MMType1 font dictionary. 此项可能出现在Type1MMType1字体字典的字体描述符中。

FontFile2 - - (PDF 1.1) TrueType font program, as described in the TrueType Reference Manual . FontFile2 - - - (PDF 1.1)TrueType字体程序,作为的TrueType参考手册中描述。 This entry may appear in the font descriptor for a TrueType font dictionary or (PDF 1.3) for a CIDFontType2CIDFont dictionary. 此项可能出现在TrueType字体字典的字体描述符中,或者出现在CIDFontType2CIDFont字典的(PDF 1.3)中。

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 . FontFile3 - Type1C- (PDF 1.2)Type 1 –紧凑字体格式(CFF)中表示的等效字体程序,如Adobe技术注释#5176 “紧凑字体格式规范”中所述 This entry may appear in the font descriptor for a Type1 or MMType1 font dictionary. 此项可能出现在Type1MMType1字体字典的字体描述符中。

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 . FontFile3 - CIDFontType0C- (PDF 1.3)Type 0 CIDFont程序,以紧凑字体格式(CFF)表示,如Adobe技术注释#5176, 紧凑字体格式规范中所述 This entry may appear in the font descriptor for a CIDFontType0 CIDFont dictionary. 此项可能出现在CIDFontType0 CIDFont词典的字体描述符中。

FontFile3 - OpenType - (PDF 1.6) OpenType® font program, as described in the OpenType Specification v.1.4 (see the Bibliography). FontFile3 - OpenType- (PDF 1.6)OpenType®字体程序,如OpenType规范v.1.4中所述 (请参见参考书目)。 OpenType is an extension of TrueType that allows inclusion of font programs that use the Compact Font Format (CFF). OpenType是TrueType的扩展,它允许包含使用紧凑字体格式(CFF)的字体程序。 ... ...

Thus, you should look for these keys in the FontDescriptor dictionary of the respective font, ie essentially what you did in your code with desc . 因此,您应该在相应字体的FontDescriptor字典中查找这些键,即本质上您在desc中的代码中所做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM