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

1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12using NUnit.Framework;
13
14namespace MiscellaneousSamples
15{
16 /// <summary>
17 /// This sample illustrates how to use basic SDF API (also known as Cos) to edit an
18 /// existing document.
19 /// </summary>
20 [TestFixture]
21 public class SDFTest
22 {
23
24 [Test]
25 public static void Sample()
26 {
27
28 // Relative path to the folder containing test files.
29 const string input_path = "TestFiles/";
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(Utils.GetAssetTempFile(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(Utils.GetAssetTempFile(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(Utils.CreateExternalFile("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 Assert.True(false);
108 }
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