> 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/ios/get-started/samples/pagelabelstest.md).

# PageLabels

Sample Obj-C code for using Apryse SDK to work with PDF page labels. PDF page labels can be used to describe a page, which is used to allow for non-sequential page numbering or the addition of arbitra

Sample Obj-C code for using Apryse SDK to work with PDF page labels. PDF page labels can be used to describe a page, which is used to allow for non-sequential page numbering or the addition of arbitrary labels for a page (such as the inclusion of Roman numerals at the beginning of a book). Learn more about our [iOS SDK](/ios/guides.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

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

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

//-----------------------------------------------------------------------------------
// The sample illustrates how to work with PDF page labels.
//
// PDF page labels can be used to describe a page. This is used to 
// allow for non-sequential page numbering or the addition of arbitrary 
// labels for a page (such as the inclusion of Roman numerals at the 
// beginning of a book). PDFNet PTPageLabel object can be used to specify 
// the numbering style to use (for example, upper- or lower-case Roman, 
// decimal, and so forth), the starting number for the first page,
// and an arbitrary prefix to be pre-appended to each number (for 
// example, "A-" to generate "A-1", "A-2", "A-3", and so forth.)
//-----------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
    @autoreleasepool {
        int ret = 0;
        [PTPDFNet Initialize: 0];

        @try  
        {
            //-----------------------------------------------------------
            // Example 1: Add page labels to an existing or newly created PDF
            // document.
            //-----------------------------------------------------------
            {
                PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
                [doc InitSecurityHandler];

                // Create a page labeling scheme that starts with the first page in 
                // the document (page 1) and is using uppercase roman numbering 
                // style. 
                PTPageLabel *L1 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptroman_uppercase prefix: @"My Prefix " start_at: 1];
                [doc SetPageLabel: 1 label: L1];

                // Create a page labeling scheme that starts with the fourth page in 
                // the document and is using decimal Arabic numbering style. 
                // Also the numeric portion of the first label should start with number 
                // 4 (otherwise the first label would be "My Prefix 1"). 
                PTPageLabel *L2 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptdecimal prefix: @"My Prefix " start_at: 4];
                [doc SetPageLabel: 4 label: L2];

                // Create a page labeling scheme that starts with the seventh page in 
                // the document and is using alphabetic numbering style. The numeric 
                // portion of the first label should start with number 1. 
                PTPageLabel *L3 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptalphabetic_uppercase prefix: @"My Prefix " start_at: 1];
                [doc SetPageLabel: 7 label: L3];

                [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf" flags: e_ptlinearized];
                NSLog(@"Done. Result saved in newsletter_with_pagelabels.pdf...");
            }

            //-----------------------------------------------------------
            // Example 2: Read page labels from an existing PDF document.
            //-----------------------------------------------------------
            {
                PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
                [doc InitSecurityHandler];

                PTPageLabel *label;
                int page_num = [doc GetPageCount];
                int i=1;
                for (; i<=page_num; ++i) 
                {
                    NSLog(@"Page number: %d", i); 
                    label = [doc GetPageLabel: i];
                    if ([label IsValid]) {
                        NSLog(@" Label: %@", [label GetLabelTitle: i]); 
                    }
                    else {
                        NSLog(@" No Label."); 
                    }
                }
            }

            //-----------------------------------------------------------
            // Example 3: Modify page labels from an existing PDF document.
            //-----------------------------------------------------------
            {
                PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
                [doc InitSecurityHandler];

                // Remove the alphabetic labels from example 1.
                [doc RemovePageLabel: 7]; 

                // Replace the Prefix in the decimal labels (from example 1).
                PTPageLabel *label = [doc GetPageLabel: 4];
                if ([label IsValid]) {
                    [label SetPrefix: @"A"];
                    [label SetStart: 1];
                }

                // Add a new label
                PTPageLabel *new_label = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptdecimal prefix: @"B" start_at: 1];
                [doc SetPageLabel: 10 label: new_label];  // starting from page 10.

                [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels_modified.pdf" flags: e_ptlinearized];
                NSLog(@"Done. Result saved in newsletter_with_pagelabels_modified.pdf...");

                int page_num = [doc GetPageCount];
                int i=1;
                for (; i<=page_num; ++i) 
                {
                    NSLog(@"Page number: %d", i); 
                    label = [doc GetPageLabel: i];
                    if ([label IsValid]) {
                        NSLog(@" Label: %@", [label GetLabelTitle: i]);
                    }
                    else {
                        NSLog(@" No Label."); 
                    }
                }
            }

            //-----------------------------------------------------------
            // Example 4: Delete all page labels in an existing PDF document.
            //-----------------------------------------------------------
            {
                PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
                [[doc GetRoot] EraseDictElementWithKey: @"PageLabels"];
                
                [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels_removed.pdf" flags: e_ptlinearized];
                NSLog(@"Done. Result saved in newsletter_with_pagelabels_removed.pdf...");
            }
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        [PTPDFNet Terminate: 0];            
        return ret;
    }
}
```

{% endcode %}
{% endtab %}

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

```swift
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

