Sample 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 UWP SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.Filters;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class SDFTest : Sample
18 {
19 public SDFTest() :
20 base("SDF", "The sample illustrates how to use basic Cos/SDF API to edit an existing document.")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(async () => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting SDF Test...");
29 WriteLine("--------------------------------\n");
30
31 string input_file_path = Path.Combine(InputPath, "fish.pdf");
32 WriteLine("Opening input file " + input_file_path);
33
34 try
35 {
36 // Here we create a SDF/Cos document directly from PDF file. In case you have
37 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
38 SDFDoc doc = new SDFDoc(input_file_path);
39 doc.InitSecurityHandler();
40
41 WriteLine("-------------------------------------------------");
42 WriteLine("Modifying info dictionary, adding custom properties, embedding a stream...");
43
44 Obj trailer = doc.GetTrailer(); // Get the trailer
45
46 // Now we will change PDF document information properties using SDF API
47
48 // Get the Info dictionary.
49 DictIterator itr = trailer.Find("Info");
50 Obj info;
51 if (itr.HasNext())
52 {
53 info = itr.Value();
54 // Modify 'Producer' entry.
55 info.PutString("Producer", "PDFTron PDFNet");
56
57 // Read title entry (if it is present)
58 itr = info.Find("Author");
59 if (itr.HasNext())
60 {
61 info.PutString("Author", itr.Value().GetAsPDFText() + "- Modified");
62 }
63 else
64 {
65 info.PutString("Author", "Joe Doe");
66 }
67 }
68 else
69 {
70 // Info dict is missing.
71 info = trailer.PutDict("Info");
72 info.PutString("Producer", "PDFTron PDFNet");
73 info.PutString("Title", "My document");
74 }
75
76 // Create a custom inline dictionary within Info dictionary
77 Obj custom_dict = info.PutDict("My Direct Dict");
78
79 // Add some key/value pairs
80 custom_dict.PutNumber("My Number", 100);
81
82 Obj my_array = custom_dict.PutArray("My Array");
83
84 // Create a custom indirect array within Info dictionary
85 Obj custom_array = doc.CreateIndirectArray();
86 info.Put("My Indirect Array", custom_array);
87
88 // Create indirect link to root
89 custom_array.PushBack(trailer.Get("Root").Value());
90
91 // Embed a custom stream (file my_stream.txt).
92 MappedFile embed_file = new MappedFile(Path.Combine(InputPath, "my_stream.txt"));
93 FilterReader mystm = new FilterReader(embed_file);
94 custom_array.PushBack(doc.CreateIndirectStream(mystm));
95
96 // Save the changes.
97 String output_file_path = Path.Combine(OutputPath, "sdftest_out.pdf");
98 await doc.SaveAsync(output_file_path, 0, "%PDF-1.4");
99 doc.Destroy();
100 WriteLine("Test completed.");
101 WriteLine("Done. Results saved in " + output_file_path);
102 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
103 }
104 catch (Exception e) {
105 WriteLine(GetExceptionMessage(e));
106 }
107
108 WriteLine("\n--------------------------------");
109 WriteLine("Done SDF Test.");
110 WriteLine("--------------------------------\n");
111 })).AsAsyncAction();
112 }
113 }
114}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales