How to get the font info from a PDF document?

Product: WebViewer

Product Version: v8.8.0

Please give a brief summary of your issue:
Not sure if this can be done by just using the WebViewer.
But the intention here is to match the annotation font with the document’s font.
So we would like to get the font-family, and font-size from the loaded PDF. Then we’ll use the information to set the annotation’s font.

Thank you for the support.

Hi @kim,

Fonts can be complicated in a PDF as there is a difference between embedded vs the fonts available in the host machine. You can extract embedded fonts using the node SDK or in WebViewer for example. Moreover, the fonts that are available in the PDF are not always available to the webfonts that are used for a FreeText. The current webfonts supported are available in the dropdown that appears when styling a freetext.

If your use case is to perform PDF text edits, then the fonts that are shown in the dropdown are the ones that are available for editing, and at this time this set cannot be augmented. We do however have plans to add support for more fonts in the future.

Best Regards,

Armando Bollain
Software Developer
Apryse Software Inc.

Hello @Armando_Bollain ,

Okay, so we can create the Font by extracting the indirectObj of SDFDoc?
Then from the created Font, we now have access to the Font.getFamilyName() method?
And we can use this when we set the font-family for the FreeTextAnnotation?

Thanks!

Hi @kim,

That is correct! Just bear in mind that you may find the font found in the PDF may not be supported in the webfonts used by the FreeText.

Best Regards,

Armando Bollain
Software Developer
Apryse Software Inc.

Hello @Armando_Bollain ,

Thanks for the quick response and confirming.
The link you provided was helpful!
Currently, we’re getting the font-name by using PDFNet.Font.getName(), although sometimes we noticed that it is inaccurate for some documents. I think this is what you meant by:

Moreover, the fonts that are available in the PDF are not always available to the webfonts that are used for a FreeText

Right? Like we uploaded a document that has Robotic or Lato, but the getName() method will return us "DejaVuSans".

Or is there any more reason for that?
And if it’s possible to get Robotic or Lato instead of DejaVuSans?
Also if it’s possible to get the font-size?

Thanks!

Hi @Armando_Bollain ,

I just noticed that after exporting (exportAnnotations) the FreeTextAnnotations and then importing them back (to view).
The Font and FontSizes did not persist, so they went back to their default value.

Do you know why that is?
Using WebViewer v8.8.0

Looking forward for your reply, thanks!

Hi @kim,

Sorry for the delay.

we uploaded a document that has Robotic or Lato , but the getName() method will return us "DejaVuSans" .

The issue will sometimes be that the font is not actually embedded in the PDF, and the viewer will then use a fallback font. I suspect that is why you are seeing DejaVuSans. You can read more about fonts here.

For your latest question, there is a workaround to generate an appearance for a free text, which should be generated with the font you expect. Here is the code:

  const { Annotations } = instance;

  Annotations.setCustomDrawHandler(Annotations.FreeTextAnnotation, (ctx, pageMatrix, rotation, options) => {
    options.originalDraw(ctx, pageMatrix, rotation);
  }, { generateAppearance: true });

The appearance should show the correct font when the doc is downloaded. However, we can also have a look at how you are setting font and fontsize to confirm the issue.

Armando Bollain
Software Developer
Apryse Software Inc.

Hello @Armando_Bollain ,

Ah that makes sense, thanks I only took a glance of the link you provided so “DejaVuSans” is one of the substitute font. I’ll read more about it later.

Thanks for the workaround, I’ll try this one.
We’re setting the annotation’s font using the FreeTextAnnotation.Font.

annot.Font = customStyle.fontName || DEFAULT_FONT

Where customStyle.fontName is the extracted embedded font (Arial | Cambria | DejaVuSans | Lato).
And DEFAULT_FONT = 'Arial'