How to get font encoding?

WebViewer Version: 11.5.0

Do you have an issue with a specific file(s)?
No

Can you reproduce using one of our samples or online demos?
No

Are you using the WebViewer server?
No

Does the issue only happen on certain browsers?
No

Is your issue related to a front-end framework?
No

Is your issue related to annotations?
No

Please give a brief summary of your issue:
(Think of this as an email subject)

I can’t get the encoding of any font on any text element.

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

The promise of getEncoding() never resolves

console.log(await (await gState.getFont()).getEncoding());

Please provide a link to a minimal sample where the issue is reproducible:
N/A

1 Like

Hello, I’m AiRon, an automated tech support bot :robot:
While you wait for one of our customer support representatives, I can help you with your question.


It seems like you’re trying to retrieve the font encoding for text elements using WebViewer’s JavaScript API. The method getEncoding() is part of the font object in Apryse SDK, which should return a promise resolving the encoding of the font. If the promise isn’t resolving, there might be an issue with how the font object is being retrieved or how the promise is handled.

Some things you can try:

  • Ensure that the font object is correctly retrieved from the graphics state. Verify that gState.getFont() is returning a valid font object.

  • Check if the font being used actually has an encoding. Some fonts might not have explicit encoding information available.

  • Review your implementation to ensure that await is correctly used in an async function, and that there are no errors being silently ignored.

  • Consider using console.log or breakpoints to debug and check the value of gState and the result of gState.getFont() before calling getEncoding().



Here are some resources that might help you:


Hopefully, this helps you solve your problem while you wait for a human to get back to you.

1 Like

Hello Peter,

Thank you for contacting WebViewer Forums.

Can you provide steps to replicate the issue where getEncoding() does not resolve? Please see this documentation on how to access the element’s graphic state here: Read, Write, Edit PDF Page Content: JavaScript Library | Apryse documentation

Regards,
Luke

1 Like

A_Day_in_the_Life_of_Your_Data.pdf (1.2 MB)

after loading the PDF:

const reader = await PDFNet.ElementReader.create();
await reader.beginOnPage(1);
let element: Core.PDFNet.Element;
while ((element = await reader.next()) !== null) {
  switch (await element.getType()) {
    case PDFNet.Element.Type.e_text:
      const font = await(await element.getGState()).getFont();
      console.log(
        await font.getEncoding()
      );
      break;
    default:
      break;
  }
}

What is wrong with my code? This promise never resolves and this happens with every PDF not just this one but I added it for example.

1 Like

Hello Peter,

Thank you for your reply.

You may want to pass in the page of the document from the Viewer into the reader. I was able to get the console log after doing so. For example:

const doc = await documentViewer.getDocument().getPDFDoc();
const firstPage = await doc.getPage(1);
const reader = await PDFNet.ElementReader.create();
await reader.beginOnPage(firstPage);

Regards,
Luke

1 Like