Organize Pages

  • 5 minutes to read

The PDF Document API allows you to perform the following actions:

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. Retrieve a specific page by its 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 the 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.

This example shows how to copy a page from one PDF 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");
        }
    }

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 reference to the DevExpress.Pdf.Drawing assembly to access the PdfGraphics class. Refer to the following article fro more information about PDF graphics: PDF Graphics.

Rotate and Resize a Page

The following API allows you to rotate a page at a specific angle:

API Description
PdfDocumentProcessor.LoadDocument Loads a document into the PDF Document API component.
PdfDocumentProcessor.Document Obtains the document.
PdfDocument.Pages Returns the page collection. Access the specific page by its index.
PdfPageTreeObject.Rotate Specifies the rotation angle (in degrees).
PdfDocumentProcessor.SaveDocument Saves the result.

This example rotates all document 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");
}

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 the content horizontally or vertically, use a negative value.

The code sample below scales the content to 75%:

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:

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument(@"Document.pdf");
    PdfPage page = processor.Document.Pages[0];
    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.

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 particular page from a document, call the PdfDocumentProcessor.DeletePage method with the specified page number.

This example shows how to delete odd-numbered pages in the document, starting from the last odd-numbered page.

TIP

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T114310.

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