Skip to main content
A newer version of this page is available. .

Page Manipulation

  • 5 minutes to read

The topic describes the following page manipulation capabilities:

Copy pages from one document to another PDF

To copy pages:

Example

This example shows how to copy a page from one PDF document to another PDF.

using DevExpress.Pdf;

namespace CopyPage {
    class Program {
        static void Main(string[] args) {

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

Add/Insert a new page into a document

To add a new page to the end of a document, call the PdfDocumentProcessor.AddNewPage method with the specified page size, as shown below.

To insert a new page into a document, call the PdfDocumentProcessor.InsertNewPage method with the page number where the new page should be inserted and the page size.

To save the resulting document, call the PdfDocumentProcessor.SaveDocument overload method.

The following code shows how to add a new page to a document:

using DevExpress.Pdf;

namespace PdfExample {

    class Program {
        static void Main(string[] args) {

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

The graphics content (for example, an image or string) can be added to an existing page’s foreground/background by calling the PdfGraphics.AddToPageForeground or PdfGraphics.AddToPageBackground overload method.

The PDF graphics is represented by an instance of the PdfGraphics class. To access this class, you need to reference the DevExpress.Pdf.Drawing assembly. See the Adding Graphics Content to a Page topic to learn more.

Render a new page with graphics

The PdfDocumentProcessor class contains the PdfDocumentProcessor.RenderNewPage overload methods that render a new page with graphics content.

See the PDF Graphics section to learn more about PDF Graphics.

Delete a page from a document

To delete a particular page from a document, call the PdfDocumentProcessor.DeletePage method with the number of the page that should be deleted.

Example

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;
// ...

    class Program {
        static void Main(string[] args) {
            using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) {
                pdfDocumentProcessor.LoadDocument("..\\..\\TextDelete.pdf");
                for (int i = pdfDocumentProcessor.Document.Pages.Count / 2; i >= 0; i--)
                    pdfDocumentProcessor.DeletePage(i * 2 + 1);
                pdfDocumentProcessor.SaveDocument("..\\..\\Deleted.pdf");
            }
        }
    }

Rotate a page in a document

To rotate a page at a specified angle:

Example

This example illustrates how to use the PDF Document API component to rotate pages.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/how-to-rotate-pdf-pages-t114305

using DevExpress.Pdf;

namespace PdfPageRotationExample
{

    class Program {
        static void Main(string[] args)
        {
            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");
            }
        }
    }
}