How do I preserve existing destiantions (bookmarks, links) after editing a PDF page?

Q: I have encountered one issue with the 'basic editing' example. I
have a PDF document as input file that contains some Named
Destinations; But the output file loses the Named destinations
functionality. As I can see, the all changes that done by the in-place
text editing algorithm are replacing some designated characters by an
others. Question: is it possible to run this code with no side
effects?
------
A: Page editing samples (ElementEdit - http://www.pdftron.com/net/samplecode.html#ElementEdit
etc) essentially copy PDF content to a new page and replace the old
page with a new one. The problem is that old destinations still point
to the old page. In order for the new page to truly take the identity
of the old page you can use the following line (just before deleting
the old page):

...
PageIterator old_pg = doc.GetPageIterator(1);

doc.GetSDFDoc().Swap(old_pg.Current().GetObjNum(),
new_pg.GetObjNum()); // <---

doc.PageRemove(old_pg);
...

By adding this line you will preserve all bookmarks and annotations.

This way all bookmarks, destinations, links, and annotations that
point to the old page will not be invalidated and will point to the
new page.