Combine PDFs
Combine related PDFs when readers need to open or share them as one file. Combine two existing PDFs into a single document. This example follows CombinePdf.cs, appends DocumentB.pdf after DocumentA.pdf, and saves the result as CombinePdf_output.pdf.
What you’ll create
- One PDF containing all pages from
DocumentA.pdf, followed by all pages fromDocumentB.pdf. - The original page order within each input document.
- A file named
CombinePdf_output.pdfin your chosen output directory.
Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. Ensure DocumentA.pdf and DocumentB.pdf are in the Resources folder. The class reads two bundled PDFs and saves the combined document to an existing output directory.
using System;
using System.IO;
using DynamicPDF.Flow;
using DynamicPDF.Flow.Merger;
namespace FluentExamples.Examples;
internal static class CombinePdf
{
public static void Run(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var path = Path.Combine(
outputRoot, "CombinePdf_output.pdf");
var documentA = Path.Combine(resources, "DocumentA.pdf");
var documentB = Path.Combine(resources, "DocumentB.pdf");
DocumentBuilder.Create()
.Pdf(PdfSource.FromFile(documentA))
.Pdf(PdfSource.FromFile(documentB))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Imports DynamicPDF.Flow.Merger
Friend Module CombinePdf
Public Sub Run(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim outputPath = Path.Combine(
outputRoot, "CombinePdf_output.pdf")
Dim documentA = Path.Combine(resources, "DocumentA.pdf")
Dim documentB = Path.Combine(resources, "DocumentB.pdf")
DocumentBuilder.Create().
Pdf(PdfSource.FromFile(documentA)).
Pdf(PdfSource.FromFile(documentB)).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
End Module
How it works
1. Locate the input PDFs and choose the output path
The example uses DocumentA.pdf and DocumentB.pdf from the Resources folder.
The output path joins outputRoot with CombinePdf_output.pdf. Make sure the output directory exists. Saving overwrites an existing file with that name; the input PDFs are read from their separate resource paths.
2. Add the first PDF
DocumentBuilder.Create() starts the document. PdfSource.FromFile supplies DocumentA.pdf as a file-based PDF source, and the first .Pdf(...) call adds all of its pages using the default merge options.
3. Append the second PDF
The second .Pdf(...) call adds all pages from DocumentB.pdf after the first document's pages. The order of these calls determines the document order in the output; neither call selects a page range or reorders pages within an input.
4. Save the combined PDF
.Save(path) builds and saves the combined document directly through the fluent builder to CombinePdf_output.pdf. Console.WriteLine prints the output path so you can find and open the file.
Next steps
Replace the resource files with your own PDFs, or add another .Pdf(PdfSource.FromFile(...)) call before .Save(path) to append a third document. Explore Create a PDF to build new content, or browse all examples.