Table of Contents

Class PdfDocumentPageCollection

Namespace
Syncfusion.Pdf
Assembly
Syncfusion.Pdf.Portable.dll

Implements a virtual collection of all pages in the document.

public class PdfDocumentPageCollection : IEnumerable
Inheritance
PdfDocumentPageCollection
Implements
Inherited Members

Examples

//Load an existing document.
PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf");
//Create a new document.
PdfDocument doc = new PdfDocument();
int startIndex = 0;
int endIndex = ldoc.Pages.Count - 1;
//Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = doc.Pages;
int newCount = 0;
//Parse through pages from page collection.
foreach (PdfPageBase page in docPages)
{
   newCount++;
}
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
ldoc.Close(true);
'Load an existing document.
Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = ldoc.Pages.Count - 1
'Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex)
'Get all the pages from new document.
Dim docPages As PdfDocumentPageCollection = doc.Pages
Dim newCount As Integer = 0
'Parse through pages from page collection.
For Each page As PdfPageBase In docPages
newCount += 1
Next
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
ldoc.Close(True)

Properties

Count

Gets the total number of the pages.

public int Count { get; }

Property Value

int

Examples

//Load an existing document.
PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf");
//Create a new document.
PdfDocument doc = new PdfDocument();
int startIndex = 0;
int endIndex = ldoc.Pages.Count - 1;
//Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = doc.Pages;
//Get total page count.
int newCount = docPages.Count;
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
ldoc.Close(true);
'Load an existing document.
Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = ldoc.Pages.Count - 1
'Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex)
'Get all the pages fron new document.
Dim docPages As PdfDocumentPageCollection = doc.Pages
'Get total page count.
Dim newCount As Integer = docPages.Count
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
ldoc.Close(True)
See Also

this[int]

Gets a page by its index in the document.

public PdfPage this[int index] { get; }

Parameters

index int

Property Value

PdfPage

Examples

//Load an existing document.
PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf");
//Create a new document.
PdfDocument doc = new PdfDocument();
int startIndex = 0;
int endIndex = ldoc.Pages.Count - 1;
//Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = doc.Pages;
//Gets a page by its index in the document.
PdfPage page = docPages[0] as PdfPage;
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
ldoc.Close(true);
'Load an existing document.
Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = ldoc.Pages.Count - 1
'Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex)
'Get all the pages fron new document.
Dim docPages As PdfDocumentPageCollection = doc.Pages
'Gets a page by its index in the document.
Dim page As PdfPage = TryCast(docPages(0), PdfPage)
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
ldoc.Close(True)
See Also

Methods

Add()

Creates a page and adds it to the last section in the document.

public PdfPage Add()

Returns

PdfPage

Created PdfPage object.

Examples

//Load an existing document.
PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf");
//Create a new document.
PdfDocument doc = new PdfDocument();
int startIndex = 0;
int endIndex = ldoc.Pages.Count - 1;
//Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = doc.Pages;
//Add new page in the document.
docPages.Add();
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
ldoc.Close(true);
'Load an existing document.
Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
Dim startIndex As Integer = 0
Dim endIndex As Integer = ldoc.Pages.Count - 1
'Importing pages from source document.
doc.ImportPageRange(ldoc, startIndex, endIndex)
'Get all the pages fron new document.
Dim docPages As PdfDocumentPageCollection = doc.Pages
'Add new page in the document.
docPages.Add()
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
ldoc.Close(True)
See Also

GetEnumerator()

Returns an enumerator that iterates through a collection.

public IEnumerator GetEnumerator()

Returns

IEnumerator

An IEnumerator object that can be used to iterate through the collection.

See Also

IndexOf(PdfPage)

Gets the index of the page in the document.

public int IndexOf(PdfPage page)

Parameters

page PdfPage

The current page.

Returns

int

Index of the page in the document if exists, -1 otherwise.

Examples

//Create new a document.
PdfDocument document = new PdfDocument();
//Load an existing document.
PdfLoadedDocument doc = new PdfLoadedDocument("Input.pdf");
//Get first page from source document.
PdfLoadedPage lpage = doc.Pages[0] as PdfLoadedPage;
//Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1);
//Get all the pages from new document.
PdfDocumentPageCollection docPages = document.Pages;
for (int i = 0; i != docPages.Count; i++)
{
//Get index for all the pages from source document.
int index = docPages.IndexOf(docPages[0]);
}
//Save and Close the document.
document.Save("Output.pdf");
document.Close(true);
'Create new a document.
Dim document As PdfDocument = New PdfDocument()
'Load an existing document.
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Get first page from source document.
Dim lpage As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)
'Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1)
'Get all the pages from new document.
Dim docPages As PdfDocumentPageCollection = document.Pages
For i As Integer = 0 To docPages.Count - 1
' Get index for all the pages from source document.
Dim index As Integer = docPages.IndexOf(docPages(0))
Next i
'Save and Close the document.
document.Save("Output.pdf")
document.Close(True)
See Also

Insert(int, PdfPage)

Inserts a page at the specified index to the last section in the document.

public void Insert(int index, PdfPage page)

Parameters

index int

The index of the page in the section.

page PdfPage

The PdfPage.

Examples

//Create new a document.
PdfDocument document = new PdfDocument();
//Load an existing document.
PdfLoadedDocument doc = new PdfLoadedDocument("Input.pdf");
//Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = document.Pages;
//Insert page to the specified index.
docPages.Insert(1, new PdfPage());
docPages.Insert(10, new PdfPage());
//Save and Close the document.
document.Save("Output.pdf");
document.Close(true);
'Create new a document.
Dim document As PdfDocument = New PdfDocument()
'Load an existing document.
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1)
'Get all the pages fron new document.
Dim docPages As PdfDocumentPageCollection = document.Pages
'Insert page to the specified index.
docPages.Insert(1, New PdfPage())
docPages.Insert(10, New PdfPage())
'Save and Close the document.
document.Save("Output.pdf")
document.Close(True)

Remarks

Page index start from 0 to (TotalPageCount - 1). Selected index must be within 0 to (TotalPageCount - 1) range.

See Also

Insert(int, PdfPageBase)

Inserts a loaded page in the new PDF document at specified index.

public void Insert(int index, PdfPageBase loadedPage)

Parameters

index int

The index at which the page to be inserted

loadedPage PdfPageBase

The page to be inserted

Examples

//Create new a document.
PdfDocument document = new PdfDocument();
//Load an existing document.
PdfLoadedDocument doc = new PdfLoadedDocument("Input.pdf");
//Get first page from source document.
PdfLoadedPage lpage = doc.Pages[0] as PdfLoadedPage;
//Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1);
//Get all the pages fron new document.
PdfDocumentPageCollection docPages = document.Pages;
//Insert page to the specified index.
docPages.Insert(1, lpage);
//Save and Close the document.
document.Save("Output.pdf");
document.Close(true);
'Create new a document.
Dim document As PdfDocument = New PdfDocument()
'Load an existing document.
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Get first page from source document.
Dim lpage As PdfLoadedPage = TryCast(doc.Pages(0), PdfLoadedPage)
'Importing pages from source document.
document.ImportPageRange(doc, 0, doc.Pages.Count - 1)
'Insert page to the specified index.
document.Pages.Insert(1, lpage)
'Save and Close the document.
document.Save("Output.pdf")
document.Close(True)

Remarks

Page index start from 0 to (TotalPageCount - 1). Selected index must be within 0 to (TotalPageCount - 1) range.

See Also

Events

PageAdded

Represents the method that executes on a PdfDocument when a new page is created.

public event PageAddedEventHandler PageAdded

Event Type

PageAddedEventHandler

Examples

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add the event
document.Pages.PageAdded += new PageAddedEventHandler(Pages_PageAdded);
//Creates a new page and adds it as the last page of the document
PdfPage page = document.Pages.Add();
//Create font with Bold font style.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
//Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, new PointF(10, 10));
page = document.Pages.Add();
//Saves the document
document.Save("Sample.pdf");
document.Close(true);
//Event handler for PageAdded event
void Pages_PageAdded(object sender, PageAddedEventArgs args)
{
PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 10);
args.Page.Graphics.DrawString("New Page", font, PdfBrushes.Black, new PointF(100, 100));
}
'Create a new PDF document.
Dim document As New PdfDocument()
'Add the event
document.Pages.PageAdded += New PageAddedEventHandler(Pages_PageAdded)
'Creates a new page and adds it as the last page of the document
Dim page As PdfPage = document.Pages.Add()
'Create font with Bold font style.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12F, PdfFontStyle.Bold)
'Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, New PointF(10, 10))
page = document.Pages.Add()
'Saves the document
document.Save("Sample.pdf")
document.Close(True)
'Event handler for PageAdded event
Private Sub Pages_PageAdded(sender As Object, args As PageAddedEventArgs)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Courier, 10)
args.Page.Graphics.DrawString("New Page", font, PdfBrushes.Black, New PointF(100, 100))
End Sub
See Also

See Also