c# - ShiftPageNumbers with iTextSharp -


i'm having trouble using itextsharp merge pdfs while preserving bookmarks. i've written following hackish code test idea, shiftpagenumbers method doesn't seem working me. have idea why?

string[] files = directory.getfiles(folder); string path = path.getdirectoryname(files[0]); ilist<dictionary<string, object>> oldbookmarks     = simplebookmark.getbookmark(new pdfreader(files[0])); list<dictionary<string, object>> newbookmarks = new list<dictionary<string, object>>();  filestream outfile = new filestream(output, filemode.create); pdfconcatenate newpdf = new pdfconcatenate(outfile);  switch (oldbookmarks.count()) {     case 0:         break;      case 1:         oldbookmarks = (ilist<dictionary<string, object>>)(oldbookmarks[0])["kids"];         break;      default:         break; }  files = oldbookmarks.select(b => b["file"]).cast<string>().toarray();  foreach (string filename in files) {     console.write:ine(filename);     pdfreader reader = new pdfreader(path.combine(path, filename));     newpdf.addpages(reader);      list<dictionary<string, object>> tempbookmarks =         oldbookmarks.where(b => (string)b["file"] == filename).tolist();      // handles bookmarks     simplebookmark.shiftpagenumbers(tempbookmarks, length, null);     newbookmarks.addrange(tempbookmarks);     length += reader.numberofpages;      reader.close(); }  newpdf.writer.outlines = newbookmarks; newpdf.close(); 

in end, wound using recursive function adjust page numbers. figured easiest show if included code entire application. program designed compile ebooks published american radio relay league (arrl), broken many pdfs contain same list of bookmarks. each first-level bookmark , children link different pdf file. therefore, standard pdf merging program not correctly preserve bookmarks.

using system; using system.collections.generic; using system.io; using system.linq; using itextsharp.text.pdf;  namespace arrl_book_compiler {     internal class program     {         private const string action_key = "action";         private const string file_key = "file";         private const string kids_key = "kids";         private const string page_key = "page";         private const char delim = ' ';          private static void main(string[] args)         {             if (args.count() != 2)             {                 console.writeline("arg 1: pdf directory");                 console.writeline("arg 2: output filename");                 environment.exit(1);             }              merge(args[0], args[1]);         }          /// <summary>         /// compiles arrl books.         /// </summary>         /// <param name="folder">directory containing pdfs in entire book.</param>         /// <param name="output">path , filename of output (compiled) pdf.</param>         private static void merge(string folder, string output)         {             int offset = 1;  // setting 0 causes bookmarks off 1             string[] dirfiles                 = directory.getfiles(folder).where(f => f.endswith("pdf")).toarray();  // pdfs in directory             ilist<dictionary<string, object>> oldbookmarks                 = simplebookmark.getbookmark(new pdfreader(dirfiles[0]));             list<dictionary<string, object>> newbookmarks = new list<dictionary<string, object>>();              filestream outfile = new filestream(output, filemode.create);             pdfconcatenate newpdf = new pdfconcatenate(outfile);              if (oldbookmarks.count() == 1)             {                 oldbookmarks = (ilist<dictionary<string, object>>)(oldbookmarks[0])[kids_key];             }              string[] bfiles                 = oldbookmarks.select(b => b[file_key]).cast<string>().toarray();  // files in bookmark order             ienumerable<string> missingfiles = bfiles.except(dirfiles.select(f => path.getfilename(f)));              if (missingfiles.any())             {                 console.error.writeline("the following files present in bookmarks not in directory:");                 foreach (string filename in missingfiles) console.error.writeline(filename);                 environment.exit(2);             }              for(int = 0; < bfiles.count(); i++)             {                 console.write(string.format("\r{0}% complete", (int)((i + 1f)/bfiles.count()*100)));                 string filename = bfiles[i];                 pdfreader reader = new pdfreader(path.combine(folder, filename));                 list<dictionary<string, object>> tempbookmarks =                     oldbookmarks.where(b => b.containskey(file_key) && (string)b[file_key] == filename).tolist();                  // handles bookmarks                 newbookmarks.addrange(modifybookmarks(  // exception if linq can't find file_key key in list item                     oldbookmarks.where(b => b.containskey(file_key) && (string)b[file_key] == filename).tolist(),                     offset));                 offset += reader.numberofpages;                  newpdf.addpages(reader);                 reader.close();             }              newpdf.writer.outlines = newbookmarks;             newpdf.close();         }          private static list<dictionary<string, object>>             modifybookmarks(list<dictionary<string, object>> list, int offset)         {             (int = 0; < list.count(); i++)             {                 string pagekey = (string)list[i][page_key];                 int index = pagekey.indexof(delim);                 list[i][page_key] = (int.parse(pagekey.substring(0, index)) + offset).tostring()                     + pagekey.substring(index);                  if (list[i].containskey(file_key)) list[i].remove(file_key);                 if (list[i].containskey(action_key)) list[i][action_key] = "goto";                  if (list[i].containskey(kids_key))                     list[i][kids_key] = modifybookmarks((list<dictionary<string, object>>)list[i][kids_key], offset);             }              return list;         }     } } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -