Updating textbox after changing it with JavaScript Tool  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

HQue
User
Posts: 16
Joined: Fri Mar 14, 2025 2:34 pm

Updating textbox after changing it with JavaScript Tool

Post by HQue »

Hey there,

I wanted to add a more comfortable way to add symbols to a PDF - a bit like in word because Alt+code or copy paste from the character table is a bit tedious.
I saw that this has been already a thing in the past.
viewtopic.php?t=22979

I added a custom tool using JavaScript that does that
But when I have selected a text field with the cursor inside the text the symbol is only added after I deselect the text (sometimes when I wait too long with the deselect it's not added at all).
When the text is just selected it works without any problem

That's the function:

Code: Select all

function addGreekLetter(doc) {

    var textAnnot = doc.selectedAnnots[0]

    const originalText = textAnnot.contents;
    const symobToBeAdded = greekLetterToBeAddedGlobal 
    const newText = originalText + symobToBeAdded;

    textAnnot.setProps({
        contents: newText,
    });
}

Could anyone help me how to achieve this?
Like updating the text/deselecting it automatically?

I'd be happy to share the code here with anyone who find it useful when that works.

Best,

Niklas
Mathew
User
Posts: 573
Joined: Thu Jun 19, 2014 7:30 pm

Re: Updating textbox after changing it with JavaScript Tool  SOLVED

Post by Mathew »

HQue wrote: Thu May 22, 2025 7:51 am
But when I have selected a text field with the cursor inside the text the symbol is only added after I deselect the text (sometimes when I wait too long with the deselect it's not added at all).
When the text is just selected it works without any problem

Could anyone help me how to achieve this?
Like updating the text/deselecting it automatically?
Hi Niklas,

The only workaround I can think of is exactly what you suggested: use app.execMenuItem() to activate the selection tool, which will deselect the text inside the markup:

Code: Select all

function addGreekLetter(doc) {

    const textAnnot = doc.selectedAnnots[0]

    const originalText = textAnnot.contents;
    const symobToBeAdded = greekLetterToBeAddedGlobal 
    const newText = originalText + symobToBeAdded;
    // we have a handle to the annotation, so can deselect it
    app.execMenuItem('cmd.tool.editAnnots', doc);

    textAnnot.setProps({
        contents: newText,
    });
}
HQue
User
Posts: 16
Joined: Fri Mar 14, 2025 2:34 pm

Re: Updating textbox after changing it with JavaScript Tool

Post by HQue »

Lovely, that works!
Thank you Mathew!
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Updating textbox after changing it with JavaScript Tool

Post by Stefan - PDF-XChange »

:)