Some test text!

menu
search

Access a PDF page

Only available with the Full API

To access a PDF page.

const doc = await PDFNet.PDFDoc.createFromURL(filename);

// Access a PDF page
const page = await 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.

linkAbout working with pages

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:

const page = await doc.getPage(page_num);
if (page != null)
{
  console.log("Document does contain page#: %d", page_num);
}
else
{
  console.log("Document does not contain page#: %d", 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:

const itr = await doc.getPageIterator();
for (itr; await itr.hasNext(); itr.next())
{
  const curritr = await itr.current();
  const mediabox = curritr.getMediaBox();
  console.log("Media box: %f, %f, %f, %f", 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, PageIterator.GetPageNumber() returns 0. For example:

const itr = await doc.getPageIterator(page_num);
if (itr.GetPageNumber() > 0)
{
  console.log("Document does contain page#: %d", page_num);
}
else
{
  console.log("Document does not contain page#: %d", page_num);
}

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close