> 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/elementedittest.md).

# ElementEdit

Here is a complete PDF and DOCX editing guide using Obj-C. Modify properties, edit text, bookmarks,annotation and lot more using COS API

Sample Obj-C code for using Apryse SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with [interactive PDF editor widgets](/ios/get-started/samples.md#pdfview). Some of Apryse SDK's other functions for programmatically editing PDFs include the [Cos/SDF low-level API](/ios/get-started/samples.md#sdf), [page manipulation](/ios/get-started/samples.md#pdfpage), and more. 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-2012 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 code shows how to edit the page display list and how to modify graphics state 
// attributes on existing Elements. In particular the sample program strips all images from 
// the page, changes path fill color to red, and changes text color to blue. 
//---------------------------------------------------------------------------------------

static void ProcessElementEditTestElements(PTElementReader *reader, PTElementWriter *writer, NSMutableSet *visited)
{
    PTElement *element;    
    while ((element = [reader Next]))     // Read page contents
    {
        switch ([element GetType])
        {        
        case e_ptimage: 
        case e_ptinline_image: 
            // remove all images by skipping them            
            break;
        case e_ptpath:
            {
                // Set all paths to red color.
                PTGState *gs = [element GetGState];
                [gs SetFillColorSpace: [PTColorSpace CreateDeviceRGB]];
                PTColorPt *cp = [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0];
                [gs SetFillColorWithColorPt: cp];
                [writer WriteElement: element];
                break;
            }
        case e_pttext_obj:
            {
                // Set all text to blue color.
                PTGState *gs = [element GetGState];
                [gs SetFillColorSpace: [PTColorSpace CreateDeviceRGB]];
                PTColorPt *cp = [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0];
                [gs SetFillColorWithColorPt: cp];
                [writer WriteElement: element];
                break;
            }
        case e_ptform:
            {
                [writer WriteElement: element]; // write Form XObject reference to current stream

                PTObj *form_obj = [element GetXObject];

                if (![visited containsObject:@([form_obj GetObjNum])]) // if this XObject has not been processed
                {
                    // recursively process the Form XObject
                    [visited addObject:@([form_obj GetObjNum])];
                    PTElementWriter *new_writer = [[PTElementWriter alloc] init];
                    [reader FormBegin];
                    [new_writer WriterBeginWithSDFObj:form_obj compress:YES resources: NULL];
                    
                    [reader ClearChangeList];
                    [new_writer SetDefaultGState: reader];
                    
                    ProcessElementEditTestElements(reader, new_writer, visited);
                    [reader End];
                    [new_writer End];
                }

                break; 
            }
        default:
                [writer WriteElement: element];
        }
    }
}

int main(int argc, char *argv[])
{
    @autoreleasepool {
    
        int ret = 0;
        [PTPDFNet Initialize: 0];

        @try 
        {
            NSLog(@"Opening the input file...");
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];

            PTElementWriter *writer = [[PTElementWriter alloc] init];
            PTElementReader *reader = [[PTElementReader alloc] init];
            NSMutableSet *visited = [[NSMutableSet alloc] init];

            PTPageIterator *itr = [doc GetPageIterator: 1];
            while ([itr HasNext])
            {
                PTPage *page = [itr Current];
                [visited addObject:@([[page GetSDFObj] GetObjNum])];

                [reader ReaderBeginWithPage: page ocg_context: 0];
                [writer WriterBeginWithPage: page placement: e_ptreplacement page_coord_sys: NO compress: YES resources: [page GetResourceDict] ];
                
                ProcessElementEditTestElements(reader, writer, visited);

                [writer End];
                [reader End];
                
                [itr Next];
            }

            // Save modified document
            [doc SaveToFile: @"../../TestFiles/Output/newsletter_edited.pdf" flags: e_ptremove_unused];        
            NSLog(@"Done. Result saved in newsletter_edited.pdf...");
        }
        @catch(NSException *e)
        {
            NSLog(@"Caught PDFNet exception: %@", 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 code shows how to edit the page display list and how to modify graphics state
// attributes on existing Elements. In particular the sample program strips all images from
// the page, changes path fill color to red, and changes text color to blue.
//---------------------------------------------------------------------------------------

private func ProcessElementEditTestElements(reader: PTElementReader, writer: PTElementWriter, visited: NSMutableSet) {
    while let element = reader.next() {
        switch element.getType() {
        case e_ptimage, e_ptinline_image:
            // remove all images by skipping them
            break
        case e_ptpath:
            // Set all paths to red color.
            let gs: PTGState = element.getGState()
            gs.setFill(PTColorSpace.createDeviceRGB())
            let cp = PTColorPt(x: 1, y: 0, z: 0, w: 0)
            gs.setFillColor(with: cp)
            writer.write(element)
        case e_pttext_obj:
            // Set all text to blue color.
            let gs: PTGState = element.getGState()
            gs.setFill(PTColorSpace.createDeviceRGB())
            let cp = PTColorPt(x: 0, y: 0, z: 1, w: 0)
            gs.setFillColor(with: cp)
            writer.write(element)
        case e_ptform:
            writer.write(element)   // write Form XObject reference to current stream
            
            let form_obj: PTObj = element.getXObject()
            
            if !visited.contains(form_obj.getNum()) {
                // recursively process the Form XObject
                visited.add(form_obj.getNum())
                let new_writer: PTElementWriter = PTElementWriter()
                reader.formBegin()
                new_writer.writerBegin(withSDFObj: form_obj, compress: true, resources: nil)
                ProcessElementEditTestElements(reader: reader, writer: new_writer, visited: visited)
                reader.end()
                new_writer.end()
            }
        default:
            writer.write(element)
        }
    }
}

func runElementEditTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        

        do {
            try PTPDFNet.catchException {
                print("Opening the input file...")
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                let writer: PTElementWriter = PTElementWriter()
                let reader: PTElementReader = PTElementReader()
                let visited = NSMutableSet()
                
                let itr: PTPageIterator = doc.getPageIterator(1)
                while itr.hasNext() {
                    let page: PTPage = itr.current()
                    visited.add(page.getSDFObj().getNum())
                    
                    reader.readerBegin(with: page, ocg_context: nil)
                    writer.begin(page, placement: e_ptreplacement, page_coord_sys: false, compress: true)
                    
                    ProcessElementEditTestElements(reader: reader, writer: writer, visited: visited)
                    
                    writer.end()
                    reader.end()
                    
                    itr.next()
                }
                
                // Save modified document
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_edited.pdf").path, flags: e_ptremove_unused.rawValue)
                print("Done. Result saved in newsletter_edited.pdf...")
            }
        } catch let e as NSError {
            print("Caught PDFNet exception: \(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/elementedittest.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.
