Run a script to enter text into polygon line contents when right clicking "complete"  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

Cambo
User
Posts: 4
Joined: Mon Nov 24, 2025 4:24 am

Run a script to enter text into polygon line contents when right clicking "complete"  SOLVED

Post by Cambo »

Hi,
We use PDF Exchange Editor for engineering drawings and are trying to create a "pipe" dialog that the user selects a "pipe" type, diameter and inputs a height above floor. The pipe is just a polygon line and the selected information will be input into the polygon line contents field. The dialog box and retrieving data is achievable however "listening" for a right mouse click to complete a polygon line is proving elusive and I'd love any guidance.
Question Part One.
Is it feasible to monitor for mouse clicks while a tool (polygon line) is being used? Reading the event listening on the Event listener API information it looks like its just for multimedia. (https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#eventlistener)
The goal is when the user clicks each segment of the polygon line data is added to a string. Then when polygon line "complete" (right mouse click) the text string is added to the polygon line contents.

Question Part Two (If part one isn't feasible and it needs to be done with a selection of polygon lines after completed)
I've had success in doing it after the polygon line is drawn using this.selectionAnnot script below. But it doesn't work every time. In the console I have to go back to the PDF window, click on the polygon line, then click on the comment then run the script. For some reason it doesn't add the contents every time
var annot = this.selectedAnnot;
for (var i = 0; i < aAnnots.length; i++) {
annot = aAnnots;
annot.contents = systemType + "-" + pipeND + "-" + material;
}

Thanks in advance for any assistance and guidance.

Cambo

ps
I'm a hobby developer and very inexperienced using .js.
User avatar
Sean - PDF-XChange
Site Admin
Posts: 869
Joined: Wed Sep 14, 2016 5:42 pm

Re: Run a script to enter text into polygon line contents when right clicking "complete"

Post by Sean - PDF-XChange »

Hi Cambo,

Regarding your first question, unfortunately, the short answer is no, not directly. PDF-XChange Editor’s JavaScript environment (AcroJS) is limited in event handling to:

a) Available Events: The Event object in AcroJS is mostly for document events (WillSave, DidSave, MouseUp, MouseDown) and multimedia events. There’s no built-in event hook for "in-progress" polygon drawing.

b) Polygon Drawing: While a user is actively placing vertices for a polygon, the polygon doesn’t exist yet as an Annot object. The annotation only exists once the drawing is finished (e.g., when the user double-clicks or right-clicks to finish).

So, you cannot track each click in real-time while drawing the polygon. You can only act on it after the annotation has been created.

Regarding your second question, since real-time tracking isn’t possible, the usual approach is:

1. Let the user draw the polygon.

2. Detect that the polygon has been created (or selected afterwards).

3. Add your contents programmatically.

In your current code:

var annot = this.selectedAnnot;
for (var i = 0; i < aAnnots.length; i++) {
annot = aAnnots;
annot.contents = systemType + "-" + pipeND + "-" + material;
}


there are a few issues:

