Sample Obj-C code for editing an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API. 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// This sample illustrates how to use basic SDF API (also known as Cos) to edit an
10// existing document.
11
12int main(int argc, char *argv[])
13{
14 @autoreleasepool {
15 int ret = 0;
16 [PTPDFNet Initialize: 0];
17
18 @try
19 {
20 NSLog(@"Opening the test file...");
21
22 // Here we create a SDF/Cos document directly from PDF file. In case you have
23 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
24 PTSDFDoc *doc = [[PTSDFDoc alloc] initWithFilepath: @"../../TestFiles/fish.pdf"];
25 [doc InitSecurityHandler];
26
27 NSLog(@"Modifying info dictionary, adding custom properties, embedding a stream...");
28 PTObj * trailer = [doc GetTrailer]; // Get the trailer
29
30 // Now we will change PDF document information properties using SDF API
31
32 // Get the Info dictionary.
33 PTDictIterator *itr = [trailer Find: @"Info"];
34 PTObj * info = [[PTObj alloc] init];
35 if ([itr HasNext])
36 {
37 info = [itr Value];
38 // Modify 'Producer' entry.
39 [info PutString: @"Producer" value: @"PDFTron PDFNet"];
40
41 // Read title entry (if it is present)
42 itr = [info Find: @"Author"];
43 if ([itr HasNext])
44 {
45 NSString *oldstr = [[itr Value] GetAsPDFText];
46 [info PutText: @"Author" value: [oldstr stringByAppendingString: @"- Modified"]];
47 }
48 else
49 {
50 [info PutString: @"Author" value: @"Me, myself, and I"];
51 }
52 }
53 else
54 {
55 // Info dict is missing.
56 info = [trailer PutDict: @"Info"];
57 [info PutString: @"Producer" value: @"PDFTron PDFNet"];
58 [info PutString: @"Title" value: @"My document"];
59 }
60
61 // Create a custom inline dictionary within Info dictionary
62 PTObj * custom_dict = [info PutDict: @"My Direct Dict"];
63 [custom_dict PutNumber: @"My Number" value: 100]; // Add some key/value pairs
64 [custom_dict PutArray: @"My Array"];
65
66 // Create a custom indirect array within Info dictionary
67 PTObj * custom_array = [doc CreateIndirectArray];
68 [info Put: @"My Indirect Array" obj: custom_array]; // Add some entries
69
70 // Create indirect link to root
71 [custom_array PushBack: [[trailer Get: @"Root"] Value]];
72
73 // Embed a custom stream (file mystream.txt).
74 PTMappedFile *embed_file = [[PTMappedFile alloc] initWithFilename: @"../../TestFiles/my_stream.txt"];
75 PTFilterReader *mystm = [[PTFilterReader alloc] initWithFilter: embed_file];
76 [custom_array PushBack: [doc CreateIndirectStream: mystm]];
77
78 // Save the changes.
79 NSLog(@"Saving modified test file...");
80 [doc SaveSDFDocToFile: @"../../TestFiles/Output/sdftest_out.pdf" flags:0 header: @"%PDF-1.4"];
81
82 NSLog(@"Test completed.");
83 }
84 @catch (NSException *e)
85 {
86 NSLog(@"%@", e.reason);
87 ret = 1;
88 }
89 [PTPDFNet Terminate: 0];
90 return ret;
91 }
92}
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// This sample illustrates how to use basic SDF API (also known as Cos) to edit an
10// existing document.
11
12func runSDFTest() -> Int {
13 return autoreleasepool {
14 var ret: Int = 0
15
16
17 do {
18 try PTPDFNet.catchException {
19 print("Opening the test file...")
20
21 // Here we create a SDF/Cos document directly from PDF file. In case you have
22 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
23 let doc: PTSDFDoc = PTSDFDoc(filepath: Bundle.main.path(forResource: "fish", ofType: "pdf"))
24 doc.initSecurityHandler()
25
26 print("Modifying info dictionary, adding custom properties, embedding a stream...")
27 let trailer: PTObj = doc.getTrailer() // Get the trailer
28
29 // Now we will change PDF document information properties using SDF API
30
31 // Get the Info dictionary.
32 var itr: PTDictIterator = trailer.find("Info")
33 var info: PTObj = PTObj()
34 if itr.hasNext() {
35 info = itr.value()
36 // Modify 'Producer' entry.
37 info.put("Producer", value: "PDFTron PDFNet")
38
39 // Read title entry (if it is present)
40 itr = info.find("Author")
41 if itr.hasNext() {
42 let oldstr: String = itr.value().getAsPDFText()
43 info.putText("Author", value: oldstr + "- Modified")
44 }
45 else {
46 info.put("Author", value: "Me, myself, and I")
47 }
48 }
49 else {
50 // Info dict is missing.
51 info = (trailer.putDict("Info"))!
52 info.put("Producer", value: "PDFTron PDFNet")
53 info.put("Title", value: "My document")
54 }
55
56 // Create a custom inline dictionary within Info dictionary
57 let custom_dict: PTObj = info.putDict("My Direct Dict")
58 custom_dict.putNumber("My Number", value: 100) // Add some key/value pairs
59 custom_dict.putArray("My Array")
60
61 // Create a custom indirect array within Info dictionary
62 let custom_array: PTObj = doc.createIndirectArray()
63 info.put("My Indirect Array", obj: custom_array) // Add some entries
64
65 // Create indirect link to root
66 custom_array.pushBack(trailer.get("Root").value())
67
68 // Embed a custom stream (file mystream.txt).
69 let embed_file = PTMappedFile(filename: Bundle.main.path(forResource: "my_stream", ofType: "txt"))
70 let mystm = PTFilterReader(filter: embed_file)
71 custom_array.pushBack(doc.createIndirectStream(mystm))
72
73 // Save the changes.
74 print("Saving modified test file...")
75 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("sdftest_out.pdf").path, flags: 0, header: "%PDF-1.4")
76
77 print("Test completed.")
78 }
79 } catch let e as NSError {
80 print("\(e)")
81 ret = 1
82 }
83 return ret
84 }
85}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales