Null Error on GetTextString()

WebViewer Version: 11.5.0

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

Can you reproduce using one of our samples or online demos?
I think I get the same error as warnings in console on JavaScript PDF Text Editing Demo | Apryse WebViewer

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 get indirect call to null error when calling getTextString() on a 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)

I get this error

[ERROR] getTextString failed
Object { type: “JavascriptError”, message: “indirect call to null” }

When running this code

const pageTextElments: TextDataElement[] = [];
const { documentViewer, PDFNet } = pdfWebViewerInstance.Core;

const doc = documentViewer.getDocument();
const pdfdoc = await doc.getPDFDoc();
const reader = await PDFNet.ElementReader.create();
const page = await pdfdoc.getPage(pageNumber);

await reader.beginOnPage(page);

let element: Core.PDFNet.Element;
let elementIndex = -1;

while ((element = await reader.next()) !== null) {
  elementIndex += 1;

  console.log("Calling get type");
  let elementType;
  try {
    elementType = await element.getType();
  } catch (err) {
    console.error("[ERROR] element.getType failed", err);
    continue;
  }
  console.log("Called get type", elementType);

  switch (elementType) {
    case PDFNet.Element.Type.e_text:
    case PDFNet.Element.Type.e_text_end:
    case PDFNet.Element.Type.e_text_new_line: {
      console.log(`[INFO] Handling text element: index=${elementIndex}, type=${elementType}`);
      const textDataElement = new TextDataElement(elementIndex, elementType);

      if (elementType === PDFNet.Element.Type.e_text) {
        try {
          const text = await element.getTextString();
          console.log(`[DEBUG] getTextString() ->`, text);
          textDataElement.setElementText(text);
        } catch (err) {
          console.error("[ERROR] getTextString failed", err);
        }
      }

      pageTextElments.push(textDataElement);
      break;
    }

    case PDFNet.Element.Type.e_form: {
      console.log(`[INFO] Handling form element: index=${elementIndex}, type=${elementType}`);
      try {
        const xobj = await element.getXObject();
        console.log("[DEBUG] getXObject() ->", xobj);

        const formObjNumber = await xobj.getObjNum();
        console.log("[DEBUG] getObjNum() ->", formObjNumber);

        const formTextElments = await this._getFormTextElements(
          reader,
          elementIndex,
          pdfWebViewerInstance,
          formObjNumber
        );
        console.log(`[DEBUG] _getFormTextElements() returned ${formTextElments?.length ?? 0} elements`);
        pageTextElments.push(...formTextElments);
      } catch (err) {
        console.error("[ERROR] Failed while processing form element", err);
      }
      break;
    }
  }
}

console.log("[TRACE] Loop finished, ending reader...");

try {
  await reader.end();
  console.log("[TRACE] reader.end() finished");
} catch (err) {
  console.error("[ERROR] reader.end() failed", err);
}

try {
  await reader.destroy();
  console.log("[TRACE] reader.destroy() finished");
} catch (err) {
  console.error("[ERROR] reader.destroy() failed", err);
}

on the attached PDF
0000066.pdf (997.4 KB)

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 looks like you’re encountering an error when calling getTextString() on a text element using Apryse’s WebViewer. This issue could be due to a few possible reasons, such as the text element not being fully initialized, or the element type not supporting the getTextString() method.

Some things you can try:

  • Ensure that the element is indeed of a text type before calling getTextString(). You can do this by checking if elementType is PDFNet.Element.Type.e_text before attempting to get the text string.

  • Verify that the PDF document is not corrupted and that the text content is correctly embedded within the PDF.

  • Consider adding additional error handling to catch and log specific errors that occur when calling getTextString().

  • Attempt to reproduce the issue with a different PDF file to determine if the problem is file-specific.



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