Page Manipulation
- 5 minutes to read
The topic describes the following page manipulation capabilities:
- Copy pages from one document to another PDF
- Add/Insert a new page into a document
- Render a new page with graphics
- Delete a page from a document
- Rotate a page in a document
Copy pages from one document to another PDF
To copy pages:
- Create two PdfDocumentProcessor instances;
- Load a source document to the first instance and a target document to the second by calling the PdfDocumentProcessor.LoadDocument overload method;
Retrieve pages within the source document using the PdfDocument.Pages property.
This property returns an IList<PdfPage> object. You can use the collection methods to perform most page manipulations (for example, split one document into multiple PDF files or add an extracted page to another PDF). To access the document, use the PdfDocumentProcessor.Document property.
- Save the resulting document by calling the PdfDocumentProcessor.SaveDocument overload method.
Example
This example shows how to copy a page from one PDF document to another PDF.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-copy-a-page-from-one-document-to-another-pdf-t581087
Imports DevExpress.Pdf
Namespace CopyPage
Friend Class Program
Shared Sub Main(ByVal args() As String)
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
End Sub
End Class
End Namespace
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:
- Load an original document into the PDF Document API component by calling the overloaded PdfDocumentProcessor.LoadDocument method;
- Retrieve a page by iterating through the page collection the PdfDocument.Pages property returns. To access the document, use the PdfDocumentProcessor.Document property;
- Specify the rotation angle (in degrees) using the PdfPageTreeObject.Rotate property.
- Save the resulting document by calling the PdfDocumentProcessor.SaveDocument overload method.
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");
}
}
}
}