c# - 如何从PDF中获取文本的字体名称?

标签 c# itext

我希望提取 PDF 文件中文本的所有不同字体名称。我正在使用 iTextSharp DLL,下面给出的是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text.pdf.parser;
using iTextSharp.text.pdf;

namespace GetFontName
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader reader = new PdfReader("C:/Users/agnihotri/Downloads/Test.pdf");
            HashSet<String> names = new HashSet<string>();
            PdfDictionary resources;
            for (int p = 1; p <= reader.NumberOfPages; p++)
            {
                PdfDictionary dic = reader.GetPageN(p);
                resources = dic.GetAsDict(PdfName.RESOURCES);
                if (resources != null)
                {
                    //gets fonts dictionary
                    PdfDictionary fonts = resources.GetAsDict(PdfName.FONT);
                    if (fonts != null)
                    {

                        PdfDictionary font;

                        foreach (PdfName key in fonts.Keys)
                        {
                        font = fonts.GetAsDict(key);
                        string name = font.GetAsName(iTextSharp.text.pdf.PdfName.BASEFONT).ToString();

                            //check for prefix subsetted font

                        if (name.Length > 8 && name.ToCharArray()[7] == '+')
                        {
                        name = String.Format("%s subset (%s)", name.Substring(8), name.Substring(1, 7));

                        }
                        else
                        {
                                //get type of fully embedded fonts
                        name = name.Substring(1);
                        PdfDictionary desc = font.GetAsDict(PdfName.FONTDESCRIPTOR);
                        if (desc == null)
                        name += "no font descriptor";
                        else if (desc.Get(PdfName.FONTFILE) != null)
                        name += "(Type1) embedded";
                        else if (desc.Get(PdfName.FONTFILE2) != null)
                        name += "(TrueType) embedded ";
                        else if (desc.Get(PdfName.FONTFILE3) != null)
                        name += name;//("+font.GetASName(PdfName.SUBTYPE).ToString().SubSTring(1)+")embedded';
                        }

                        names.Add(name);
                        }
                    }
                }
            }
            var collections = from name in names
            select name;
            foreach (string fname in collections)
            {
            Console.WriteLine(fname);
            }
            Console.Read();

        }
    }
}

对于每个作为输入的 pdf 文件,我得到的输出是“Glyphless Font”没有字体描述符。输入文件的链接如下:

https://drive.google.com/open?id=0B6tD8gqVZtLiM3NYMmVVVllNcWc

最佳答案

我已在 Adob​​e Acrobat 中打开您的 PDF,并查看了字体面板。这是我看到的:

enter image description here

您有一个嵌入的 LiberationMono 子集,这意味着字体名称将作为 ABCDEF+LiberationMono 存储在文件中(其中 ABCDEF 是一系列 6 个随机但唯一的字符),因为该字体是子集。请参阅What are the extra characters in the font name of my PDF?

现在让我们看一下在 iText RUPS 中打开的同一文件:

enter image description here

我们找到/Font对象,它有一个/FontDescriptor。在 /FontDescriptor 中,我们找到了 /FontName,其格式符合我们的预期:BAAAAA+LiberationMono

既然您知道在哪里查找该名称,您就可以调整您的代码了。

关于c# - 如何从PDF中获取文本的字体名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37814245/

相关文章:

c# - WebMethod 中的 CacheDuration 被忽略

c# - itext ColumnText 忽略对齐

c# - 在 webapi 方法中获取主机名

c# - .NET 中的双重检查锁定需要什么样的 'volatile' 操作

c# - 类似于 C# 的 ERB 库

c# - 这个C#函数组合方法是如何工作的?

java - 使用itext在亚马逊s3上存储pdf文件

itext - PdfWriter 和事件

jasper-reports - 用最新的 iText 7.0.1 替换 JasperReport iText 2.1.7

java - 使用 iText java 创建的带有水印图像的 PDF 文件