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 and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//-----------------------------------------------------------------------------------------
10// The sample code illustrates how to use the ContentReplacer class to make using
11// 'template' pdf documents easier.
12//-----------------------------------------------------------------------------------------
13
14int main(int argc, char *argv[])
15{
16 @autoreleasepool {
17
18 NSString* input_path = @"../../TestFiles/";
19 NSString* output_path = @"../../TestFiles/Output/";
20
21 int ret = 0;
22 [PTPDFNet Initialize: 0];
23
24 // The following example illustrates how to replace an image in a certain region,
25 // and how to change template text.
26 @try
27 {
28 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [input_path stringByAppendingPathComponent: @"BusinessCardTemplate.pdf"]];
29 [doc InitSecurityHandler];
30
31 // first, replace the image on the first page
32 PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
33 PTPage *page = [doc GetPage: 1];
34 PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: [input_path stringByAppendingPathComponent: @"peppers.jpg"]];
35 [replacer AddImage: [page GetMediaBox] replacement_image: [img GetSDFObj]];
36 // next, replace the text place holders on the second page
37 [replacer AddString: @"NAME" replacement_text: @"John Smith"];
38 [replacer AddString: @"QUALIFICATIONS" replacement_text: @"Philosophy Doctor"];
39 [replacer AddString: @"JOB_TITLE" replacement_text: @"Software Developer"];
40 [replacer AddString: @"ADDRESS_LINE1" replacement_text: @"#100 123 Software Rd"];
41 [replacer AddString: @"ADDRESS_LINE2" replacement_text: @"Vancouver, BC"];
42 [replacer AddString: @"PHONE_OFFICE" replacement_text: @"604-730-8989"];
43 [replacer AddString: @"PHONE_MOBILE" replacement_text: @"604-765-4321"];
44 [replacer AddString: @"EMAIL" replacement_text: @"info@pdftron.com"];
45 [replacer AddString: @"WEBSITE_URL" replacement_text: @"http://www.pdftron.com"];
46 // finally, apply
47 [replacer Process: page];
48
49 [doc SaveToFile: [output_path stringByAppendingPathComponent: @"BusinessCard.pdf"] flags: 0];
50 NSLog(@"Done. Result saved in BusinessCard.pdf");
51 }
52 @catch(NSException *e)
53 {
54 NSLog(@"%@", e.reason);
55 ret = 1;
56 }
57
58 // The following example illustrates how to replace text in a given region
59 @try
60 {
61 // Open the document that was saved in the previous code sample
62 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [input_path stringByAppendingPathComponent: @"newsletter.pdf"]];
63 [doc InitSecurityHandler];
64
65 PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
66 PTPage *page = [doc GetPage: 1];
67 PTPDFRect * target_region = [page GetMediaBox];
68 [replacer AddText: target_region replacement_text: @"hello hello hello hello hello hello hello hello hello hello"];
69 [replacer Process: page];
70
71 [doc SaveToFile: [output_path stringByAppendingPathComponent: @"ContentReplaced.pdf"] flags: 0];
72
73 NSLog(@"Done. Result saved in ContentReplaced.pdf");
74 }
75 @catch(NSException *e)
76 {
77 NSLog(@"%@", e.reason);
78 ret = 1;
79 }
80 NSLog(@"Done.");
81 [PTPDFNet Terminate: 0];
82 return ret;
83 }
84
85}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9//-----------------------------------------------------------------------------------------
10// The sample code illustrates how to use the ContentReplacer class to make using
11// 'template' pdf documents easier.
12//-----------------------------------------------------------------------------------------
13
14func runContentReplacerTest() -> Int {
15 return autoreleasepool {
16 let input_path: String = (Bundle.main.resourcePath)! + ("/")
17 let output_path: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("").path
18
19 var ret: Int = 0
20
21
22 // The following example illustrates how to replace an image in a certain region,
23 // and how to change template text.
24 do {
25 try PTPDFNet.catchException {
26 let doc: PTPDFDoc = PTPDFDoc(filepath: input_path + ("BusinessCardTemplate.pdf"))
27 doc.initSecurityHandler()
28
29 // first, replace the image on the first page
30 let replacer: PTContentReplacer = PTContentReplacer()
31 let page: PTPage = doc.getPage(1)
32 let img: PTImage = PTImage.create(doc.getSDFDoc(), filename: input_path + ("peppers.jpg"))
33 replacer.addImage(page.getMediaBox(), replacement_image: img.getSDFObj())
34 // next, replace the text place holders on the second page
35 replacer.add("NAME", replacement_text: "John Smith")
36 replacer.add("QUALIFICATIONS", replacement_text: "Philosophy Doctor")
37 replacer.add("JOB_TITLE", replacement_text: "Software Developer")
38 replacer.add("ADDRESS_LINE1", replacement_text: "#100 123 Software Rd")
39 replacer.add("ADDRESS_LINE2", replacement_text: "Vancouver, BC")
40 replacer.add("PHONE_OFFICE", replacement_text: "604-730-8989")
41 replacer.add("PHONE_MOBILE", replacement_text: "604-765-4321")
42 replacer.add("EMAIL", replacement_text: "info@pdftron.com")
43 replacer.add("WEBSITE_URL", replacement_text: "http://www.pdftron.com")
44 // finally, apply
45 replacer.process(page)
46
47 doc.save(toFile: URL(fileURLWithPath: output_path).appendingPathComponent("BusinessCard.pdf").path, flags: 0)
48 }
49 } catch let e as NSError {
50 print("\(e)")
51 ret = 1
52 }
53
54 // The following example illustrates how to replace text in a given region
55 do {
56 try PTPDFNet.catchException {
57 // Open the document that was saved in the previous code sample
58 let doc: PTPDFDoc = PTPDFDoc(filepath: input_path + ("newsletter.pdf"))
59 doc.initSecurityHandler()
60
61 let replacer: PTContentReplacer = PTContentReplacer()
62 let page: PTPage = doc.getPage(1)
63 let target_region: PTPDFRect = page.getMediaBox()
64 replacer.addText(target_region, replacement_text: "hello hello hello hello hello hello hello hello hello hello")
65 replacer.process(page)
66
67 doc.save(toFile: URL(fileURLWithPath: output_path).appendingPathComponent("ContentReplaced.pdf").path, flags: 0)
68
69 print("Done.")
70 }
71 } catch let e as NSError {
72 print("\(e)")
73 ret = 1
74 }
75
76 return ret
77 }
78}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales