'How can I extract the Fonts used in a PDF file via PDFSharp?
I am trying to extract the fonts used in a PDF document via PDFSharp (http://www.pdfsharp.net/).
Unfortunately I am not able to do so since I keep on getting strange results and I don't know how to interpret them. In the assembly I have seen classes like PdfFontTable etc, but they are all internal. When decompiling the PdfResources class again everything related to Fonts is internal.
I have tried to access the Resources to get the fonts as:
var reader = Reader.Open(stream, PdfDocumentOpenMode.InformationOnly);
foreach (var page in reader.Pages)
{
var resources = page.Resources;
var fonts = resources.Elements.GetValue("/Font");
}
But that gives me an incomprehensible response:
Is there a way to extract the list of fonts used just like I see them in Adobe Acrobat Reader?
Solution 1:[1]
There is no direct API way to query the fonts. But it is possible to query the internal document data structures. The following code works and returns a list with fonts.
List<string> fonts = new List<string>();
PdfDocument pdfDoc = PdfReader.Open("YourFileName.pdf");
foreach (PdfObject obj in pdfDoc.Internals.GetAllObjects())
{
if ("dictionary" == obj.Internals.TypeID)
{
for (int i = 0; i < ((PdfDictionary)obj).Elements.Count; ++i)
{
if (((PdfDictionary)obj).Elements.ContainsKey("/BaseFont"))
{
PdfItem item = ((PdfDictionary)obj).Elements.GetValue("/BaseFont");
string fontname = item.ToString();
int idx = fontname.IndexOf('+');
if (idx > 0)
{
fontname = fontname.Substring(idx + 1).Trim();
if (!fonts.Contains(fontname))
fonts.Add(fontname);
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tichy |



