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

# ContentReplacer

Sample Obj-C code to use Apryse SDK for searching and replacing text strings and images inside existing PDF files (e.g. business cards and other PDF templates). Unlike PDF forms, the ContentReplacer w

Sample Obj-C code to use Apryse SDK for searching and replacing text strings and images inside existing PDF files (e.g. business cards and other PDF templates). Unlike PDF forms, the ContentReplacer works on actual PDF content and is not limited to static rectangular annotation regions. 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 code illustrates how to use the ContentReplacer class to make using 
// 'template' pdf documents easier.
//-----------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
    @autoreleasepool {

        NSString* input_path = @"../../TestFiles/";
        NSString* output_path = @"../../TestFiles/Output/";
        
        int ret = 0;
        [PTPDFNet Initialize: 0];

        // The following example illustrates how to replace an image in a certain region,
        // and how to change template text.
        @try  
        {
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [input_path stringByAppendingPathComponent: @"BusinessCardTemplate.pdf"]];
            [doc InitSecurityHandler];
            
            // first, replace the image on the first page
            PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
            PTPage *page = [doc GetPage: 1];
            PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: [input_path stringByAppendingPathComponent: @"peppers.jpg"]];
            [replacer AddImage: [page GetMediaBox] replacement_image: [img GetSDFObj]];
            // next, replace the text place holders on the second page
            [replacer AddString: @"NAME" replacement_text: @"John Smith"];
            [replacer AddString: @"QUALIFICATIONS" replacement_text: @"Philosophy Doctor"]; 
            [replacer AddString: @"JOB_TITLE" replacement_text: @"Software Developer"]; 
            [replacer AddString: @"ADDRESS_LINE1" replacement_text: @"#100 123 Software Rd"]; 
            [replacer AddString: @"ADDRESS_LINE2" replacement_text: @"Vancouver, BC"]; 
            [replacer AddString: @"PHONE_OFFICE" replacement_text: @"604-730-8989"]; 
            [replacer AddString: @"PHONE_MOBILE" replacement_text: @"604-765-4321"]; 
            [replacer AddString: @"EMAIL" replacement_text: @"info@pdftron.com"]; 
            [replacer AddString: @"WEBSITE_URL" replacement_text: @"http://www.pdftron.com"]; 
            // finally, apply
            [replacer Process: page];

            [doc SaveToFile: [output_path stringByAppendingPathComponent: @"BusinessCard.pdf"] flags: 0];
            NSLog(@"Done. Result saved in BusinessCard.pdf");
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        
        // The following example illustrates how to replace text in a given region
        @try  
        {
            // Open the document that was saved in the previous code sample
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [input_path stringByAppendingPathComponent: @"newsletter.pdf"]];
            [doc InitSecurityHandler];
            
            PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
            PTPage *page = [doc GetPage: 1];
            PTPDFRect * target_region = [page GetMediaBox];
            [replacer AddText: target_region replacement_text: @"hello hello hello hello hello hello hello hello hello hello"];
            [replacer Process: page];

            [doc SaveToFile: [output_path stringByAppendingPathComponent: @"ContentReplaced.pdf"] flags: 0];

            NSLog(@"Done. Result saved in ContentReplaced.pdf");
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        NSLog(@"Done.");
        [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 illustrates how to use the ContentReplacer class to make using
// 'template' pdf documents easier.
//-----------------------------------------------------------------------------------------

func runContentReplacerTest() -> Int {
    return autoreleasepool {
        let input_path: String = (Bundle.main.resourcePath)! + ("/")
        let output_path: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("").path
        
        var ret: Int = 0
        
        
        // The following example illustrates how to replace an image in a certain region,
        // and how to change template text.
        do {
            try PTPDFNet.catchException {
                let doc: PTPDFDoc = PTPDFDoc(filepath: input_path + ("BusinessCardTemplate.pdf"))
                doc.initSecurityHandler()
                
                // first, replace the image on the first page
                let replacer: PTContentReplacer = PTContentReplacer()
                let page: PTPage = doc.getPage(1)
                let img: PTImage = PTImage.create(doc.getSDFDoc(), filename: input_path + ("peppers.jpg"))
                replacer.addImage(page.getMediaBox(), replacement_image: img.getSDFObj())
                // next, replace the text place holders on the second page
                replacer.add("NAME", replacement_text: "John Smith")
                replacer.add("QUALIFICATIONS", replacement_text: "Philosophy Doctor")
                replacer.add("JOB_TITLE", replacement_text: "Software Developer")
                replacer.add("ADDRESS_LINE1", replacement_text: "#100 123 Software Rd")
                replacer.add("ADDRESS_LINE2", replacement_text: "Vancouver, BC")
                replacer.add("PHONE_OFFICE", replacement_text: "604-730-8989")
                replacer.add("PHONE_MOBILE", replacement_text: "604-765-4321")
                replacer.add("EMAIL", replacement_text: "info@pdftron.com")
                replacer.add("WEBSITE_URL", replacement_text: "http://www.pdftron.com")
                // finally, apply
                replacer.process(page)
                
                doc.save(toFile: URL(fileURLWithPath: output_path).appendingPathComponent("BusinessCard.pdf").path, flags: 0)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // The following example illustrates how to replace text in a given region
        do {
            try PTPDFNet.catchException {
                // Open the document that was saved in the previous code sample
                let doc: PTPDFDoc = PTPDFDoc(filepath: input_path + ("newsletter.pdf"))
                doc.initSecurityHandler()
                
                let replacer: PTContentReplacer = PTContentReplacer()
                let page: PTPage = doc.getPage(1)
                let target_region: PTPDFRect = page.getMediaBox()
                replacer.addText(target_region, replacement_text: "hello hello hello hello hello hello hello hello hello hello")
                replacer.process(page)
                
                doc.save(toFile: URL(fileURLWithPath: output_path).appendingPathComponent("ContentReplaced.pdf").path, flags: 0)
                
                print("Done.")
            }
        } 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/contentreplacertest.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.
