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 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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales