SDF

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales