officefileapi-119762-pdf-document-api-document-manipulation-page-manipulation.md
The PDF Document API allows you to add, remove, and customize pages. Refer to the following sections to review available API members:
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. |
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");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("..\..\Document.pdf")
processor.AddNewPage(PdfPaperSize.A4)
processor.SaveDocument("..\..\Result.pdf")
End Using
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");
}
}
Imports DevExpress.Pdf
Using source As New PdfDocumentProcessor()
source.LoadDocument("..\..\Document1.pdf")
Using target As New PdfDocumentProcessor()
target.LoadDocument("..\..\Document2.pdf")
target.Document.Pages.Insert(3, source.Document.Pages(0))
target.SaveDocument("..\..\Result.pdf")
End Using
End Using
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.
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]);
}
}
Imports DevExpress.Pdf
Using source As New PdfDocumentProcessor()
source.LoadDocument("..\..\Document.pdf")
Using target As New PdfDocumentProcessor()
target.CreateEmptyDocument("..\..\ExtractedFirstPage.pdf")
target.Document.Pages.Add(source.Document.Pages(0))
End Using
End Using
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.v25.2 assembly to access the PdfGraphics class. Refer to the following article for more information about PDF graphics: PDF Graphics.
Call the PdfGraphics.DrawPageContent(PdfPage) method to draw page content.
The following code snippet scales source content of two document pages and draws these 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.CreateGraphicsWorldSystem()) {
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);
}
processor.SaveDocument("out2.pdf");
}
}
Imports DevExpress.Pdf
Imports System.Drawing
'...
Using processor As New PdfDocumentProcessor()
processor.CreateEmptyDocument()
Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4Rotated)
Using processor2 As New PdfDocumentProcessor()
processor2.LoadDocument("FirstLook.pdf")
Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
graphics.SaveGraphicsState()
' Obtain the first document page's boundaries.
Dim clip As PdfRectangle = processor2.Document.Pages(0).CropBox
' Resize the source page content to fit half of the target page.
graphics.ScaleTransform(CSng((page.CropBox.Width \ 2) \ clip.Width), CSng((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(CSng(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(CSng(page.CropBox.Width \ clip.Width \ 2), CSng(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)
End Using
processor.SaveDocument("out2.pdf")
End Using
End Using
The Rotate property specifies the page’s rotation angle.
The code sample below rotates all document 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");
}
Imports DevExpress.Pdf
Using pdfDocumentProcessor As New PdfDocumentProcessor()
pdfDocumentProcessor.LoadDocument("..\..\docs\TextRotate.pdf")
Dim angle As Integer = 0
For Each page As PdfPage In pdfDocumentProcessor.Document.Pages
angle = (angle + 90) Mod 360
page.Rotate = angle
Next page
pdfDocumentProcessor.SaveDocument("..\..\docs\Rotated.pdf")
End Using
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");
}
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("Document.pdf")
Dim page As PdfPage = processor.Document.Pages(0)
page.Resize(PdfPaperSize.Letter, PdfContentHorizontalAlignment.Center,
PdfContentVerticalAlignment.Center)
processor.SaveDocument("out2.pdf")
End Using
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");
}
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("Document.pdf")
Dim page As PdfPage = processor.Document.Pages(0)
page.ScaleContent(0.75,0.75)
processor.SaveDocument("result.pdf")
End Using
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");
}
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("Document.pdf")
Dim page As PdfPage = 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")
End Using
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");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("Document.pdf")
Dim page As PdfPage = processor.Document.Pages(0)
page.OffsetContent(50, 50)
processor.SaveDocument("result.pdf")
End Using
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");
}
Imports DevExpress.Pdf
Using pdfDocumentProcessor As New PdfDocumentProcessor()
pdfDocumentProcessor.LoadDocument("..\..\docs\TextDelete.pdf")
For i As Integer = pdfDocumentProcessor.Document.Pages.Count To 0+1 Step -1
If i Mod 2 <> 0 Then
pdfDocumentProcessor.DeletePage(i)
End If
Next i
pdfDocumentProcessor.SaveDocument("..\..\docs\Deleted.pdf")
End Using
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:
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");
}
Using pdfDocumentProcessor As New PdfDocumentProcessor()
' Load a document
pdfDocumentProcessor.LoadDocument("Document.pdf")
' Access the first page properties
Dim pageFacade As PdfPageFacade = pdfDocumentProcessor.DocumentFacade.Pages(0)
' Define an area to clear
Dim cropBox As PdfRectangle = pdfDocumentProcessor.Document.Pages(0).CropBox
Dim halfPage As Double = cropBox.Top / 2
Dim pageRectangle As New PdfRectangle(cropBox.Left, halfPage, cropBox.Right + halfPage, cropBox.Top)
' Set what content type to keep
Dim options As New PdfClearContentOptions() With
{
.ClearAnnotations = False,
.ClearGraphics = False,
.ClearImages = False
}
' Clear the page area
pageFacade.ClearContent(pageRectangle, options)
pdfDocumentProcessor.SaveDocument("Document_cleared.pdf")
End Using