我正在尋找PDF文件中的文本提取所有不同的字體名稱。我正在使用iTextSharp DLL,下面給出的是我的代碼。如何從PDF獲取文本的字體名稱?
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();
}
}
}
我得到的輸出是「Glyphless字體」無字體描述」爲每一個PDF文件作爲輸入的鏈接,輸入文件如下:
https://drive.google.com/open?id=0B6tD8gqVZtLiM3NYMmVVVllNcWc
PdfReader reader = new PdfReader(「C:/Users/agnihotri/Downloads/Test.pdf」); - 仔細檢查文件的路徑,這可能是問題,因爲代碼看起來不錯。我也強烈建議添加一些調試,如果試圖從互聯網上覆制粘貼,看看他們真的工作。 –