Hi Pramod,
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 the following code for details.
static void Main(string[] args)
{
Document pdf = new Document(myDir + "Input.pdf");
Dictionary<string, string> fontNames = new Dictionary<string, string>();
for (int i = 1; i <= pdf.Pages.Count; i++)
{
foreach (Aspose.Pdf.Text.Font font in pdf.Pages[i].Resources.Fonts)
{
if (!fontNames.ContainsKey(font.FontName))
fontNames.Add(font.FontName, font.FontName);
}
if (pdf.Pages[i].Resources.Forms.Count > 0)
ProcessXForms(pdf.Pages[i].Resources.Forms, fontNames);
}
foreach (string fontName in fontNames.Keys)
Console.WriteLine("Font {0} on page resource", fontName);
pdf.Dispose();
}
public static void ProcessXForms(Aspose.Pdf.XFormCollection forms, Dictionary<string, string> fontNames)
{
foreach (Aspose.Pdf.XForm form in forms)
{
if (form.Resources.Fonts != null)
{
foreach (Aspose.Pdf.Text.Font font in form.Resources.Fonts)
{
if (!fontNames.ContainsKey(font.FontName))
fontNames.Add(font.FontName, font.FontName);
// recursive call
if (form.Resources.Forms.Count > 0)
ProcessXForms(form.Resources.Forms, fontNames);
}
}
}
}
Please feel free to contact us for any further assistance.
Best Regards,