Introduction
Merging several PDF documents to single PDF file is
very common task performed by many applications and Apitron PDF Kit provides very simple and clean API for it. See the
code provided in next section in order to quickly get the idea and use it in
your own app.
The code
void MergeDocuments()
{
FixedDocument resultingDocument =
new
FixedDocument();
//
let's assume that we have a folder with files which should be merged
foreach (string fileName in Directory.EnumerateFiles("Documents","*.pdf"))
{
//
open found document and copy its pages to resulting doc
using (Stream file = File.OpenRead(fileName))
{
FixedDocument pdfDocument = new FixedDocument(file);
// enumerate pages and save each page as separate PDF file
for (int i = 0; i <
pdfDocument.Pages.Count; i++)
{
// Export page before inserting it into the new
document,
// it preserves resources used by this page.
resultingDocument.Pages.Add(Page.Export(resultingDocument,
pdfDocument.Pages[i]));
}
}
}
//
save resulting document
using (Stream outputStream = File.Create("resultingDocument.pdf"))
{
resultingDocument.Save(outputStream);
}
}
This code searches for pdf files
in subdirectory “Documents” and processes each PDF file found by append its
pages to the resulting document.
The article describing document
splitting can be found in our blog or in Documentation
section on our website. You can
also find there a complete book with very detailed explanation of many PDF
related aspects (see Apitron PDF
Kit in Action). Feel free to ask questions, we are always ready to help.
Downloadable version of this article can be found by the following link [PDF].
No comments:
Post a Comment