SDF

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales