Element.updateTextMetrics() incorrectly inserts identity Tm, overriding intended Td position

WebViewer Version: 11.5.0

Do you have an issue with a specific file(s)?
No but I can attach an example
8mb_minified.pdf (18.3 KB)

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:

Element.updateTextMetrics() incorrectly inserts identity Tm, overriding intended Td position

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

Description and Steps to Reproduce

I’m modifying text elements using the WebViewer full API. The general approach is:

  1. Remove an existing textElement.
  2. Create a new one with new text.
  3. Reposition it based on the original bounding box.
  4. Call updateTextMetrics() to finalize the geometry.

This works in many cases, but on PDFs that use Td positioning (rather than Tm), the output is incorrect. Specifically, updateTextMetrics() inserts an identity Tm (1 0 0 1 0 0), which overrides the effect of the preceding Td and causes the text to render at the origin instead of the correct position.


Input Code Example

const bbox = oldElement.getBBox();
const matrix = await Core.PDFNet.Matrix2D.create(1, 0, 0, 1, bbox.x1, bbox.y1);
await newElement.setTextMatrix(matrix);
await newElement.updateTextMetrics();

Generated Output (Incorrect)

BT
42.52 727.045 Td
0 J
1 w
1 0 0 1 0 0 Tm         ← inserted by `updateTextMetrics()`
/F1 11 Tf
(GAVE HER ONE, GAVE HIM TWO...) Tj
ET

Expected Output

BT
42.52 727.045 Td
0 J
1 w
/F1 11 Tf
(GAVE HER ONE, GAVE HIM TWO...) Tj
ET

Why This Is a Problem

By inserting 1 0 0 1 0 0 Tm, updateTextMetrics() cancels the effect of the prior Td, causing the text to render at (0,0) instead of the correct position set by Td. This makes it impossible to rely on Td positioning once updateTextMetrics() is called, breaking previously valid layout code.

Please let me know if there’s a workaround because I can’t find a way to update text matrix correctly for all PDFs. Thanks!

Workaround Attempt

I tried the following to avoid calling updateTextMetrics() when the original element uses Td:

if (oldElement.hasTextMatrix()) {
    await newElement.updateTextMetrics();
}

But this fails for other PDFs, so it’s not a reliable solution across all documents.

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 encountering an issue with the updateTextMetrics() method, which inserts an identity Tm matrix and overrides the intended Td position. This behavior can indeed disrupt the positioning of text in your PDF when using the Apryse WebViewer SDK.

Some things you can try:

  • Instead of relying on updateTextMetrics(), manually handle the positioning by adjusting the text matrix and ensuring that the intended transformations are applied directly.

  • Use the setTextMatrix() method carefully to establish the desired position and transformation without calling updateTextMetrics() if it introduces undesired changes.

  • Check if any transformation matrices (Tm or Td) are already applied and consider the order of operations to maintain the intended transformations.

  • Consider logging or debugging the transformation matrices before and after applying them to ensure the correct transformations are being used.




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

Hi Peter,
My name is Christopher and I am a Support Engineer with the Apryse toolkit. I wanted to reach out and let you know that we are looking into your issue and I will reach out with additional information as soon as possible.

Thanks

1 Like

Hi Peter,
I spoke with my team regarding your issue, and before we get into the details of editing PDF content streams, it would be best if you could elaborate on your project requirements.
Why is replacing text important for you?
How do you determine what text to replace? Manual interaction? Or something automatic?
What if the new text is longer/larger than the original text (requires more space)?

Thanks

1 Like

Hi Christopher,

This issue is unrelated to my project. I’ve already implemented a workaround to handle the case. However, I’m reporting this because the function’s current behavior is incorrect and shouldn’t be working this way.

It’s the responsibility of the Apryse team to ensure the function properly handles all relevant cases. While I’ve closed the ticket on my end, I strongly recommend reviewing and updating the function logic accordingly. Whether or not you choose to address it is your call.

Thanks.

1 Like

Hey Peter,
Thanks for bringing this to our attention, we will review this

Thanks,

1 Like