New PDF Document API: Add, Reorder, and Remove Pages
- 6 minutes to read
The PDF Document API library supports the following page management actions:
- Add new pages to a PDF document
- Copy and reorder pages within a PDF document
- Copy pages from one PDF document to another
- Rotate and resize pages
- Rotate, scale, and offset page content
- Remove pages from a PDF document
Add Pages
Call the PdfDocument.Pages.Add method to add a new page with the specified size to the end of the document.
To add a page at a specific position, use the PdfDocument.Pages.Insert method.
The following code snippet adds pages to a PDF document: one page at the end of the document and another page after the first page.
using DevExpress.Docs.Pdf;
using DevExpress.Drawing.Printing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(
new FileStream(@"C:\Test Documents\Document.pdf", FileMode.Open,
FileAccess.ReadWrite)))
{
// Add pages to the end and at a specific position.
pdfDocument.Pages.Add(DXPaperKind.A4);
pdfDocument.Pages.Insert(1, new Page(DXPaperKind.Letter));
pdfDocument.Save(new FileStream(@"C:\Test Documents\Document_upd.pdf",
FileMode.Create, FileAccess.Write));
}
Copy and Reorder Pages
The PdfDocument.Pages property allows you to access document pages. Retrieve the required page by its index. Call the Page.Clone() method to create a copy of the page. Pass the copy to the Pages.Add method or the Pages.Insert method to add it to the document.
The following code snippet clones the first page and appends the copy to the end of the document:
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(
new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Clone the first page and append the clone.
Page extracted = pdfDocument.Pages[0].Clone();
pdfDocument.Pages.Add(extracted);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Retrieve the required page by its index and pass it to the Pages.Add or Pages.Insert method to reorder pages.
The following code snippet moves the first page to the end of the document:
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(
new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Move the first page to the end of the document.
Page extracted = pdfDocument.Pages[0];
pdfDocument.Pages.Add(extracted);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Copy Pages from Another Document
To copy pages from another PDF document, call the PdfDocument.Pages.Add method or the PdfDocument.Pages.Insert method and pass pages from another document.
The following code snippet copies the first page from one document to another document:
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument source = new PdfDocument(
new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
using (PdfDocument target = new PdfDocument(
new FileStream(@"Document1.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Insert a page from the source document.
target.Pages.Insert(3, source.Pages[0].Clone());
target.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
}
Rotate and Resize Pages
Use the Page.Rotation property to specify the rotation angle of the page. The Page.Resize method allows you to change the page size.
The following code snippet rotates the first page by 90 degrees and resizes it to A4 size:
using DevExpress.Docs.Pdf;
using System.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Rotate the first page and resize it to A4.
Page page = pdfDocument.Pages[0];
page.Rotation = PageRotationAngle.Clockwise90;
page.Resize(
new RectangleF(0, 0, 595.28f, 841.89f),
PageContentHorizontalAlignment.Center,
PageContentVerticalAlignment.Center);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Transform Page Content
Rotate Page Content
Use the Page.RotateContent method to rotate page content. The method takes three parameters: the rotation angle in degrees and rotation center coordinates.
The following code snippet rotates content on the first page by 270 degrees around the point with coordinates (300, 300):
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Rotate page content by 270 degrees around the point with coordinates (300, 300).
Page page = pdfDocument.Pages[0];
page.RotateContent(300, 300, 270);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Scale Page Content
The Page.ScaleContent method allows you to scale page content horizontally and vertically.
The following code snippet scales the content on the first page. The new content size is 50% of original both horizontally and vertically:
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Scale page content along both axes.
Page page = pdfDocument.Pages[0];
page.ScaleContent(0.5, 0.5);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Offset Page Content
Use the Page.OffsetContent method to move page content horizontally and vertically.
The following code snippet moves the content of the first page 50 units to the right and 50 units down.
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Offset page content by X and Y values.
Page page = pdfDocument.Pages[0];
page.OffsetContent(50, 50);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Remove Pages
To remove a page from a PDF document, call the PageCollection.Remove method. The PageCollection.RemoveAt(Int32) method removes a page by its index.
The following code snippet removes the first page from a PDF document:
using DevExpress.Docs.Pdf;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument(new FileStream(@"Document.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
// Remove the first page from the document.
pdfDocument.Pages.RemoveAt(0);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}