Free Support Forum - aspose.com

Get fonts information from PDF document in C#/Java using Aspose.PDF

Hi Team,
Is this possible to get the font size and font name from the PDF document using aspose.PDF in java.please kindly help me to achieve the same.

Thanks & regards,
priyanga G

Hi Team,

Is this possible to get font size and font name for raster images using aspose.Pdf in java.
please kindly guide me to achieve the same.

Thanks & regards,
Priyanga G

@priyanga,

You can search text and retrieve font name and size of the PDF document. Please refer to this help topic: Search and Get Text from Pages of PDF Document. In reference to the font name and size of image’s text, Aspose.PDF for Java API does not OCR images. You can try Aspose.OCR for Java API for OCR raster images to retrieve the font name and size of the text. Please refer to this help topic: Extract the OCR Text. If this does not help, then please elaborate a bit more about the scenario and send us a sample PDF document.

Hi @imran.rafique,

Thanks for your support.

The previously mentioned topic is very helpful.But some document having two more fonts are attached .The output show the only one font name and size in the output .please kindly help me to get all the embedded fonts name list.

Please,find the below attachment.

source:sample1.zip (57.0 KB)

@priyanga,

You can retrieve all fonts from a PDF document as follows:
C#

string dataDir = @"C:\Pdf\test854\";
Document doc = new Document(dataDir + "sample1.pdf");
Aspose.Pdf.Text.Font[] fonts = doc.FontUtilities.GetAllFonts();
foreach (Aspose.Pdf.Text.Font font in fonts)
{
    Console.WriteLine(font.FontName);
}

Result:

TimesNewRomanPS-ItalicMT
TimesNewRomanPSMT
Calibri

Hi @imran.rafique,

Thanks for your feedback.

Could you please explain in java

Thanks & regards ,
priyanga G

@priyanga,

The getAllFonts method is not available in Aspose.PDF for Java API. We have logged a feature request under the ticket ID PDFJAVA-37763 in our issue tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

hi @imran.rafique,

Is this any update on this. please,let me know the status.

Thanks & regards,
priyanga G

@priyanga,

The linked ticket ID PDFJAVA-37763 has been resolved. Please download and try the latest version 18.5 of Aspose.PDF for Java API.

Java

Document doc = new Document("input.pdf");
Font[] fonts = doc.getFontUtilities().getAllFonts();
for (Font f : fonts)
{
    System.out.println(f.getFontName());
}

Hi @imran.rafique,

Thanks for your update.Is this possible to get font size from the PDF document .please ,once the feature is available kindly update the status.

Thanks & regards,
Priyanga G

@priyanga,

The save method of a Font instance, helps to save font in the ByteArrayOutputStream instance, and we can then call size method of ByteArrayOutputStream instance. Please try the code as follows:
Java

Document doc = new Document("input.pdf");
Font[] fonts = doc.getFontUtilities().getAllFonts();
for (Font f : fonts)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    f.save(baos);
    System.out.println(baos.size());
    System.out.println(f.getFontName());
}

HI @imran.rafique,

Thank you very much for your reply. The following line will show the null pointer exception.please, kindly help me to solve this issue>

f.save(baos);

Thanks & regards,
priyanga G

@priyanga,

We managed to replicate the null pointer error in our environment. An investigation has been logged under the ticket ID PDFJAVA-37798 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Hi @imran.rafique,

Please ,let me know any update on this status.

Thanks & Regards,
Priyanga G

@priyanga,

The linked ticket ID PDFJAVA-37798 has been resolved. Please download and try the latest version 18.6 of Aspose.PDF for Java API.

Hi @imran.rafique,

Thanks for your update. I am started to using the Aspose.words 18.6v.

Still,The following line will show the null pointer exception.please, kindly help me to solve this issue>

f.save(baos);

Thanks & Regards,
Priyanga G

@priyanga,

In reference to the linked ticket ID PDFJAVA-37798, please try with the latest version 18.6 of Aspose.PDF for Java API. The null pointer error does not occur in our environment. However, if you are using Aspose.Words API, then please send us the complete code.
Java

Document doc = new Document("C:\\Pdf\\test911\\sample1.pdf");
Font[] fonts = doc.getFontUtilities().getAllFonts();
for (Font f : fonts)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    f.save(baos);
    System.out.println(baos.size());
    System.out.println(f.getFontName());
}

Hi @imran.rafique,

Thanks for your quick response.

Here ,I have attached the code.Please,kindly help me to solve the issue.

source:asposefont.zip (350 Bytes)

Thanks & Regards,
Priyanga G

@priyanga,

We have tested your source PDF document and code with the latest version 18.6 of Aspose.PDF for Java API, and could not replicate the error. It is working fine.

Result:
22256
TimesNewRomanPS-ItalicMT
37508
TimesNewRomanPSMT
30276
Calibri

Dear Imran,

Thanks for your support and reply.

We didn’t understand the font size like 22256.

Please clarify the exact font size like integer or float.

Example : font size = 14.5

Regards,
Priyanga G