- aAnnots is undefined in your snippet - you probably meant "this.selectedAnnots".
- "this.selectedAnnot" returns a single annotation, while "this.selectedAnnots" is an array.
- Inside your loop, annot = aAnnots is wrong; you need annot = aAnnots{i} (but with square brackets - I can't enter that here as it happens to be the short cut for italic text).
- You also need to refresh the selection after running the script sometimes.

Please try this code instead:

// Grab selected annotations
var selAnnots = this.selectedAnnots;

// Make sure there’s at least one selected
if (selAnnots && selAnnots.length > 0) {
for (var i = 0; i < selAnnots.length; i++) {
var annot = selAnnots{i};
// Only modify polygon annotations (optional)
if (annot.type === "Polygon") {
annot.contents = systemType + "-" + pipeND + "-" + material;
}
}
app.alert("Contents updated!");
} else {
app.alert("Please select a polygon annotation first.");
}


Note that annot.type === "Polygon" ensures you’re only modifying polygon lines, and app.alert is optional but useful for debugging.

Always select the polygon after drawing, so that the script knows which annotation to update. You could also add a button that runs this script after drawing in PDF-XChange Editor.

I hope that is of some assistance to you.

Kind regards,
Sean Godley
Technical Writer
PDF-XChange Co LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623
Cambo
User
Posts: 4
Joined: Mon Nov 24, 2025 4:24 am

Re: Run a script to enter text into polygon line contents when right clicking "complete"

Post by Cambo »

Hi Sean,
Thanks for the reply. That's what I was expecting re question one. Yet I was still hopeful for a long shot of it being possible. But alas. Not to be.
Re question two, that works much better now with the edits. Changing to square brackets and adjusting the annot.type to PolyLine worked a treat.

Thank you

Cambo

ps
Thanks for inadvertently letting me know that a double click finishes a polyline! I was right clicking every time. That's made my day!
User avatar
Sean - PDF-XChange
Site Admin
Posts: 869
Joined: Wed Sep 14, 2016 5:42 pm

Re: Run a script to enter text into polygon line contents when right clicking "complete"

Post by Sean - PDF-XChange »

Hi Cambo,

I'm glad we got that resolved for you.

Thanks and have a nice day,
Sean Godley
Technical Writer
PDF-XChange Co LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623
Mathew
User
Posts: 790
Joined: Thu Jun 19, 2014 7:30 pm

Re: Run a script to enter text into polygon line contents when right clicking "complete"

Post by Mathew »

Cambo wrote: Mon Nov 24, 2025 4:57 am
Question Part One.
Is it feasible to monitor for mouse clicks while a tool (polygon line) is being used?
The closest I've come to this is to wait until a markup is drawn and then get the most recent markup. It's taken me quite a while to get my head around async functions, but they are incredibly useful for things like this. I posted a messier way to do it with timeouts here - viewtopic.php?p=187486#p187486 but I think this way is better:

Here's an async function that will do that, and at the end I've shown a sample script showing how it could be used.
[EDIT] I updated it to be a bit more efficient after I initially posted it.

Code: Select all

/** simple async function to get new annotation
  *
  * @async
  * @param {object} doc - the document to get the new annotation from
  * @param {string} diaTxt - the text to show in the thermometer dialog
  * @returns {object|nothing} - the newly drawn annotation
**/

var getNewAnn = function() {
    // returns one annotation newer than testDate or nothing
    function getNext(doc,testDate) {
        const [annot] = doc.getAnnots(doc.pageNum, ANSB_ModDate) || [];
        if (annot && annot.creationDate > testDate) return annot;
    }
    
    return async function(doc,diaTxt) {
        const waitInterval = 100; //ms to wait between checks
        // set up the thermometer
        const thDia = app.thermometer;
        thDia.text = diaTxt;
        thDia.begin();
        
        let newAnn;
        const startWait = new Date();
        // wait...
        while (!thDia.cancelled && !(newAnn = getNext(doc,startWait))) {
            await app.timeOut(waitInterval);
        }
        // stop the thermometer
        thDia.end();
        return newAnn;
    };
}();

Code: Select all

// example usage
{
    // function to run when the polyline has been drawn
    // the getNewAnn function will provide the newest annotation
    const drawn = function(annot) {
        if (!annot) return; // probably cancelled
        app.alert(util.printf("You drew a %s!",annot.type));
    };
    // start the polyline tool
    app.execMenuItem('cmd.tool.annot.polyline');
    // start the async function to get the new annotation
    getNewAnn(this, "Draw a polyline! Or cancel.")
        .then(drawn) // function gets the newly drawn annotation
        .catch(e => console.println(e));
}
User avatar
Sean - PDF-XChange
Site Admin
Posts: 869
Joined: Wed Sep 14, 2016 5:42 pm

Re: Run a script to enter text into polygon line contents when right clicking "complete"

Post by Sean - PDF-XChange »

Hi Mathew,

Thanks as always for the excellent input here.

Kind regards,
Sean Godley
Technical Writer
PDF-XChange Co LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623