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.
替换页面的简单示例可以来自PageManager
cli 示例:
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);
这确实将 A.pdf 中的第一页替换为 B.pdf 中的第一页,并将结果保存为 A-withPage1FromB-simple.pdf。
但不幸的是,PageManager
它不会更新书签。因此,在上面代码的结果中,仍然有一个书签用来指向原始的第一页;由于此页面不再存在,因此它现在不再指向任何地方。并且指向 fileB 中第一页的书签被完全忽略。
其他文档级别、页面相关属性也不会被传输,例如页面标签。但是,在页面标签的情况下,第一页的原始标签在替换后仍与第一页相关联。这是由于不同类型的引用(按页码,而不是按对象)。