How to safely import annotations from one document to a different one?

I want to store annotations from one document, in a database, and then later load in another document. But they sometimes come out in the wrong position on the page.

This is because, though the pages might look the same, dimension wise, one or both might be rotated differently, and there may be different origins for the crop and media boxes. So you would need to normalize the annotations from the source document so their position takes this into account, and then reverse the process on the destination document.

You would do the following when storing the coordinates.

Matrix2D mtx = srcPage.getDefaultMatrix(); Rect rect = annot.getRect(); Point pt1 = mtx.multPoint(rect.x1, rect.y1); Point pt2 = mtx.multPoint(rect.x2, rect.y2); rect = new Rect(pt1.x, pt1.y, pt2.x, pt2.y); rect.normalize(); // make x1,y1 bottom left corner of rect. // store rect in DB

Then on loading you would do the reverse, but use the following line of code.

Matrix2D mtx = destPage.getDefaultMatrix().Inverse();

Of course this is not bullet proof. For example, if the target page is much smaller then the source page, it is possible for annots to be outside the crop box. You might want to detect this and fit it to page.