Legacy Example NOTICE
This article predates the introduction of our PdfDocument class. PdfDocument has a Combine method which is much more efficient for combining two or more PDF files. Support strongly recommends using this newer class for this task
Q10429 - HOWTO: Combine Multiple PDFs with Automatic Repair of Damaged PDFs
Q10428 - HOWTO: Combine Multiple PDF Files With Fillable Forms (AcroForms)
Q10430 - HOWTO: Separate a Multi-Page PDF With Automatic Repair of Damaged PDFs
Main Article Content
With the PdfRasterizer add-on, merging two Pdf files into one is a simple coding task. First combine the two file paths into one string array. Then use that array to construct a FileSystemImageSource object. Then using a PdfEncoder, call the Save method overload that takes an ImageSource.
C#
static Form1()
{
RegisteredDecoders.Decoders.Add(new PdfDecoder());
}
private void MergePdfs(string FirstFilePath, string SecondFilePath, string OutputPath)
{
FileSystemImageSource fsis = new FileSystemImageSource(new string[]{ FirstFilePath, SecondFilePath }, true);
using (FileStream fs = new FileStream(OutputPath, FileMode.OpenOrCreate))
{
PdfEncoder encoder = new PdfEncoder();
encoder.Save(fs, fsis, null);
}
}
VB.NET
Shared Sub New()
RegisteredDecoders.Decoders.Add(New PdfDecoder())
End Sub
Private Sub MergePdfs(ByVal FirstFilePath As String, ByVal SecondFilePath As String, ByVal OutputPath As String)
Dim fsis As New FileSystemImageSource(New String() {FirstFilePath, SecondFilePath}, True)
Using fs As New FileStream(OutputPath, FileMode.OpenOrCreate)
Dim encoder As New PdfEncoder()
encoder.Save(fs, fsis, Nothing)
End Using
End Sub
Combining Without Rasterizing
If you're using DotImage 10.0 or newer or DotPdf, You can combine two or more PDFs together using our PdfDocument Classes static Combine method:
C#
void PdfDocument.Combine(String outputPdfPath, string[] arrayOfSourcePdfFilePaths);
For more on PdfDocument.Combine please see the API reference for:
PdfDocument.Combine(string, string[])
PdfDocument.Combine(Stream, Stream[])
Original Article:
Q10261 - HOWTO: Merge Two Pdf Files Into One