Replace paragraph while retaining text styles of certain words

Product: PDFNet-node

Product Version: 9.2.0-1

Please give a brief summary of your issue:
Replace whole paragraph while maintaining text style of words

Please describe your issue and provide steps to reproduce it:
I need to replace certain words from a PDF in a nodejs backend. I initially tried using the addString method of content replacer to replace it directly. However, I encountered a problem that when the new word is longer than the replaced word, the line can go beyond the page layout.

To solve this, I extracted the whole paragraph of the relevant word, created new text and replaced the whole paragraph with the addText function. Works well except that the style of the special words (bold, italic) are lost. So, I kept a record of the word index and after the paragraph replacement, went back, put a white rectangle on top the replaced word, and placed a word with the new style on top of it.

The problem is I communicate with frontend client that allows it to replace content multiple times. So, when I try to extract the text again, duplicate words show up for the special words. I am creating the paragraphs by looking at the difference in height between lines and putting them in the same paragraph (using paragraph id gave me duplicate paragraph with ____, ____ values).

I tried using the e_remove_hidden_text flag for text extractor with no success to remove the duplicate text.

How can I replace words (or paras) so that the replaced content is contained in the layout, style of words is preserved, and duplicate text is removed? Either a solution to my problem or a different approach will be appreciated. Thanks!

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Thank you for contacting us about this. You mentioned the following:

Just to clarify, what sort of rectangle did you place here? If you could provide us with an example document along with the steps you take in code (perhaps a working sample project) that would help us understand this issue a bit better.

Thank you in advance.

Hi, apologies for the delayed response. I am simply drawing a white background rectangle on top of the special word before writing a new word with the original style and finally replacing the word again (as not doing so seems to give me unreliable result). Here is the relevant part of my code (typescript). Specialword is the word corresponding word I record earlier with its style and font.

const specialWord = specialWords.find(w => w.ind === wordInd);

         if (specialWord) {

            const { x1, y1, x2, y2 } = await word.getBBox();

            const style = await word.getStyle();

            const fontName = specialWord.fontName || await style.getFontName();

            const fontSize = specialWord.fontSize || await style.getFontSize();

            // draw white background over previous word

            let element = await builder.createRect(x1, y1, x2-x1, y2-y1);

            let gState = await element.getGState();

            await element.setPathFill(true);

            await gState.setFillColorWithColorPt(await PDFNet.ColorPt.init(1, 1, 1, 0));

            await writer.writeElement(element);

            // write the new word with original style
             const font = await PDFNet.Font.createFromName(doc, fontName, '');

            element = await builder.createTextBeginWithFont(font, fontSize);

            await element.setPathFill(true);

            gState = await element.getGState();

            await gState.setFillColorSpace(await PDFNet.ColorSpace.createDeviceRGB());

            await gState.setFillColorWithColorPt(specialWord.type.includes('tag') ? await PDFNet.ColorPt.init(18/255, 89/255, 161/255) : await PDFNet.ColorPt.init(0, 0, 0));

            writer.writeElement(element);

            element = await builder.createUnicodeTextRun(wordStr);

            await element.setTextMatrixEntries(1, 0, 0, 1, x1, y2 - fontSize);

            writer.writeElement(element);

            writer.writeElement(await builder.createTextEnd());

         
            // replace words in that area to remove duplicates
            const replacer = await PDFNet.ContentReplacer.create();

            await replacer.addText(new PDFNet.Rect(x1+1, y1+2, x2-1, y2-2), ''); // remove the word in layer 1 // slightly smaller box

            await replacer.process(page);

          }

Can u share the full code ?

What is special word ?

I want to replace the firstname and lastname in the resume and the resumes are dynamic