We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF font detection

I am looking for a way to identify Fonts from PDF using ASPOSE PDF.NET

Hi Pramod,


Thanks for contacting support.

As per your requirement, you may consider parsing through complete PDF file where you can get formatting information (i.e. Font, size, foreground color etc) for each TextFragment. Please visit the following link for further information on Search and get Text from all pages using Regular Expression

But is there any was where I can just get all the FONTS used in a PDF?

Hi Pramod,


You can enumerate the fonts from Resource collection of Page object and forms(XObject).

For details, Page object provides Resources collection and Resources provides Fonts collection. Enumerate all pages and then all fonts on the pages. Some fonts may be found in forms (XObjects), so also enumerate all the Forms in the Page.Resources and then enumerate xForm.Resources.Fonts. The XForm may contain another form inside the resources, so it is a recursion. Please check following code for the details.

static void Main(string[] args)<o:p></o:p>

{<o:p></o:p>

Document pdf = new Document(myDir + “Input.pdf”);<o:p></o:p>

Dictionary<string, string> fontNames = new Dictionary<string, string>();<o:p></o:p>

for (int i = 1; i <= pdf.Pages.Count; i++)<o:p></o:p>

{<o:p></o:p>

foreach (Aspose.Pdf.Text.Font font in pdf.Pages[i].Resources.Fonts)<o:p></o:p>

{<o:p></o:p>

if(!fontNames.ContainsKey(font.FontName))<o:p></o:p>

fontNames.Add(font.FontName,font.FontName);<o:p></o:p>

<o:p></o:p>

}<o:p></o:p>

if (pdf.Pages[i].Resources.Forms.Count > 0)<o:p></o:p>

ProcessXForms(pdf.Pages[i].Resources.Forms,fontNames);<o:p></o:p>

<o:p></o:p>

}<o:p></o:p>

foreach (string fontName in fontNames.Keys)<o:p></o:p>

{<o:p></o:p>

Console.WriteLine(“Font {0} on page resource”, fontName);<o:p></o:p>

}<o:p></o:p>

pdf.Dispose();<o:p></o:p>

}

public static void ProcessXForms(Aspose.Pdf.XFormCollection forms,Dictionary<string,string> fontNames)<o:p></o:p>

{<o:p></o:p>

foreach (Aspose.Pdf.XForm form in forms)<o:p></o:p>

{<o:p></o:p>

if (form.Resources.Fonts != null)<o:p></o:p>

{<o:p></o:p>

foreach (Aspose.Pdf.Text.Font font in form.Resources.Fonts)<o:p></o:p>

{<o:p></o:p>

if (!fontNames.ContainsKey(font.FontName))<o:p></o:p>

fontNames.Add(font.FontName,font.FontName);<o:p></o:p>

}<o:p></o:p>

// recursive call <o:p></o:p>

if (form.Resources.Forms.Count > 0)<o:p></o:p>

ProcessXForms(form.Resources.Forms, fontNames);<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

<o:p>Please feel free to contact us for any further assistance.</o:p>

<o:p>
</o:p>

<o:p>Best Regards,</o:p>