Merge PDF Documents
Use RadFixedDocument.Merge() to merge imported PDF documents into a document model, or use PdfStreamWriter to stream pages directly to a new PDF with minimal memory use.
See the PdfProcessing Content Merging, Splitting, and Adding Demo for a live example.
Merging with RadFixedDocument
Use RadFixedDocument.Merge() with the RadFixedDocument model when you need a full document model and document-level content. The method clones the source document and appends its supported content to the current RadFixedDocument, including pages, bookmarks, form fields, and destinations:
int timeoutInterval = 100;
string pdfFolderPath = @"..\..\..\Pdf Files";
// Adjust this path to your PDF files directory
string[] pathsCollection = Directory.GetFiles(pdfFolderPath);
RadFixedDocument resultFile = new RadFixedDocument();
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
foreach (string path in pathsCollection)
{
RadFixedDocument document;
using (Stream stream = File.OpenRead(path))
{
document = pdfFormatProvider.Import(stream, TimeSpan.FromSeconds(timeoutInterval));
resultFile.Merge(document);
}
}
string outputFilePath = "merged_by_RadFixedDocument.pdf";
File.Delete(outputFilePath);
using (Stream output = File.OpenWrite(outputFilePath))
{
pdfFormatProvider.Export(resultFile, output, TimeSpan.FromSeconds(timeoutInterval));
}
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
Merging with PdfStreamWriter
Use the PdfStreamWriter overview when you need a low-memory merge flow. It streams page content from source PDF files directly into a new output file without preserving the PDF document model in memory:
string pdfFolderPath = @"..\..\..\Pdf Files";
// Adjust this path to your PDF files directory
string[] pathsCollection = Directory.GetFiles(pdfFolderPath);
using (MemoryStream stream = new MemoryStream())
{
using (PdfStreamWriter fileWriter = new PdfStreamWriter(stream, leaveStreamOpen: true))
{
foreach (string path in pathsCollection)
{
using (PdfFileSource fileSource = new PdfFileSource(new MemoryStream(File.ReadAllBytes(path))))
{
for (int i = 0; i < fileSource.Pages.Length; i++)
{
PdfPageSource sourcePage = fileSource.Pages[i];
using (PdfPageStreamWriter resultPage = fileWriter.BeginPage(sourcePage.Size))
{
resultPage.WriteContent(sourcePage);
}
}
}
}
}
string outputFilePath = "merged_by_PdfStreamWriter.pdf";
File.Delete(outputFilePath);
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
stream.WriteTo(fileStream);
}
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
}
The Manipulate Pages SDK demo shows this approach end to end.