Back to Devexpress

PdfDocument.Pages Property

officefileapi-devexpress-dot-pdf-dot-pdfdocument.md

latest10.9 KB
Original Source

PdfDocument.Pages Property

Obtains to the collection of document pages.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Core.dll

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public IList<PdfPage> Pages { get; }
vb
Public ReadOnly Property Pages As IList(Of PdfPage)

Property Value

TypeDescription
IList<PdfPage>

A collection of PdfPage objects.

|

Remarks

The Pages property obtian a list of document pages. Access a specific page by its index.

Add a New Page

Use the following API to append or insert a new page:

MethodDescription
PdfDocumentProcessor.AddNewPageAppends an empty page.
PdfDocumentProcessor.InsertNewPageInserts a page at a specified position (page number).

The following code adds a new page to a document:

csharp
using DevExpress.Pdf;

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

Using processor As New PdfDocumentProcessor()
    processor.LoadDocument("..\..\Document.pdf")
    processor.AddNewPage(PdfPaperSize.A4)
    processor.SaveDocument("..\..\Result.pdf")
End Using

Copy a Page from Another Document

The Insert(Int32, T) method allows you to add an extracted page to another PDF file. When you add a page from another document, all page content, including bookmarks referring to this page, is copied to the resulting document.

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

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

Extract a Page

Use the Add(T) method to add pages to the collection. When you add a page from another document, all page content, including bookmarks referring to this page, is copied to the resulting document.

View Example

csharp
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]);
    }
}
vb
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

Delete a Page

Call the PdfDocumentProcessor.DeletePage method to remove a page from a document. The PdfDocumentProcessor.DeletePages method removes a list of pages form a document.

The code sample below deletes odd-numbered pages in a document, starting with the last odd-numbered page.

View Example: Delete Pages from PDF

csharp
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");
}
vb
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 following code snippets (auto-collected from DevExpress Examples) contain references to the Pages property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

pdf-document-api-extract-pages-from-document/CS/ExtractDocumentPages/Program.cs#L19

csharp
target.CreateEmptyDocument("..\\..\\ExtractedPage" + (i + 1).ToString() + ".pdf");
    target.Document.Pages.Add(source.Document.Pages[i]);
}

how-to-custom-draw-in-pdf-viewer/CS/PDF_Viewer/Form1.cs#L46

csharp
{
    PdfPage page = processor.Document.Pages[rect.PageIndex];
    PdfRectangle pageCropBox = page.CropBox;

pdf-document-api-add-graphics-to-landscape-and-portrait-pages/CS/CreateGraphics/Program.cs#L27

csharp
{
    IList<PdfPage> pages = processor.Document.Pages;
    for (int i = 0; i < pages.Count; i++)

pdf-document-api-copy-pages/CS/CopyPage/Program.cs#L17

csharp
target.LoadDocument("..\\..\\..\\Document2.pdf");
target.Document.Pages.Insert(3, source.Document.Pages[0]);
target.SaveDocument("..\\..\\..\\Result.pdf");

pdf-document-api-replace-form-field-with-image/CS/ReplaceFormFieldWithImage/Program.cs#L25

csharp
PdfRectangle rect = widget.Rectangle;
PdfPage page = processor.Document.Pages[widget.PageNumber - 1];
double x = rect.Left - page.CropBox.Left;

pdf-document-api-extract-pages-from-document/VB/ExtractDocumentPages/Program.vb#L14

vb
target.CreateEmptyDocument("..\..\ExtractedPage" & (i + 1).ToString() & ".pdf")
    target.Document.Pages.Add(source.Document.Pages(i))
End Using

how-to-custom-draw-in-pdf-viewer/VB/PDF_Viewer/Form1.vb#L42

vb
Using graph As PdfGraphics = processor.CreateGraphics()
    Dim page As PdfPage = processor.Document.Pages(rect.PageIndex)
    Dim pageCropBox As PdfRectangle = page.CropBox

pdf-document-api-add-graphics-to-landscape-and-portrait-pages/VB/CreateGraphics/Program.vb#L24

vb
Private Shared Sub AddGraphics(ByVal processor As PdfDocumentProcessor, ByVal text As String, ByVal textBrush As DXSolidBrush)
    Dim pages As IList(Of PdfPage) = processor.Document.Pages
    For i As Integer = 0 To pages.Count - 1

pdf-document-api-copy-pages/VB/CopyPage/Program.vb#L12

vb
target.LoadDocument("..\..\..\Document2.pdf")
target.Document.Pages.Insert(3, source.Document.Pages(0))
target.SaveDocument("..\..\..\Result.pdf")

pdf-document-api-replace-form-field-with-image/VB/ReplaceFormFieldWithImage/Program.vb#L23

vb
Dim rect As PdfRectangle = widget.Rectangle
Dim page As PdfPage = processor.Document.Pages(widget.PageNumber - 1)
Dim x As Double = rect.Left - page.CropBox.Left

See Also

Organize Pages in PDF Documents

PdfDocument Class

PdfDocument Members

DevExpress.Pdf Namespace