Get List of the Fonts from the PDF File using Aspose.PDF for .NET

Hello,
i try to execute the example show in the documentation. I use the aspose.pdf-11.1.0.jar
//create PdfExtractor object
PdfExtractor pdfExtractor = new PdfExtractor();
//bind input PDF file
pdfExtractor.bindPdf(“easya.pdf”);
//get fonts list
Set resSet = pdfExtractor.getFonts();
//iterating over the elements in the set
Iterator it = resSet.iterator();
while (it.hasNext()) {
//get and display element
Object element = it.next();
System.out.println(element);
}
but the method getFonts() not exits.
How i can resolve?
Thanks a lot.

Hi Filippo,

Thanks for your inquiry. You can use the following code snippet to get fonts from a PDF document. It will help you to accomplish the task.

Furthermore, I am afraid the getFonts() method is not implemented in PdfExtractor. I would appreciate it if you could point me to the documentation so that I can update it as well. We are sorry for the inconvenience.

public static void GetFontsList() {
    Document pdf = new Document(myDir + "test_pdfextractor.pdf");

    Map<String, String> fontNames = new HashMap<String, String>();

    for (int i = 1; i <= pdf.getPages().size(); i++) {
        for (com.aspose.pdf.Font font: (Iterable<com.aspose.pdf.Font>) pdf.getPages().get_Item(i).getResources().getFonts()) {
            if (!fontNames.containsValue(font.getFontName())) {
                fontNames.put(font.getFontName(), font.getFontName());
            }
        }

        if (pdf.getPages().get_Item(i).getResources().getForms().size() > 0) {
            ProcessXForms(pdf.getPages().get_Item(i).getResources().getForms(), fontNames);
        }
    }

    Set fonts = fontNames.keySet();

    for (Iterator font = fonts.iterator(); font.hasNext();) {
        System.out.println("Font found in the PDF document: " + (String) font.next());
    }

    pdf.dispose();
}

public static void ProcessXForms(com.aspose.pdf.XFormCollection forms, Map<String, String> fontNames) {
    for (com.aspose.pdf.XForm form: (Iterable<com.aspose.pdf.XForm>) forms) {
        if (form.getResources().getFonts().size() != 0) {
            for (com.aspose.pdf.Font font: (Iterable<com.aspose.pdf.Font>) form.getResources().getFonts()) {
                if (!fontNames.containsValue(font.getFontName())) {
                    fontNames.put(font.getFontName(), font.getFontName());
                }
            }
            // recursive call
            if (form.getResources().getForms().size() > 0) {
                ProcessXForms(form.getResources().getForms(), fontNames);
            }
        }
    }
}

Best Regards,

@filippoangileri

We are excited to share with you that you can now get all fonts from a PDF document using Aspose.PDF for .NET.