//-----------------------------------------------------------------------------------
// The sample illustrates how to work with PDF page labels.
//
// PDF page labels can be used to describe a page. This is used to
// allow for non-sequential page numbering or the addition of arbitrary
// labels for a page (such as the inclusion of Roman numerals at the
// beginning of a book). PDFNet PTPageLabel object can be used to specify
// the numbering style to use (for example, upper- or lower-case Roman,
// decimal, and so forth), the starting number for the first page,
// and an arbitrary prefix to be pre-appended to each number (for
// example, "A-" to generate "A-1", "A-2", "A-3", and so forth.)
//-----------------------------------------------------------------------------------
func runPageLabelsTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        
        
        do {
            try PTPDFNet.catchException {
                //-----------------------------------------------------------
                // Example 1: Add page labels to an existing or newly created PDF
                // document.
                //-----------------------------------------------------------
                do {
                    let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                    doc.initSecurityHandler()
                    
                    // Create a page labeling scheme that starts with the first page in
                    // the document (page 1) and is using uppercase roman numbering
                    // style.
                    let L1 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptroman_uppercase, prefix: "My Prefix ", start_at: 1)
                    doc.setPageLabel(1, label: L1)
                    
                    // Create a page labeling scheme that starts with the fourth page in
                    // the document and is using decimal Arabic numbering style.
                    // Also the numeric portion of the first label should start with number
                    // 4 (otherwise the first label would be "My Prefix 1").
                    let L2 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptdecimal, prefix: "My Prefix ", start_at: 4)
                    doc.setPageLabel(4, label: L2)
                    
                    // Create a page labeling scheme that starts with the seventh page in
                    // the document and is using alphabetic numbering style. The numeric
                    // portion of the first label should start with number 1.
                    let L3 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptalphabetic_uppercase, prefix: "My Prefix ", start_at: 1)
                    doc.setPageLabel(7, label: L3)
                    
                    doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path, flags: e_ptlinearized.rawValue)
                    print("Done. Result saved in newsletter_with_pagelabels.pdf...")
                }
                
                //-----------------------------------------------------------
                // Example 2: Read page labels from an existing PDF document.
                //-----------------------------------------------------------
                do {
                    let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
                    doc.initSecurityHandler()
                    
                    let page_num = doc.getPageCount()
                    
                    for i in 1...page_num {
                        print("Page number: \(i)")
                        let label: PTPageLabel = doc.getPageLabel(i)
                        if label.isValid() {
                            print(" Label: \(String(describing: label.getTitle(i)))")
                        }
                        else {
                            print(" No Label.")
                        }
                    }
                }
                
                //-----------------------------------------------------------
                // Example 3: Modify page labels from an existing PDF document.
                //-----------------------------------------------------------
                do {
                    let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
                    doc.initSecurityHandler()
                    
                    // Remove the alphabetic labels from example 1.
                    doc.removePageLabel(7)
                    
                    // Replace the Prefix in the decimal labels (from example 1).
                    var label: PTPageLabel = doc.getPageLabel(4)
                    if label.isValid() {
                        label.setPrefix("A")
                        label.setStart(1)
                    }
                    
                    // Add a new label
                    let new_label = PTPageLabel.create(doc.getSDFDoc(), style: e_ptdecimal, prefix: "B", start_at: 1)
                    doc.setPageLabel(10, label: new_label)  // starting from page 10.
                    
                    doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels_modified.pdf").path, flags: e_ptlinearized.rawValue)
                    print("Done. Result saved in newsletter_with_pagelabels_modified.pdf...")

                    let page_num = doc.getPageCount()
                    
                    for i in 1...page_num {
                        print("Page number: \(i)")
                        label = doc.getPageLabel(i)
                        if label.isValid() {
                            print(" Label: \(String(describing: label.getTitle(i)))")
                        }
                        else {
                            print(" No Label.")
                        }
                    }
                }
                
                //-----------------------------------------------------------
                // Example 4: Delete all page labels in an existing PDF document.
                //-----------------------------------------------------------
                do {
                    let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
                    doc.getRoot().eraseDictElement(withKey: "PageLabels")
                    
                    doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels_removed.pdf").path, flags: e_ptlinearized.rawValue)
                    print("Done. Result saved in newsletter_with_pagelabels_removed.pdf...")
                }
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        return ret
    }
}
```

{% 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/ios/get-started/samples/pagelabelstest.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.
