Organize Pages
- 5 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:
- Add a new page
- Copy pages from one document to another
- Render new page with graphics
- Rotate and resize a page
- Transform page content
- Delete a page
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.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.
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 a reference to the DevExpress.Pdf.Drawing.v20.2 assembly to access the PdfGraphics class. Refer to the following article for more information about PDF graphics: PDF Graphics.
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.
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:
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%:
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];
// 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:
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.
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");
}