SimpleBookmark.ShiftPageNumbers Issue

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

SimpleBookmark.ShiftPageNumbers Issue

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));
            PdfReader reader;
            int page_offset = 0;
            int n = 0;
            List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
            IList<Dictionary<string, object>> tmp;
            string outputxml;

            foreach (string file in inputfiles)
                reader = new PdfReader(file);
                n = reader.NumberOfPages;
                tmp = SimpleBookmark.GetBookmark(reader);
                if (page_offset != 0)
                    SimpleBookmark.ShiftPageNumbers(tmp, page_offset, null);                  
                    outputxml = Directory.GetCurrentDirectory() + string.Format("\\bookmarkxml{0}.xml", page_offset);
                    SimpleBookmark.ExportToXML(tmp, new FileStream(outputxml, FileMode.Create), "ISO8859-1", true);
                for (int i = 0; i < n; i++)
                    copy.AddPage(copy.GetImportedPage(reader, i + 1));

                page_offset += n;
            copy.Outlines = bookmarks;
            outputxml = Directory.GetCurrentDirectory() + "\\allbookmarks.xml";
            SimpleBookmark.ExportToXML(bookmarks, new FileStream(outputxml, FileMode.Create), "ISO8859-1", true);

Wondering if maybe this method worked in a previous rev.

Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Re: SimpleBookmark.ShiftPageNumbers Issue

Well, after sleeping on it, I came up with a workaround.

Basically, I:

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.