Using JavaScript to place the annotation to clipboard first

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

JCatsuki
User
Posts: 2
Joined: Wed Apr 01, 2026 3:36 pm

Using JavaScript to place the annotation to clipboard first

Post by JCatsuki »

Hello!
Im a QS who uses PDF-XChange as a takeoff app.
My coding skill is pretty much non existent so I heavily rely on this forum and of course, ai.
Im trying to learn how to code those on my free time to help me understand coding more.

What i want to ask is if there's a way to use JS to place a predefined annotation (oval or rectangle drawing tool) in the clipboard first instead of placing it anywhere on the pdf?
Im trying to make a Counting Tool similar to most takeoff apps (idk know if there's one here already), or something a work around.
Since PDF-XChange has a "paste to where the mouse is pointing" feature, i want that as a work around.

Thanks in advance.
Mathew
User
Posts: 790
Joined: Thu Jun 19, 2014 7:30 pm

Re: Using JavaScript to place the annotation to clipboard first

Post by Mathew »

JCatsuki wrote: Thu Apr 02, 2026 12:25 pm Hello!
Im a QS who uses PDF-XChange as a takeoff app.
My coding skill is pretty much non existent so I heavily rely on this forum and of course, ai.
Im trying to learn how to code those on my free time to help me understand coding more.

What i want to ask is if there's a way to use JS to place a predefined annotation (oval or rectangle drawing tool) in the clipboard first instead of placing it anywhere on the pdf?
Im trying to make a Counting Tool similar to most takeoff apps (idk know if there's one here already), or something a work around.
Since PDF-XChange has a "paste to where the mouse is pointing" feature, i want that as a work around.

Thanks in advance.
Hi JCatsuki,
Javascript does not have access to the clipboard in PXCE. Is this something that can be done with a stamp? If you want javascript to place it, js can add a stamp from your predefined stamps using doc.addAnnot().
- Mathew.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 12611
Joined: Wed Jan 03, 2018 6:52 pm

Using JavaScript to place the annotation to clipboard first

Post by Daniel - PDF-XChange »

:)
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
JCatsuki
User
Posts: 2
Joined: Wed Apr 01, 2026 3:36 pm

Re: Using JavaScript to place the annotation to clipboard first

Post by JCatsuki »

Mathew wrote: Thu Apr 02, 2026 9:08 pm Hi JCatsuki,
Javascript does not have access to the clipboard in PXCE. Is this something that can be done with a stamp? If you want javascript to place it, js can add a stamp from your predefined stamps using doc.addAnnot().
- Mathew.
Thanks for the answer, I am somewhat aware of that fact. I just want to be sure since since it's the ai who first told me that.
And yes, the stamp is the closest tool to the counting tool of takeoff apps, but i was told that stamps, by default, bloats the pdf file.
So im looking for ways to simulate the counting tool. I dont need a counting script though, PXCE (or most pdf editors) natively counts the annotations depending on how the user sorts it.
Here what ive been thinking and done so far:
  1. Make a set of very simple stamps so it wont bloat my files too much. [Simplest approach, i think]
  2. Use any stamp for counting, then select all those stamps, use a JS to convert/replace said stamps to simple drawing annot (like oval or rectangle) complete with needed parameter/properties. This one was actually suggested by Gemini. But this isnt a counting tool, this more like a batch correction/revision tool. And so i repurposed this approach into that tool. [REVISED/REPURPOSED]
  3. Next is to make a stamp with a certain tag in one of it parameter. Whenever I place that stamp on the pdf a JS will detect the tag of that stamp then replace that stamp with again a drawing annot of predefined properties. Basically the main idea is just to borrow the stamp's way of placing the annot. But since i lacked expertise on coding, rellying on ai wont really help me that much. [FAILED]
  4. Which led me to this approach. Since PXCE has a feature of pasting to where the mouse is, I decided to try making a JS that lets you create an oval/rect annot then instead of placing it somewhere on the pdf (like what two AIs suggested to me), it will place it in the clipboard instead. This way, it skips to the part were to I need to find the created annot on the pdf then select>copy>delete the annot. But as what you and (2) ai have said, "Javascript does not have access to the clipboard in PXCE". [FAILED]
Do you have any suggestion? Im happy to take even negative responses as long as I learn from it.
.
PS. I never expected you were the one who would reply to my post. Im actually very thankful to one of the JS you've made, the annotation report, the one that counts annotations then consolidates it. I did tweak it to my needs though. I also did try making something of a Region Tool that is based on your JS, it basically just counts all the annots inside an highlighted area. Great if the client wants to determine how many devices are there in a certain area.
Mathew
User
Posts: 790
Joined: Thu Jun 19, 2014 7:30 pm

Re: Using JavaScript to place the annotation to clipboard first

Post by Mathew »

JCatsuki wrote: Fri Apr 03, 2026 6:54 am Do you have any suggestion? Im happy to take even negative responses as long as I learn from it.
Hi Catsuki,

I'm thinking the free highlight tool could be very useful for counting. It has the benefit of being quick to place, customizable (you can set custom subjects and colors to ease counting different types), and is a simple annotation rather than a stamp. Very happy to hear you found one of my tools useful, and it's good to hear that you found ways to customize it. Open Source in action :)

I went off on a bit of a tangent with the bloat issue, so apologies in advance for being off-topic:

I was unaware that stamps bloat files. In theory, if they are the same stamp, PXCE should recognize that fact and actually use less space because it can refer to the same stamp instead of duplicating the annotation data. I did a simple test:
  1. copying a very simple annotation (rectangle) and a stamp made of that same rectangle 1000 times (using javascript), and
  2. duplicating the same using the Edit > Duplicate tool (also 1000 times).
image.png
The copied stamp file is (as you said) 30% larger - my guess is because of a larger overhead for storing stamps than annotations(?) The duplicate tool is vastly more efficient for both.

However, IMO, PXCE could keep track of the stamps used in a file, and if that stamp is reused without any changes, it could do the same as it does for the Duplicate command. There may be a feature request related to this floating around somewhere, because I vaguely remember this coming up in the past. (Actually, doing this for the js doc.addAnnot method may be simpler because javascript cannot modify stamps.)

I did also try a brief test just placing the same stamp over and over, but didn't have the patience to do 1000 of them - I did 100 and it seems that the program is adding the same data over and over (similar to test 1 above), rather than the more efficient approach used by Duplicate.

Big picture: If the stamps are small, the amount of bloat is also very small. For 1000 stamps, even in the un-optimized approach, it only added (305k - 237k = 68k using stamps) -- ie only 68 bytes (0.068k) per stamp. I'm constantly dealing with vastly more bloated pdfs (ie from Autocad or Revit where hatch patterns are used at small scales).

Coding: Another approach, that I'm sure you can get AI to expand on, is to use an async function to replace a placed stamp or other annotation. See this post for an example script: viewtopic.php?p=201993#p201993

- Mathew.
You do not have the required permissions to view the files attached to this post.