Anyone else notice that the SimpleBookmark.ShiftPageNumbers method doesn't seem to be working in 5.4.3?
I'm merging PDFs and trying to preserve the existing bookmarks. The bookmarks are preserved in the final PDF, but SimpleBookmark.ShiftPageNumbers doesn't seem to shift the page number by the offset - actually, it doesn't seem to shift them at all. This code is pretty much straight out of the iText in Action 2nd Ed. book (save for the few C# conversions). The code also spits out the bookmarks to XML, where I have verified that the page numbers for the bookmarks aren't being shifted and remain at their original values.
Here's my code:
static void MergePDFs(string outputfile, List<string> inputfiles)
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(outputfile, FileMode.Create));
int page_offset = 0;
int n = 0;
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
IList<Dictionary<string, object>> tmp;
foreach (string file in inputfiles)
reader = new PdfReader(file);
n = reader.NumberOfPages;
tmp = SimpleBookmark.GetBookmark(reader);
Well, after sleeping on it, I came up with a workaround.
1) Export bookmarks from the PDF to an XML file.
2) Load the XML file with XDocument.
3) Use linq to find all of the "Code" attributes ("this.pageNum = n;").
4) Manually modify n by the offset.
5) Save it out as a new XML file.
6) Use the SimpleBookMark.ImportFromXML to get a new IList.
7) Add the new IList to bookmarks collection for the new PDF file.
My initial tests have proved successful.
This is not a production app, so I'm not too concerned with this hack.
Would be happy to share my code with anyone who wants it.