REW

How Do I Add A Page To A PDF In DevExpress?

Published Aug 29, 2025 6 min read
On this page

To add a page to a PDF in DevExpress, you use the PdfDocumentProcessor class, part of the Office File API. This tool allows for various page manipulation tasks, including inserting blank pages or appending pages from another PDF document. The process involves creating an instance of PdfDocumentProcessor, loading your target PDF, performing the page manipulation, and then saving the changes.

Note: The PdfDocumentProcessor component requires a DevExpress Universal or Office File API subscription.

Prerequisites

  • A C# project (e.g., Windows Forms, WPF, ASP.NET).
  • The DevExpress Office File API libraries are referenced in your project.

Method 1: Inserting a new blank page

This method is useful when you need to insert a placeholder page into an existing PDF document. You can specify the page dimensions and where the page should be inserted.

Step 1: Load the existing PDFInstantiate a PdfDocumentProcessor object and use the LoadDocument method to open your file.

using DevExpress.Pdf;
using System.IO;
// ...
// Create a PdfDocumentProcessor instance
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load the existing PDF document
    processor.LoadDocument("path_to_your_document.pdf");

Use code with caution.

Step 2: Insert the new pageCall the InsertNewPage method on the processor object. This method requires the zero-based page index where the new page will be inserted, and a PdfRectangle to define the page size.

    // Insert a new blank A4 page at page index 2 (the third page).
    // PdfPaperSize is a helper for common page dimensions.
    processor.InsertNewPage(2, PdfPaperSize.A4);

Use code with caution.

Step 3: Save the modified PDFFinally, save the document to a new file or overwrite the original.

    // Save the modified document
    processor.SaveDocument("path_to_your_new_document.pdf");
}

Use code with caution.

Complete code example for a blank page

This example shows how to insert a new, empty, legal-sized page at the beginning of a document.

using DevExpress.Pdf;
using System;
using System.Drawing;
using System.IO;
public class PdfPageInserter
{
    public static void InsertBlankPage(string sourcePath, string outputPath)
    {
        try
        {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                // Load the source PDF
                processor.LoadDocument(sourcePath);
                // Define the dimensions for a Legal-sized page
                // Note: Page size is measured in points (1 point = 1/72 inch)
                PdfRectangle legalPageRect = new PdfRectangle(0, 0, 612, 1008);
                // Insert the new blank page at the first position (index 0)
                // Existing pages will be shifted back
                processor.InsertNewPage(0, legalPageRect);
                // Save the changes to a new file
                processor.SaveDocument(outputPath);
                Console.WriteLine("Blank page inserted successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Use code with caution.

Method 2: Inserting pages from another PDF

To insert one or more pages from a different PDF, you need to use two PdfDocumentProcessor instances: one for the source and one for the target document.

Step 1: Create two PdfDocumentProcessor instancesYou'll need one for the document you are modifying and another for the document from which you're copying pages.

using DevExpress.Pdf;
using System.IO;
// ...
using (PdfDocumentProcessor targetProcessor = new PdfDocumentProcessor())
using (PdfDocumentProcessor sourceProcessor = new PdfDocumentProcessor())
{
    // Load the target document
    targetProcessor.LoadDocument("target_document.pdf");
    // Load the source document
    sourceProcessor.LoadDocument("source_document.pdf");

Use code with caution.

Step 2: Copy pages from source to targetAccess the Pages collection from the source document (sourceProcessor.Document.Pages) and use the Insert method on the target document's Pages collection to add the copied page.

    // Specify the page index where the new pages will be inserted
    int insertionIndex = 1; // Insert after the first page
    // Copy the first two pages (indexes 0 and 1) from the source document
    // and insert them into the target document
    var pageToCopy1 = sourceProcessor.Document.Pages[0];
    var pageToCopy2 = sourceProcessor.Document.Pages[1];
    // Insert the pages into the target document's page collection
    targetProcessor.Document.Pages.Insert(insertionIndex, pageToCopy1);
    targetProcessor.Document.Pages.Insert(insertionIndex + 1, pageToCopy2);

Use code with caution.

Step 3: Save the resulting PDFSave the final document with the newly inserted pages.

    // Save the combined document
    targetProcessor.SaveDocument("combined_document.pdf");
}

Use code with caution.

Complete code example for inserting pages from another PDF

This full example demonstrates how to copy a single page from a source file and insert it into a target file at a specific location.

using DevExpress.Pdf;
using System;
using System.IO;
public class PdfMerger
{
    public static void InsertPagesFromAnotherPdf(string targetPath, string sourcePath, string outputPath, int insertPageIndex)
    {
        try
        {
            using (PdfDocumentProcessor targetProcessor = new PdfDocumentProcessor())
            using (PdfDocumentProcessor sourceProcessor = new PdfDocumentProcessor())
            {
                // Load the documents
                targetProcessor.LoadDocument(targetPath);
                sourceProcessor.LoadDocument(sourcePath);
                // Get the page to be copied (e.g., the second page from the source)
                PdfPage pageToCopy = sourceProcessor.Document.Pages[1];
                // Insert the copied page into the target document at the specified index
                targetProcessor.Document.Pages.Insert(insertPageIndex, pageToCopy);
                // Save the new document
                targetProcessor.SaveDocument(outputPath);
                Console.WriteLine("Page from source PDF inserted into target PDF successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Use code with caution.

Method 3: Combining entire PDF documents

For cases where you want to append all pages from one PDF to another, the AppendDocument method provides a more efficient and direct approach than iterating through pages. This is especially useful for merging documents and ensuring that all additional content, such as forms and bookmarks, is also copied.

Step 1: Create a PdfDocumentProcessor instanceYou only need one processor instance for the target document.

using DevExpress.Pdf;
using System.IO;
// ...
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    // Load the first document (the base document)
    processor.LoadDocument("document_1.pdf");

Use code with caution.

Step 2: Append the second documentUse the AppendDocument method to add the content of a second PDF directly to the end of the first.

    // Append the content of the second document
    // The path can be a string or a Stream
    processor.AppendDocument("document_2.pdf");

Use code with caution.

Step 3: Save the merged PDFThe merged document is saved after the AppendDocument call.

    // Save the merged document
    processor.SaveDocument("merged_document.pdf");
}

Use code with caution.

Complete code example for combining PDFs

using DevExpress.Pdf;
using System;
using System.IO;
public class PdfCombiner
{
    public static void CombinePdfs(string baseFilePath, string fileToAppendPath, string outputPath)
    {
        try
        {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                // Load the initial document
                processor.LoadDocument(baseFilePath);
                // Append the second document to the end
                processor.AppendDocument(fileToAppendPath);
                // Save the result
                processor.SaveDocument(outputPath);
                Console.WriteLine("PDFs combined successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Use code with caution.

Important considerations

  • Licensing: Remember that the PdfDocumentProcessor component requires a DevExpress Universal or Office File API subscription. These code examples will not work in a production environment without the proper license.
  • Performance: For very large PDFs, streaming operations can be more memory-efficient than loading the entire document into memory. DevExpress API often supports working with Stream objects for this purpose.
  • Error Handling: It is always a best practice to wrap your file operations in try...catch blocks to handle potential IOException or other exceptions, especially when dealing with file paths and I/O.
  • Alternative Methods: While PdfDocumentProcessor is the primary tool for this, DevExpress also offers other PDF-related components, such as XRPdfContent for embedding PDF content in reports. However, for direct PDF page manipulation, PdfDocumentProcessor is the intended solution.
Enjoyed this article? Share it with a friend.