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.
About 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:
1Page page = doc.GetPage(page_num);
2if (page != null)
3{
4 Console.WriteLine("Document does contain page#: {0}", page_num);
5}
6else
7{
8 Console.WriteLine("Document does not contain page#: {0}", page_num);
9}
1Page page = doc.GetPage(page_num);
2if (page != NULL)
3{
4 printf("Document does contain page#: %d", page_num);
5}
6else
7{
8 printf("Document does not contain page#: %d", page_num);
9}
1page := doc.GetPage(page_num)
2if page != nil
3{
4 fmt.Println("Document does contain page#: " + page_num);
5}
6else
7{
8 fmt.Println("Document does not contain page#: " + page_num);
9}
1Page page = doc.getPage(page_num);
2if (page != null)
3{
4 println("Document does contain page#: %d", page_num);
5}
6else
7{
8 println("Document does not contain page#: %d", page_num);
9}
1async function main() {
2 const page = await doc.getPage(page_num);
3 if (page != null)
4 {
5 console.log("Document does contain page#: %d", page_num);
6 }
7 else
8 {
9 console.log("Document does not contain page#: %d", page_num);
10 }
11}
12PDFNet.runWithCleanup(main);
1PTPage *page = [doc GetPage: page_num];
2if (page != nil)
3{
4 printf("Document does contain page#: %d", page_num);
5}
6else
7{
8 printf("Document does not contain page#: %d", page_num);
9}
1$page = $doc->GetPage($page_num);
2if ($page != NULL)
3{
4 printf("Document does contain page#: %d", page_num);
5}
6else
7{
8 printf("Document does not contain page#: %d", page_num);
9}
1page = doc.GetPage(page_num)
2if page is None:
3 print("Document does contain page#: %d" % (page_num))
4else:
5 print("Document does not contain page#: %d" % (page_num))
1page = doc.GetPage(page_num)
2if page.nil?
3 puts "Document does contain page#: %d" % [page_num]
4else
5 puts "Document does not contain page#: %d" % [page_num]
6end
1Dim page As Page = doc.GetPage(page_num)
2if page Is Nothing Then
3 Console.WriteLine("Document does contain page#: {0}", page_num)
4Else
5 Console.WriteLine("Document does not contain page#: {0}", page_num)
6End If
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:
(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: