#904·pdfkit

Ability to get real X position after adding text with continued option

Author: gohaberegCreated Jan 11, 2019Updated Jul 2, 2026

Question

Can I somehow get current X position after .text(sometext, {continued: true} call?

Description

I want to add highlights to some of the parts of text. I have a paragraph splitted to several parts so highlight should be added to some of them. To add highlight I need to know X coordinate of the word bounding rect. The problem is the doc.x is always pointed to the page left side but actual position is at the end of the previous part.

Code sample

javascript
const paragraph = ['Hi!', 'I want to higlight next sentense.', 'This one.'];

paragraph.forEach((sentense, i) => {
  if (i === 1) {
    doc.highlight(
      x, // this one I don't know
      doc.y, 
      doc.widthOfString(sentense),
      doc.heightOfString(sentense)
    );
 }

  doc.text(sentense, {
    continued: i !== paragraph.length - 1
  });
});