[tool] Merge annotations (comments) between documents

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

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

[tool] Merge annotations (comments) between documents

Post by Mathew »

This tool attempts to merge all comments in multiple pdf documents without overwriting more recent changes, and detecting conflicts (ie comments simultaneously edited). It's still a bit rough around the edges, so I don't recommend relying on it yet -- if anyone has time to test it and report back problems I'd really appreciate the help.

Background
If more than one person is adding comments to a pdf document, they either have to share a single document (but then it's locked when one person is editing it), or there needs to be a way to merge the comments in multiple documents. Other pdf software has set up subscription-based online editing to handle this, but then you need to trust your documents to their servers, and it's a proprietory solution. A simple brute force method is to export an FDF from all the documents, and import all of them into the document you want. But that will overwrite anything that was changed in the second document, and does not handle deleted comments.

Installation
Extract the zip and save in the Javascripts folder either in the application folder, or in %APPDATA%\Tracker Software\PDFXEditor\3.0\Javascripts
mergeAnnots v0.7.1.zip

Usage
After restarting PDFX-Change, it will add a button in the "Share" ribbon, (or in the Comments menu in the classic UI):
image.png
Open the documents you want to merge, and run the tool. It will open a dialog:
image(1).png
The left side lists documents that will be merged. If you ran the tool before, it will list the last documents you merged on the left, prefixed with [File] to indicate files that are not currently open. Below the list of files, it shows some brief information about the selected file.
On the right side is a list of the currently open documents. Use the < or > buttons to add or remove documents from the merge list on the left side.
The 'Browse...' button opens a dialog to open a file directly (if you didn't open it before running the tool). PXCE will pop up a security alert before allowing the script to open files. You can stop this for a particular file, directory or drive (I have a server mapped to a drive letter, and under Preferences > Security I added the drive letter as a trusted folder.)


At the top left is a button to set the merge settings. For now, I recommend keeping "Show merge conflicts dialog for all merges" so you can see what it's going to do before the merge happens:
image(2).png
Merge Data file
The script can save a data file in the document to improve the speed and accuracy of merges. A saved data file is a file attachment titled 'commentMergeData.txt' that allows the following improvements:
  1. Keep track of which annotations have been deleted: If there is no data file, the script will assume that if an annotation is missing (i.e. deleted) in one document, that it is new in another document, and will add it back (so to delete an annotation, you'd need to manually delete it in all documents before merge)
  2. Determine if an annotation has been updated since the previous merge: If there's no merge file, it assumes everything was updated and compares modification dates to all other files. This slows the merge, but if only the modification dates differ, it won't update the annotations in PDFX-Change before build 388.
  3. (in PDFX-Change before build 388) Keep track of modification dates: When the script adds a missing annotation, it can't set the modification date, so that annotation will appear newer in the next merge; there are cases where this could lead to overwriting changes in another pdf during a subsequent merge. (If only the modification dates differ, it won't update the annotations in PDFX-Change before build 388.)
  4. Optionally save a log of changes.
Conflicts
Depending on what the merge settings are, a Merge Conflicts dialog will show before the merge starts:
image(3).png
For all merges, the script finds the most recently modified annotation, and sets that document as the source for that annotation. Under Merge it will show ?? if there's no default action for that conflict in the settings. You'll need to either set it to Skip or >>>>> (Use Source) to proceed with the merge.
The buttons across the top allow you to set what happens to that annotation in the merge:
  • Skip the merge for that annotation,
  • Use Source apply the source annotation change to the destination, or
  • Change Source move the currently listed destination document to the 'source' (ie revert to an older version)
At the bottom, there is more detail about the currently selected annotation, and buttons to show the annotations.


Limitations/todo
This is a tough nut to crack. Handling the various conflicts is difficult to figure out (for my brain), and there are quite a few aspects of pdfs that are opaque to javascript. I wanted this tool to be able to handle merging more than just two documents at the same time: This makes handling conflicts particularly difficult.
  • I've only tested this in PDFX-Change build 391.
  • If the documents have different number of pages, the script will fail! I don't know how to know if pages have been moved, inserted, or deleted - the script currently assumes all page numbers between the documents match. I may try something like adding a hidden marker on each page to identify it, but I don't know what the best approach is. There may be some internal identifier to pages that I don't know, or a way to identify them; if you know of a way, please let me know.
  • There's no built-in way for javascript to set which comments are in front of the others (ie their z-index). One can work around this by temporarily moving annotations off the page, and back on in the desired order, but haven't added it to this tool yet.
  • JavaScript can't add stamps the way it can for other annotations. If you try, the application will look through the currently installed stamps, and try to add the same stamp. This is OK unless the stamp isn't installed on the merging computer or is a pasted image (in which case it will fail) or is a dynamic stamp (in which case it will pop up the dialog associated with that stamp instead of just copying it). I've made a workaround (that requires the source document be saved) copies the whole page, moves the stamp to the desired page and deletes the copied page; but watch out: It requires a registered copy of PXCE! If you want to disable it, edit line 666 and set to false:

    Code: Select all

      let APPREGISTERED = true; // will only try to use copyStamp function if registered
  • There's currently no option whether to restrict merging comments (ie by author, locked, status, etc).
  • If two annotations have the same name on the same page number, it will assume they are the same annotation. This will only be a problem if someone manually sets the annotation name.
  • The script doesn't currently handle form fields. It could, but I think it would need a 'diff' algorithm to deal with changes to text fields. That could be helpful in dealing with merge conflicts in text annotations also.
  • The log is just pasted onto the end of the merge data file and only holds onto one merge. It could be set up to use as a way to revert changes after a merge.
  • I'm still fiddling with this tool, so haven't set up a translation data file yet, although the script can handle one.
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Tue Jul 08, 2025 10:11 pm, edited 10 times in total.
KD952
User
Posts: 109
Joined: Mon Feb 13, 2023 6:13 am

Re: [tool] Merge annotations (comments) between documents

Post by KD952 »

:shock: Wow!
Now we know where you have been for the last few months. Working hard.
Kind regards,
Daniel
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 11385
Joined: Wed Jan 03, 2018 6:52 pm

Re: [tool] Merge annotations (comments) between documents

Post by Daniel - PDF-XChange »

Hello, KD952

Indeed, this looks quite powerful, thank you once again for your hard work Mathew!

Kind regards,
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
User avatar
David.P
User
Posts: 1652
Joined: Thu Feb 28, 2008 8:16 pm

Re: [tool] Merge annotations (comments) between documents

Post by David.P »

:shock: Mathew must be kiDdiNg!

This is unbelievable :!:

wow.png
You do not have the required permissions to view the files attached to this post.
David.P
PDF-XChange Pro
Mathew
User
Posts: 608
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Merge annotations (comments) between documents

Post by Mathew »

... i think it was a bit too soon to upload this: I've run into quite a few bugs :oops: I'll upload an update when I've got them squashed.
Mathew
User
Posts: 608
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Merge annotations (comments) between documents

Post by Mathew »

OK, I think it's working in version 0.7. Probably some other bugs lurking in there so still needs some testing. I updated the file in the first post also.
mergeAnnots v0.7.zip
Changes in v0.7
  • new property for state prior to merge,
  • fix some state --> action mappings,
  • fix saving updated modification dates to data file,
  • move to Comments menu in classic UI,
  • add file info to first dialog
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Tue Jul 08, 2025 10:12 pm, edited 1 time in total.
User avatar
Dimitar - PDF-XChange
Site Admin
Posts: 2315
Joined: Mon Jan 15, 2018 9:01 am

Re: [tool] Merge annotations (comments) between documents

Post by Dimitar - PDF-XChange »

Thanks, Mathew.

We appreciate your effort.
Mathew
User
Posts: 608
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Merge annotations (comments) between documents

Post by Mathew »

A bug was preventing the tool from running. Fixed in this minor update.
mergeAnnots v0.7.1.zip
You do not have the required permissions to view the files attached to this post.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 11385
Joined: Wed Jan 03, 2018 6:52 pm

[tool] Merge annotations (comments) between documents

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