Introduction
Copying pages between PDF documents can be seen as a special case of merging described in one of the earlier posts. A small piece of code below shows how to perform this task if you have only two PDF documents and want to copy a page from A to B.
The code
publicvoid CopyPDFPage()
{
// open documents
using (Stream s1 = File.OpenRead("source.pdf"),
s2 = newFileStream("destination.pdf",FileMode.Open,FileAccess.ReadWrite))
{
// create document objects
FixedDocument source = newFixedDocument(s1);
FixedDocument destination = newFixedDocument(s2);
// export first page from source doc and add to destination
destination.Pages.Add(Page.Export(destination, source.Pages[0]));
// save changes as incremental update
destination.Save();
}
}
The main step here, required to copy a Page (or any other resource) from one doc to another, is the call to the static Export method. PDF resources used on the particular PDF page (images, drawings, etc.) can be defined in various places across the document and referenced within the page’s content. These resources must be repacked in order to correctly copy the page. That’s what the Export() call does – it prepares the page and referenced resources for copying.
Conclusion
PDF processing is easy with the right tools. Try the Apitron PDF Kit .NET component and see for yourself. Read our free bookwith ready to use, thoroughly explained samples, and let us know what you think.
Downloadable version of this article can be found by the following link [PDF].
Downloadable version of this article can be found by the following link [PDF].