Skip to main content
All docs
V26.1
  • New PDF Document API: Merge and Split Files

    • 2 minutes to read

    The new PDF Document API allows you to merge multiple PDFs into a single file and split a PDF into multiple files.

    Merge PDF Files

    Run Demo: Merge and Split PDF Files

    Merge Entire Documents

    Call the PdfDocument.AppendDocument to merge PDF files.

    The following code snippet merges three PDF files into a single document:

    using DevExpress.Docs.Pdf;
    
    using var pdfDocument = new PdfDocument(File.OpenRead("Document.pdf"));
    
    pdfDocument.AppendDocument(File.OpenRead("Document2.pdf"));
    pdfDocument.AppendDocument(File.OpenRead("Document3.pdf"));
    
    using var outputStream = new MemoryStream();
    pdfDocument.Save(outputStream);
    

    The LoadOptions class allows you to specify a metadata synchronization mode and a password for an encrypted document. Pass a LoadOptions instance to the PdfDocument.AppendDocument method to apply these settings when you merge PDF files.

    Append Individual Pages

    Pass the page you want to append from the source document to the PdfDocument.Pages.Add method. The method adds the page to the end of the target document.

    To add a page at a specific position, use the PdfDocument.Pages.Insert method.

    The following code snippet appends the first page from one PDF document to another:

    using DevExpress.Docs.Pdf;
    
    using (var source = new PdfDocument(File.OpenRead("Document.pdf")))
    {
        using (var output = new PdfDocument())
        {
            output.Pages.Append(source.Pages[0]);
    
    
            using (var outputStream = new FileStream("Document_1.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                output.Save(outputStream);
                outputStream.Position = 0;
            }
        }
    }
    

    Split PDF Files

    Run Demo: Merge and Split PDF Files

    To split a PDF file, clone the required pages from the source document and insert them into a new PdfDocument instance.

    The following code snippet splits a PDF file into two separate documents:

    using DevExpress.Docs.Pdf;
    
    using var source = new PdfDocument(File.OpenRead("Document.pdf"));
    
    var pageRange = Enumerable.Range(0, source.Pages.Count / 2);
    using var output = new PdfDocument();
    
    foreach (var index in pageRange)
    {
        output.Pages.Add(source.Pages[index].Clone());
    }
    using (var outputStream = new FileStream("Document_1.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        output.Save(outputStream);
        outputStream.Position = 0;
    }