0

Using pdfclown,

I was wondering the best practice to find a page in a Existing PDF doc, and replace with a page from another PDF doc.

I have the bookmark and pagelabel of both pages.

Ryan
  • 29
  • 8
  • If you're still interested in a solution, please supply example input files. – mkl Feb 27 '18 at 09:56
  • @mkl, where would you like me to send files? – Ryan Mar 02 '18 at 20:28
  • Usually one shares them using public file shares (on google drive, dropbox, ...) and posts the URLs here. – mkl Mar 03 '18 at 21:02
  • Ok, I would like to replace Page in PDF "A" with one found in PDF "B". Using book marks, or the text in the bottom right comer of the pdf page. That text is normally there. Thanks. https://www.dropbox.com/s/0terth7ll5q5mi7/Test.zip?dl=0 – Ryan Mar 07 '18 at 22:16
  • Ryan, did my answer help you or are there still issues? – mkl Mar 28 '18 at 11:04
  • It looks like it will work, Thanks! I haven't had a chance to implement it yet. ill be sure to post back when i do. – Ryan Mar 29 '18 at 14:06
  • If it proves to work for you, please accept the answer (click the tick at its upper left). If it doesn't, feel free to explain the remaining issues. – mkl Mar 29 '18 at 14:15

1 Answers1

0

A simple example for replacing pages can be derived from the PageManager cli examples:

string inputA = @"A.pdf";
string inputB = @"B.pdf";
string output = @"A-withPage1FromB-simple.pdf";

org.pdfclown.files.File fileA = new org.pdfclown.files.File(inputA);
org.pdfclown.files.File fileB = new org.pdfclown.files.File(inputB);

// replace page 0 in fileA by page 0 from fileB
Document mainDocument = fileA.Document;
Bookmarks bookmarks = mainDocument.Bookmarks;
PageManager manager = new PageManager(mainDocument);
manager.Remove(0, 1);
manager.Add(0, fileB.Document.Pages.GetSlice(0, 1));

fileA.Save(output, SerializationModeEnum.Standard);

This indeed replaces the first page in A.pdf by the first page in B.pdf and saves the result as A-withPage1FromB-simple.pdf.

Unfortunately, though, the PageManager does not update bookmarks. In the result of the code above, therefore, there still is a bookmarks which used to point to the original first page; as this page is not there, anymore, it now points nowhere anymore. And the bookmark pointing to the first page in fileB, is ignored completely.

Other document level, page related properties also are not transferred, e.g. the page label. In case of the page labels, though, the original label for the first page remains associated to the first page after replacement. This is due to a different kind of reference (by page number, not by object).

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks! Finally got around to implementing your solution. Updating bookmarks is easy, since i am finding the index of the pages based off bookmarks. – Ryan Apr 24 '18 at 18:52