SDF

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 Server SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12namespace SDFTestCS
13{
14 /// <summary>
15 /// This sample illustrates how to use basic SDF API (also known as Cos) to edit an
16 /// existing document.
17 /// </summary>
18 class Class1
19 {
20 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
21 static Class1() {}
22
23 static void Main(string[] args)
24 {
25 PDFNet.Initialize(PDFTronLicense.Key);
26
27 // Relative path to the folder containing test files.
28 string input_path = "../../../../TestFiles/";
29 string output_path = "../../../../TestFiles/Output/";
30
31
32 //------------------------------------------------------------------
33 Console.WriteLine("Opening the test file...");
34
35 try
36 {
37 // Here we create a SDF/Cos document directly from PDF file. In case you have
38 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
39 using (SDFDoc doc = new SDFDoc(input_path + "fish.pdf"))
40 {
41 doc.InitSecurityHandler();
42
43 Console.WriteLine("Modifying info dictionary, adding custom properties, embedding a stream...");
44
45 Obj trailer = doc.GetTrailer(); // Get the trailer
46
47 // Now we will change PDF document information properties using SDF API
48
49 // Get the Info dictionary.
50 DictIterator itr = trailer.Find("Info");
51 Obj info;
52 if (itr.HasNext())
53 {
54 info = itr.Value();
55 // Modify 'Producer' entry.
56 info.PutString("Producer", "PDFTron PDFNet");
57
58 // Read title entry (if it is present)
59 itr = info.Find("Author");
60 if (itr.HasNext())
61 {
62 info.PutString("Author", itr.Value().GetAsPDFText() + "- Modified");
63 }
64 else
65 {
66 info.PutString("Author", "Joe Doe");
67 }
68 }
69 else
70 {
71 // Info dict is missing.
72 info = trailer.PutDict("Info");
73 info.PutString("Producer", "PDFTron PDFNet");
74 info.PutString("Title", "My document");
75 }
76
77 // Create a custom inline dictionary within Info dictionary
78 Obj custom_dict = info.PutDict("My Direct Dict");
79
80 // Add some key/value pairs
81 custom_dict.PutNumber("My Number", 100);
82
83 Obj my_array = custom_dict.PutArray("My Array");
84
85 // Create a custom indirect array within Info dictionary
86 Obj custom_array = doc.CreateIndirectArray();
87 info.Put("My Indirect Array", custom_array);
88
89 // Create indirect link to root
90 custom_array.PushBack(trailer.Get("Root").Value());
91
92 // Embed a custom stream (file my_stream.txt).
93 MappedFile embed_file = new MappedFile(input_path + "my_stream.txt");
94 FilterReader mystm = new FilterReader(embed_file);
95 custom_array.PushBack(doc.CreateIndirectStream(mystm));
96
97 // Save the changes.
98 Console.WriteLine("Saving modified test file...");
99 doc.Save(output_path + "sdftest_out.pdf", 0, "%PDF-1.4");
100 }
101
102 Console.WriteLine("Test completed.");
103 }
104 catch (PDFNetException e)
105 {
106 Console.WriteLine(e.Message);
107 }
108 PDFNet.Terminate();
109 }
110 }
111}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales