Skip to main content

Merge PDF Documents

  • 3 minutes to read

The PDF Document API component allows you to merge multiple PDF documents.

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.

API Description
PdfDocumentProcessor.CreateEmptyDocument Creates 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.AppendDocument Appends 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 don’t need to save this document since it is written during the process. Dispose of the PdfDocumentProcessor instance to close the document.

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

using DevExpress.Pdf;

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

Merge Content of Multiple Pages

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

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

Draw Two Pages in a Landscape Page

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.CreateGraphics()) {
            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, 72, 72);
        }
        processor.SaveDocument("out2.pdf");
    }
}