Back to Devexpress

Merge PDF Documents with DevExpress PDF Document API

officefileapi-119760-pdf-document-api-document-manipulation-merging-documents.md

latest7.2 KB
Original Source

Merge PDF Documents with DevExpress PDF Document API

  • Nov 04, 2025
  • 3 minutes to read

The PDF Document API component allows you to merge multiple PDF documents. You can also merge content of multiple pages into a single PDF page.

Use API from the table below to complete the task.

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

APIDescription
PdfDocumentProcessor.CreateEmptyDocumentCreates an empty document. You can specify a file path or a stream to pass the document. The document’s content is not kept in memory during the merge.
PdfDocumentProcessor.AppendDocumentAppends the content of one document to another. All additional content (interactive forms, bookmarks,hyperlinks, file attachments, etc.) is copied to the resulting file. The original document’s content is retained.

When the merge is completed, you do not need to save this document since it is written during the process. Dispose of the PdfDocumentProcessor instance to close the document.

Tip

You can enable object streams to reduce the merged document’s file size. To do this, set the UseObjectStreams property to true and pass PdfSaveOptions to the CreateEmptyDocument or SaveDocument method. Refer to the following topic for more information: Optimize Documents.

This example illustrates how to use the PDF Document API component to merge pages of two separate PDF documents into a single PDF document.

View Example

csharp
using DevExpress.Pdf;

using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) {
    pdfDocumentProcessor.CreateEmptyDocument("..\\..\\docs\\Merged.pdf");
    pdfDocumentProcessor.AppendDocument("..\\..\\docs\\TextMerge1.pdf");
    pdfDocumentProcessor.AppendDocument("..\\..\\docs\\TextMerge2.pdf");             
}
vb
Imports DevExpress.Pdf

Using pdfDocumentProcessor As New PdfDocumentProcessor()
    pdfDocumentProcessor.CreateEmptyDocument("..\..\docs\Merged.pdf")
    pdfDocumentProcessor.AppendDocument("..\..\docs\TextMerge1.pdf")
    pdfDocumentProcessor.AppendDocument("..\..\docs\TextMerge2.pdf")
End Using

Merge Content of Multiple Pages

Call the PdfGraphics.DrawPageContent(PdfPage) method to draw page content.

The following code snippet scales source content of two document pages and draws these pages in a landscape page.

csharp
using DevExpress.Pdf;
using System.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4Rotated);
    using (PdfDocumentProcessor processor2 = new PdfDocumentProcessor()) {
        processor2.LoadDocument(@"FirstLook.pdf");

        using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {
            graphics.SaveGraphicsState();

            // Obtain the first document page's boundaries.
            PdfRectangle clip = processor2.Document.Pages[0].CropBox;

            // Resize the source page content to fit half of the target page.
            graphics.ScaleTransform((float)((page.CropBox.Width / 2) / clip.Width), (float)((page.CropBox.Width / 2) / clip.Width));

            // Draw the content of the first document page.
            graphics.DrawPageContent(processor2.Document.Pages[0]);
            graphics.RestoreGraphicsState();

            // Define the position to insert the second page content to the target page.
            graphics.SaveGraphicsState();
            graphics.TranslateTransform((float)(page.CropBox.Width / 2), 0);

            // Obtain the second document page's boundaries.
            clip = processor2.Document.Pages[1].CropBox;

            // Resize the source page content to fit half of the target page.
            graphics.ScaleTransform((float)(page.CropBox.Width / clip.Width / 2), (float)(page.CropBox.Width / clip.Width / 2));

            // Draw the content of the second document page.
            graphics.DrawPageContent(processor2.Document.Pages[1]);
            graphics.RestoreGraphicsState();

            // Add graphics content to the target page.
            graphics.AddToPageForeground(page);
        }
        processor.SaveDocument("out2.pdf");
    }
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4Rotated)
    Using processor2 As New PdfDocumentProcessor()
        processor2.LoadDocument("FirstLook.pdf")

        Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            graphics.SaveGraphicsState()

            ' Obtain the first document page's boundaries.
            Dim clip As PdfRectangle = processor2.Document.Pages(0).CropBox

            ' Resize the source page content to fit half of the target page.
            graphics.ScaleTransform(CSng((page.CropBox.Width \ 2) \ clip.Width), CSng((page.CropBox.Width \ 2) \ clip.Width))

            ' Draw the content of the first document page.
            graphics.DrawPageContent(processor2.Document.Pages(0))
            graphics.RestoreGraphicsState()

            ' Define the position to insert the second page content to the target page.
            graphics.SaveGraphicsState()
            graphics.TranslateTransform(CSng(page.CropBox.Width \ 2), 0)

            ' Obtain the second document page's boundaries.
            clip = processor2.Document.Pages(1).CropBox

            ' Resize the source page content to fit half of the target page.
            graphics.ScaleTransform(CSng(page.CropBox.Width \ clip.Width \ 2), CSng(page.CropBox.Width \ clip.Width \ 2))

            ' Draw the content of the second document page.
            graphics.DrawPageContent(processor2.Document.Pages(1))
            graphics.RestoreGraphicsState()

            ' Add graphics content to the target page.
            graphics.AddToPageForeground(page)
        End Using
        processor.SaveDocument("out2.pdf")
    End Using
End Using