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)
{
currPage.getSDFObj().erase("Annots");
}
}
// Load the new annotations into the
// document
myDoc.fdfMerge(newAnnotations);
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?
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.