Skip to main content

Organize Pages in PDF Documents

  • 8 minutes to read

The PDF Document API allows you to add, remove, and customize pages. Refer to the following sections to review available API members:

Basic API: Load and Save Files, Access Document and Pages

The table below lists API used to load documents to the PdfDocumentProcessor, access the document and its pages, and save the result.

Method Description
PdfDocumentProcessor.LoadDocument Loads a document into the PDF Document API component.
PdfDocumentProcessor.Document Obtains the document.
PdfDocument.Pages Retrieves document pages. You can get a specific page by its index.
PdfDocumentProcessor.DocumentFacade Returns a PdfDocumentFacade object. This object exposes members used to perform various operations on a PDF file without access to its inner structure.
PdfDocumentFacade.Pages Returns a list of PdfPageFacade objects that contain PDF page properties. You can obtain specific page properties by the page index.
PdfDocumentProcessor.SaveDocument Saves the result.

Add a New Page

Use the following API to append or insert a new page:

Method Description
PdfDocumentProcessor.AddNewPage Appends an empty page.
PdfDocumentProcessor.InsertNewPage Inserts a page at a specified position (page number).

The following code adds a new page to a document:

using DevExpress.Pdf;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument("..\\..\\Document.pdf");
    processor.AddNewPage(PdfPaperSize.A4);
    processor.SaveDocument("..\\..\\Result.pdf");
}

Copy Pages from One Document to Another

Use the PdfDocument.Pages property to retrieve an IList<PdfPage> object. The IList<T>.Insert(Int32, T) method allows you to add an extracted page to another PDF file.

The code sample below copies a page from one PDF document to another.

View Example: How to copy a page from one document to another

using DevExpress.Pdf;

using (PdfDocumentProcessor source = new PdfDocumentProcessor())
{
    source.LoadDocument("..\\..\\Document1.pdf");
    using (PdfDocumentProcessor target = new PdfDocumentProcessor())
    {
        target.LoadDocument("..\\..\\Document2.pdf");
        target.Document.Pages.Insert(3, source.Document.Pages[0]);
        target.SaveDocument("..\\..\\Result.pdf");
    }
}

Extract a Page

Use the Add(T) method to add pages to the collection. When you add a page from another document, all page content is copied to the resulting document.

View Example

using DevExpress.Pdf;

using (PdfDocumentProcessor source = new PdfDocumentProcessor())
{
    source.LoadDocument("..\\..\\Document.pdf");
    using (PdfDocumentProcessor target = new PdfDocumentProcessor())
    {
        target.CreateEmptyDocument("..\\..\\ExtractedFirstPage.pdf");
        target.Document.Pages.Add(source.Document.Pages[0]);
    }
}

Render New Page with Graphics

The PdfDocumentProcessor.RenderNewPage method allows you to render a new page with graphics.

Call the PdfGraphics.AddToPageForeground or PdfGraphics.AddToPageBackground method to add graphics content (for example, an image or string) to an existing page’s foreground/background.

Add a reference to the DevExpress.Pdf.Drawing.v24.2 assembly to access the PdfGraphics class. Refer to the following article for more information about PDF graphics: PDF Graphics.

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");
    }
}

Rotate and Resize a Page

Rotate a Page

The Rotate property specifies the page’s rotation angle.

The code sample below rotates all document pages.

rotated pages

View Example: How to rotate PDF pages

using DevExpress.Pdf;

using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
    pdfDocumentProcessor.LoadDocument("..\\..\\docs\\TextRotate.pdf");
    int angle = 0;
    foreach (PdfPage page in pdfDocumentProcessor.Document.Pages)
    {
        angle = (angle + 90) % 360;
        page.Rotate = angle;
    }
    pdfDocumentProcessor.SaveDocument("..\\..\\docs\\Rotated.pdf");
}

Resize a Page

Call the PdfPage.Resize method to resize a page. The page content retains its aspect ratio.

The code sample below shows how to resize a page:

resize page

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument(@"Document.pdf");
    PdfPage page = processor.Document.Pages[0];

    page.Resize(PdfPaperSize.Letter, PdfContentHorizontalAlignment.Center,
       PdfContentVerticalAlignment.Center);
    processor.SaveDocument("out2.pdf");
}

Page Content Transformations

Scale and Flip Content

Use the PdfPage.ScaleContent method to scale the page content. Specify the horizontal and vertical scale factors as parameters (1 is 100%). To flip content horizontally or vertically, use a negative value.

The code sample below scales the content to 75%:

scaled content

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument(@"Document.pdf");
    PdfPage page = processor.Document.Pages[0];
    page.ScaleContent(0.75,0.75);
    processor.SaveDocument("result.pdf");
}

Rotate Content

The PdfPage.RotateContent method rotates page content relative to the specified point. The operation does not affect annotations.

The code sample below rotates the first page’s content:

rotated content

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument(@"Document.pdf");
    PdfPage page = processor.Document.Pages[0];

    // Scale content to fit it
    // on the first page after rotation
    page.ScaleContent(0.5, 0.5);
    page.RotateContent(200,100, 270);
    processor.SaveDocument("result.pdf");
}

Specify the Content Offset

Call the PdfPage.OffsetContent method to specify the content offset. The page retains its size.

The code sample below specifies the content offset:

offset content

using DevExpress.Pdf;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument(@"Document.pdf");
    PdfPage page = processor.Document.Pages[0];
    page.OffsetContent(50, 50);
    processor.SaveDocument("result.pdf");
}

Delete a Page

To delete a page from a multi-page document, call the PdfDocumentProcessor.DeletePage method with the specified page number.

The code sample below deletes odd-numbered pages in a document, starting with the last odd-numbered page.

View Example: Delete Pages from PDF

using DevExpress.Pdf;

using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
  pdfDocumentProcessor.LoadDocument("..\\..\\docs\\TextDelete.pdf");
  for (int i = pdfDocumentProcessor.Document.Pages.Count; i > 0; i--)
    if (i % 2 != 0)
    {
      pdfDocumentProcessor.DeletePage(i);
    }

  pdfDocumentProcessor.SaveDocument("..\\..\\docs\\Deleted.pdf");
}

Clear Page Content

The PdfPageFacade class allows you to organize a page without access to its inner structure. Call the PdfPageFacade.ClearContent method to remove content in one or multiple page areas. You can specify what content type to keep in the areas that are to be cleared. To do that, pass the PdfClearContentOptions class object as the method parameter.

Run Demo: PDF Clear Page Content

The code sample below removes only text in the upper half of the first page:

result

using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
    // Load a document
    pdfDocumentProcessor.LoadDocument("Document.pdf");

    // Access the first page properties
    PdfPageFacade pageFacade = pdfDocumentProcessor.DocumentFacade.Pages[0];

    // Define an area to clear
    PdfRectangle cropBox = pdfDocumentProcessor.Document.Pages[0].CropBox;
    double halfPage = cropBox.Top / 2;
    PdfRectangle pageRectangle = new PdfRectangle(cropBox.Left, halfPage, cropBox.Right + halfPage, cropBox.Top);

    // Set what content type to keep
    PdfClearContentOptions options = new PdfClearContentOptions()
    {
      ClearAnnotations = false,
      ClearGraphics = false, 
      ClearImages = false
    };

    // Clear the page area
    pageFacade.ClearContent(pageRectangle, options);

    pdfDocumentProcessor.SaveDocument("Document_cleared.pdf");
}