Sample Java 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 Android SDK and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.filters.FilterReader;
13import com.pdftron.filters.MappedFile;
14import com.pdftron.sdf.DictIterator;
15import com.pdftron.sdf.Obj;
16import com.pdftron.sdf.SDFDoc;
17
18import java.util.ArrayList;
19
20public class SDFTest extends PDFNetSample {
21
22 private static OutputListener mOutputListener;
23
24 private static ArrayList<String> mFileList = new ArrayList<>();
25
26 public SDFTest() {
27 setTitle(R.string.sample_sdf_title);
28 setDescription(R.string.sample_sdf_description);
29 }
30
31 @Override
32 public void run(OutputListener outputListener) {
33 super.run(outputListener);
34 mOutputListener = outputListener;
35 mFileList.clear();
36 printHeader(outputListener);
37
38 try {
39 mOutputListener.println("Opening the test file...");
40
41 // Here we create a SDF/Cos document directly from PDF file. In case you have
42 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
43 SDFDoc doc = new SDFDoc((Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath()));
44 doc.initSecurityHandler();
45
46 mOutputListener.println("Modifying info dictionary, adding custom properties, embedding a stream...");
47 Obj trailer = doc.getTrailer(); // Get the trailer
48
49 // Now we will change PDF document information properties using SDF API
50
51 // Get the Info dictionary.
52 DictIterator itr = trailer.find("Info");
53 Obj info;
54 if (itr.hasNext()) {
55 info = itr.value();
56 // Modify 'Producer' entry.
57 info.putString("Producer", "PDFTron PDFNet");
58
59 // Read title entry (if it is present)
60 itr = info.find("Author");
61 if (itr.hasNext()) {
62 String oldstr = itr.value().getAsPDFText();
63
64 info.putText("Author", oldstr + "- Modified");
65 } else {
66 info.putString("Author", "Me, myself, and I");
67 }
68 } else {
69 // Info dict is missing.
70 info = trailer.putDict("Info");
71 info.putString("Producer", "PDFTron PDFNet");
72 info.putString("Title", "My document");
73 }
74
75 // Create a custom inline dictionary within Info dictionary
76 Obj custom_dict = info.putDict("My Direct Dict");
77 custom_dict.putNumber("My Number", 100); // Add some key/value pairs
78 custom_dict.putArray("My Array");
79
80 // Create a custom indirect array within Info dictionary
81 Obj custom_array = doc.createIndirectArray();
82 info.put("My Indirect Array", custom_array); // Add some entries
83
84 // Create indirect link to root
85 custom_array.pushBack(trailer.get("Root").value());
86
87 // Embed a custom stream (file mystream.txt).
88 MappedFile embed_file = new MappedFile(Utils.getAssetTempFile(INPUT_PATH + "my_stream.txt").getAbsolutePath());
89 FilterReader mystm = new FilterReader(embed_file);
90 custom_array.pushBack(doc.createIndirectStream(mystm));
91
92 // Save the changes.
93 mOutputListener.println("Saving modified test file...");
94 doc.save(Utils.createExternalFile("sdftest_out.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null, "%PDF-1.4");
95 doc.close();
96
97 mOutputListener.println("Test completed.");
98 } catch (Exception e) {
99 mOutputListener.printError(e.getStackTrace());
100 }
101
102 for (String file : mFileList) {
103 addToFileList(file);
104 }
105 printFooter(outputListener);
106 }
107
108}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample
10import com.pdftron.android.pdfnetsdksamples.R
11import com.pdftron.android.pdfnetsdksamples.util.Utils
12import com.pdftron.filters.FilterReader
13import com.pdftron.filters.MappedFile
14import com.pdftron.sdf.Obj
15import com.pdftron.sdf.SDFDoc
16import java.util.*
17
18class SDFTest : PDFNetSample() {
19 init {
20 setTitle(R.string.sample_sdf_title)
21 setDescription(R.string.sample_sdf_description)
22 }
23
24 override fun run(outputListener: OutputListener?) {
25 super.run(outputListener)
26 mOutputListener = outputListener
27 mFileList.clear()
28 printHeader(outputListener!!)
29
30 try {
31 mOutputListener!!.println("Opening the test file...")
32
33 // Here we create a SDF/Cos document directly from PDF file. In case you have
34 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
35 val doc = SDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "fish.pdf")!!.absolutePath)
36 doc.initSecurityHandler()
37
38 mOutputListener!!.println("Modifying info dictionary, adding custom properties, embedding a stream...")
39 val trailer = doc.trailer // Get the trailer
40
41 // Now we will change PDF document information properties using SDF API
42
43 // Get the Info dictionary.
44 var itr = trailer.find("Info")
45 val info: Obj
46 if (itr.hasNext()) {
47 info = itr.value()
48 // Modify 'Producer' entry.
49 info.putString("Producer", "PDFTron PDFNet")
50
51 // Read title entry (if it is present)
52 itr = info.find("Author")
53 if (itr.hasNext()) {
54 val oldstr = itr.value().asPDFText
55
56 info.putText("Author", "$oldstr- Modified")
57 } else {
58 info.putString("Author", "Me, myself, and I")
59 }
60 } else {
61 // Info dict is missing.
62 info = trailer.putDict("Info")
63 info.putString("Producer", "PDFTron PDFNet")
64 info.putString("Title", "My document")
65 }
66
67 // Create a custom inline dictionary within Info dictionary
68 val custom_dict = info.putDict("My Direct Dict")
69 custom_dict.putNumber("My Number", 100.0) // Add some key/value pairs
70 custom_dict.putArray("My Array")
71
72 // Create a custom indirect array within Info dictionary
73 val custom_array = doc.createIndirectArray()
74 info.put("My Indirect Array", custom_array) // Add some entries
75
76 // Create indirect link to root
77 custom_array.pushBack(trailer.get("Root").value())
78
79 // Embed a custom stream (file mystream.txt).
80 val embed_file = MappedFile(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "my_stream.txt")!!.absolutePath)
81 val mystm = FilterReader(embed_file)
82 custom_array.pushBack(doc.createIndirectStream(mystm))
83
84 // Save the changes.
85 mOutputListener!!.println("Saving modified test file...")
86 doc.save(Utils.createExternalFile("sdftest_out.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null, "%PDF-1.4")
87 doc.close()
88
89 mOutputListener!!.println("Test completed.")
90 } catch (e: Exception) {
91 mOutputListener!!.printError(e.stackTrace)
92 }
93
94 for (file in mFileList) {
95 addToFileList(file)
96 }
97 printFooter(outputListener)
98 }
99
100 companion object {
101
102 private var mOutputListener: OutputListener? = null
103
104 private val mFileList = ArrayList<String>()
105 }
106
107}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales