Some test text!
Core / Guides / Access a PDF page
To access a PDF page.
PDFDoc doc = new PDFDoc(filename);
// Access a PDF page
Page page = doc.GetPage(page_num);
Merge, copy, delete, and rearrange PDF pages
Full code sample which illustrates how to copy pages from one document to another, how to delete, and rearrange pages and how to use ImportPages() method for very efficient copy and merge operations.
A high-level PDF document contains a sequence of Page objects, as illustrated in the following figure:
PDFDoc Page sequence.
To find the number of pages in a PDF document, call PDFDoc.GetPageCount()
.
To retrieve a specific page of a document, use PDFDoc.GetPage(page_num)
. Page numbers in the document's page sequence are indexed from 1. If the given page number doesn't index a page in the current document, GetPage(page_num)
returns null. For example:
Page page = doc.GetPage(page_num);
if (page != null)
{
Console.WriteLine("Document does contain page#: {0}", page_num);
}
else
{
Console.WriteLine("Document does not contain page#: {0}", page_num);
}
While GetPage(i)
is convenient for retrieving an individual page, it's an inefficient way to enumerate every page of a document. It's better to traverse the pages with a PageIterator.
To do so, simply call PDFDoc.GetPageIterator()
. This returns a PageIterator object, which provides HasNext()
, Next()
and Current()
methods. The following code snippet shows how to print the page size for every page in document page sequence:
for (PageIterator itr=doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
Rect mediabox = itr.Current().GetMediaBox();
Console.WriteLine("Media box: {0}, {1}, {2}, {3}", mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2);
}
(This code finds the page size using the page's media box, which we'll talk more about in the following sections.)
To jump to a specific page with a PageIterator, call PDFDoc.GetPageIterator(page_num)
. If no such page exists, the index of the page will return 0. For example:
PageIterator itr = doc.GetPageIterator(page_num);
if (itr.GetPageNumber() > 0)
{
Console.WriteLine("Document does contain page#: {0}", page_num);
}
else
{
Console.WriteLine("Document does not contain page#: {0}", page_num);
}
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales