> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/basic-operations/pages.md).

# Access PDF pages to add, copy, delete or rearrange on Server/Desktop

Learn how to efficiently manage PDF pages with code examples. Copy, delete, and rearrange pages using ImportPages() method for seamless operations. The Apryse Server SDK streamlines secure document pr

To access a PDF page.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc doc = new PDFDoc(filename);

// Access a PDF page
Page page = doc.GetPage(page_num);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);

// Access a PDF page
Page page = doc.GetPage(page_num);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewPDFDoc(filename)

// Access a PDF page
page := doc.GetPage(page_num)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);

// Access a PDF page
Page page = doc.getPage(page_num);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);

  // Access a PDF page
  const page = await doc.getPage(page_num);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)

// Access a PDF page
val page = doc.getPage(page_num)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];

// Access a PDF page
PTPage *page = [doc GetPage: page_num];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)

// Access a PDF page
let page: PTPage = doc.getPage(page_num)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new PDFDoc($filename);

// Access a PDF page
$page = $doc->GetPage($page_num);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)

# Access a PDF page
page = doc.GetPage(page_num)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new(filename)

# Access a PDF page
page = doc.GetPage(page_num)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim doc As PDFDoc = New PDFDoc(filename)

' Access a PDF page
Dim page As Page = doc.GetPage(page_num)
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Merge, copy, delete, and rearrange PDF pages](/core/get-started/samples/pdfpagetest.md) 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. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

## About working with pages

A high-level PDF document contains a sequence of Page objects, as illustrated in the following figure:

![](https://3779731113-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fziw3GiL98Xfj63F3He8h%2Fuploads%2Fgit-blob-ac189ed4a6c6c0b49e973d38c30f51c543a85a33%2F53146f7976a63f3be37adb38823899c1f02acc1a-394x193.gif?alt=media)

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:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

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

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
Page page = doc.GetPage(page_num);
if (page != NULL)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
page := doc.GetPage(page_num)
if page != nil
{
  fmt.Println("Document does contain page#: " + page_num);
}
else
{
  fmt.Println("Document does not contain page#: " + page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Page page = doc.getPage(page_num);
if (page != null)
{
  println("Document does contain page#: %d", page_num);
}
else
{
  println("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  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);
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val page = doc.getPage(page_num)
if (page != null)
{
  println("Document does contain page#: ${page_num}")
}
else
{
  println("Document does not contain page#: ${page_num}")
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPage *page = [doc GetPage: page_num];
if (page != nil)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let page: PTPage = doc.getPage(page_num)
if (page != nil)
{
  print("Document does contain page#: \(page_num)", page_num)
}
else
{
  print("Document does not contain page#: \(page_num)", page_num)
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$page = $doc->GetPage($page_num);
if ($page != NULL)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
page = doc.GetPage(page_num)
if page is None:
  print("Document does contain page#: %d" % (page_num))
else:
  print("Document does not contain page#: %d" % (page_num))
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
page = doc.GetPage(page_num)
if page.nil?
  puts "Document does contain page#: %d" % [page_num]
else
  puts "Document does not contain page#: %d" % [page_num]
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim page As Page = doc.GetPage(page_num)
if page Is Nothing Then
  Console.WriteLine("Document does contain page#: {0}", page_num)
Else
  Console.WriteLine("Document does not contain page#: {0}", page_num)
End If
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

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

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PageIterator itr;
for (itr=doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
  Rect mediabox = itr.Current().GetMediaBox();
  printf("Media box: %f, %f, %f, %f", mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2);
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
itr := doc.GetPageIterator()
for itr.HasNext() {
  mediabox := itr.Current().GetMediaBox()
  fmt.Println("Media box: " + mediabox.x1 + mediabox.y1 + mediabox.x2 + mediabox.y2)
  itr.Next()
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
for (PageIterator itr=doc.getPageIterator(); itr.hasNext(); itr.next())
{
  Rect mediabox = itr.current().getMediaBox();
  println("Media box: %f, %f, %f, %f", mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2);
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  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);
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val itr = doc.pageIterator()
while (itr.hasNext())
{
  val mediabox: Rect = page.getMediaBox()
  println("Media box: ${mediabox.x1}, ${mediabox.y1}, ${mediabox.x2}, ${mediabox.y2}")
  itr.Next()
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
for (PageIterator *itr=[doc GetPageIterator: 1]; [itr HasNext]; [itr Next])
{
  PFPDFRect* mediabox = [[itr current] GetMediaBox];
  printf("Media box: %f, %f, %f, %f", [mediabox GetX1], [mediabox GetY1], [mediabox GetX2], [mediabox GetY2]);
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let itr: PTPageIterator = doc.getPageIterator(1)
while itr.hasNext()
{
  let mediabox: Rect = itr.current().getMediaBox()
  print("Media box: \(mediabox.x1), \(mediabox.y1), \(mediabox.x2), \(mediabox.y2)")
  itr.next()
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
for ($itr = $doc->GetPageIterator(); $itr->HasNext(); $itr->Next())	
{
  $mediabox = $itr->Current()->GetMediaBox();
  printf("Media box: %f, %f, %f, %f", $mediabox->x1, $mediabox->y1, $mediabox->x2, $mediabox->y2);
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
itr = doc.GetPageIterator()
while itr.HasNext():
  mediabox = itr.Current().GetMediaBox()
  print("Media box: %f, %f, %f, %f", mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2)
  itr.Next()
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
itr = doc.GetPageIterator()
while itr.HasNext() do
  mediabox = itr.Current().GetMediaBox()
  puts "Media box: %f, %f, %f, %f" % (mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2)
  itr.Next()
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim itr As PageIterator = doc.GetPageIterator()
While itr.HasNext()
  Dim mediabox As Rect = itr.Current().GetMediaBox()
  Console.Write("Media box: %f, %f, %f, %f" % (mediabox.x1, mediabox.y1, mediabox.x2, mediabox.y2))
End While
```

{% endcode %}
{% endtab %}
{% endtabs %}

(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:

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

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

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PageIterator itr = doc.GetPageIterator(page_num);
if (itr.Current().GetIndex() > 0)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
itr := doc.GetPageIterator(page_num)
if itr.GetPageNumber() > 0
{
  fmt.Println("Document does contain page#: " + page_num);
}
else
{
  fmt.Println("Document does not contain page#: " + page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PageIterator itr = doc.getPageIterator(page_num);
if (itr.next().getIndex() > 0)
{
  println("Document does contain page#: %d", page_num);
}
else
{
  println("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const itr = await doc.getPageIterator(page_num);
  if (itr.next().getIndex() > 0)
  {
    console.log("Document does contain page#: %d", page_num);
  }
  else
  {
    console.log("Document does not contain page#: %d", page_num);
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val itr = doc.GetPageIterator(page_num)
if (itr.next().getIndex() > 0)
{
  println("Document does contain page#: ${page_num}")
}
else
{
  println("Document does not contain page#: ${page_num}")
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PageIterator *itr = [doc GetPageIterator: page_num];
if ([[itr Current] GetIndex] > 0)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let page: PageIterator = doc.getPageIterator(page_num)
if (itr.current().getIndex() > 0)
{
  print("Document does contain page#: \(page_num)", page_num)
}
else
{
  print("Document does not contain page#: \(page_num)", page_num)
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$itr = $doc->GetPageIterator($page_num);
if ($itr->Current()->GetIndex() > 0)
{
  printf("Document does contain page#: %d", page_num);
}
else
{
  printf("Document does not contain page#: %d", page_num);
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
itr = doc.GetPageIterator(page_num)
if itr.Current().GetIndex() > 0:
  print("Document does contain page#: %d" % (page_num))
else:
  print("Document does not contain page#: %d" % (page_num))
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
page = doc.GetPageIterator(page_num)
if itr.Current().GetIndex() > 0
  puts "Document does contain page#: %d" % [page_num]
else
  puts "Document does not contain page#: %d" % [page_num]
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim page As PageIterator = doc.GetPageIterator(page_num)
if itr.GetPageNumber() > 0 Then
  Console.WriteLine("Document does contain page#: {0}", page_num)
Else
  Console.WriteLine("Document does not contain page#: {0}", page_num)
End If
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/basic-operations/pages.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
