Replacing All Existing Annotations (Java)

I would like to replace all annotations that exist in my PDF document. I'm attempting to do that by executing the following code...

// The PDF document w/ annotations
PDFDoc myDoc;
// The new annotations to be loaded into the PDF
FDFDoc newAnnotations;

// Iterate through all of the document's pages to first delete
// any / all annotations that already exist
for (int pageIdx = 1; pageIdx <= pdfDoc.getPageCount(); pageIdx++)
    Page currPage = myDoc.getPage(pageIdx);
    if (currPage.getNumAnnots() > 0)

// Load the new annotations into the
// document

After executing the above code my document contains 0 annotations. The above code works if the source PDF document doesn't already contain annotations. It appears to only not work when I'm first removing already existing annotations. Am I doing something wrong? Is there a better way to accomplish what I've described?

That code should definitely work fine. Does the issue you see only occur with certain files? What exactly is the issue?

The issue is that the .fdfmerge() call doesn’t seem to work after you’ve manually removed all previously defined annotations. For example, say the PDF already has 10 annotations and the incoming XFDF has 5 annotations. After executing my example code, you would expect that the PDF would only contain the 5 annotations found in the XFDF. Instead it has 0.

This issues is not isolated to a single PDF document. I’ve tried it on a couple different files with the same result. This may be a bug in the SDK that PDFTron should look into. Thankfully I’ve found a way to manually inject the XFDF annotations without the fdfmerge() call.

I would be interested to see the XFDF file if you can send it (either post here or send to support).

I suspect that the XFDF only contains fields and not any annotations or widgets, which I can see not creating the output you would expect.

That, our you need to upgrade to the latest version of PDFNet (currently 6.5.x)