Sample Java code to use Apryse SDK for creating, extracting, and manipulating PDF packages (also known as PDF portfolios). Learn more about our Android SDK and PDF Data Extraction SDK Capabilities.
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.common.PDFNetException;
13import com.pdftron.filters.Filter;
14import com.pdftron.pdf.ColorPt;
15import com.pdftron.pdf.ColorSpace;
16import com.pdftron.pdf.Element;
17import com.pdftron.pdf.ElementBuilder;
18import com.pdftron.pdf.ElementWriter;
19import com.pdftron.pdf.FileSpec;
20import com.pdftron.pdf.Font;
21import com.pdftron.pdf.PDFDoc;
22import com.pdftron.pdf.Page;
23import com.pdftron.pdf.Rect;
24import com.pdftron.sdf.NameTree;
25import com.pdftron.sdf.NameTreeIterator;
26import com.pdftron.sdf.Obj;
27import com.pdftron.sdf.SDFDoc;
28
29import java.util.ArrayList;
30
31//-----------------------------------------------------------------------------------
32//This sample illustrates how to create, extract, and manipulate PDF Portfolios
33//(a.k.a. PDF Packages) using PDFNet SDK.
34//-----------------------------------------------------------------------------------
35
36public class PDFPackageTest extends PDFNetSample {
37
38 private static OutputListener mOutputListener;
39
40 private static ArrayList<String> mFileList = new ArrayList<>();
41
42 public PDFPackageTest() {
43 setTitle(R.string.sample_pdfpackage_title);
44 setDescription(R.string.sample_pdfpackage_description);
45 }
46
47 @Override
48 public void run(OutputListener outputListener) {
49 super.run(outputListener);
50 mOutputListener = outputListener;
51 mFileList.clear();
52 printHeader(outputListener);
53
54 // Create a PDF Package.
55 try (PDFDoc doc = new PDFDoc()) {
56
57 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "numbered.pdf").getAbsolutePath(), "My File 1");
58 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), "My Newsletter...");
59 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath(), "An image");
60 addCoverPage(doc);
61 doc.save(Utils.createExternalFile("package.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
62 mOutputListener.println("Done.");
63 } catch (Exception e) {
64 mOutputListener.printError(e.getStackTrace());
65 }
66
67 // Extract parts from a PDF Package.
68 try (PDFDoc doc = new PDFDoc(Utils.createExternalFile("package.pdf", mFileList).getAbsolutePath())) {
69 doc.initSecurityHandler();
70
71 com.pdftron.sdf.NameTree files = NameTree.find(doc.getSDFDoc(), "EmbeddedFiles");
72 if (files.isValid()) {
73 // Traverse the list of embedded files.
74 NameTreeIterator i = files.getIterator();
75 for (int counter = 0; i.hasNext(); i.next(), ++counter) {
76 String entry_name = i.key().getAsPDFText();
77 mOutputListener.println("Part: " + entry_name);
78
79 FileSpec file_spec = new FileSpec(i.value());
80 Filter stm = file_spec.getFileData();
81 if (stm != null) {
82 String ext = "pdf";
83 if (entry_name.lastIndexOf('.') > 0) {
84 ext = entry_name.substring(entry_name.lastIndexOf('.')+1);
85 }
86 String fname = "extract_" + counter + "." + ext;
87 stm.writeToFile(Utils.createExternalFile(fname, mFileList).getAbsolutePath(), false);
88 }
89 }
90 }
91 mOutputListener.println("Done.");
92 } catch (Exception e) {
93 mOutputListener.printError(e.getStackTrace());
94 }
95
96 for (String file : mFileList) {
97 addToFileList(file);
98 }
99 printFooter(outputListener);
100 }
101
102 static void addPackage(PDFDoc doc, String file, String desc) throws PDFNetException {
103 NameTree files = NameTree.create(doc.getSDFDoc(), "EmbeddedFiles");
104 FileSpec fs = FileSpec.create(doc, file, true);
105 files.put(file.getBytes(), fs.getSDFObj());
106 fs.getSDFObj().putText("Desc", desc);
107
108 Obj collection = doc.getRoot().findObj("Collection");
109 if (collection == null) collection = doc.getRoot().putDict("Collection");
110
111 // You could here manipulate any entry in the Collection dictionary.
112 // For example, the following line sets the tile mode for initial view mode
113 // Please refer to section '2.3.5 Collections' in PDF Reference for details.
114 collection.putName("View", "T");
115 }
116
117 static void addCoverPage(PDFDoc doc) throws PDFNetException {
118 // Here we dynamically generate cover page (please see ElementBuilder
119 // sample for more extensive coverage of PDF creation API).
120 Page page = doc.pageCreate(new Rect(0, 0, 200, 200));
121
122 ElementBuilder b = new ElementBuilder();
123 ElementWriter w = new ElementWriter();
124 w.begin(page);
125 Font font = Font.create(doc.getSDFDoc(), Font.e_helvetica);
126 w.writeElement(b.createTextBegin(font, 12));
127 Element e = b.createTextRun("My PDF Collection");
128 e.setTextMatrix(1, 0, 0, 1, 50, 96);
129 e.getGState().setFillColorSpace(ColorSpace.createDeviceRGB());
130 e.getGState().setFillColor(new ColorPt(1, 0, 0));
131 w.writeElement(e);
132 w.writeElement(b.createTextEnd());
133 w.end();
134 doc.pagePushBack(page);
135
136 // Alternatively we could import a PDF page from a template PDF document
137 // (for an example please see PDFPage sample project).
138 // ...
139 }
140
141}
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.common.PDFNetException
13import com.pdftron.pdf.*
14import com.pdftron.sdf.NameTree
15import com.pdftron.sdf.SDFDoc
16import java.util.*
17
18//-----------------------------------------------------------------------------------
19//This sample illustrates how to create, extract, and manipulate PDF Portfolios
20//(a.k.a. PDF Packages) using PDFNet SDK.
21//-----------------------------------------------------------------------------------
22
23class PDFPackageTest : PDFNetSample() {
24 init {
25 setTitle(R.string.sample_pdfpackage_title)
26 setDescription(R.string.sample_pdfpackage_description)
27 }
28
29 override fun run(outputListener: OutputListener?) {
30 super.run(outputListener)
31 mOutputListener = outputListener
32 mFileList.clear()
33 printHeader(outputListener!!)
34
35 // Create a PDF Package.
36
37 // Create a PDF Package.
38 try {
39 PDFDoc().use { doc ->
40 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "numbered.pdf")!!.absolutePath, "My File 1")
41 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf")!!.absolutePath, "My Newsletter...")
42 addPackage(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg")!!.absolutePath, "An image")
43 addCoverPage(doc)
44 doc.save(Utils.createExternalFile("package.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
45 mOutputListener!!.println("Done.")
46 }
47 } catch (e: Exception) {
48 mOutputListener!!.printError(e.stackTrace)
49 }
50
51 // Extract parts from a PDF Package.
52 try {
53 PDFDoc(Utils.createExternalFile("package.pdf", mFileList).absolutePath).use { doc ->
54 doc.initSecurityHandler()
55 val files = NameTree.find(doc.sdfDoc, "EmbeddedFiles")
56 if (files.isValid) {
57 // Traverse the list of embedded files.
58 val i = files.iterator
59 var counter = 0
60 while (i.hasNext()) {
61 val entry_name = i.key().asPDFText
62 mOutputListener!!.println("Part: " + entry_name);
63 val file_spec = FileSpec(i.value())
64 val stm = file_spec.fileData
65 if (stm != null) {
66 var ext = "pdf"
67 if (entry_name.lastIndexOf('.') > 0) {
68 ext = entry_name.substring(entry_name.lastIndexOf('.') + 1)
69 }
70 val fname = "extract_$counter.$ext"
71 stm.writeToFile(Utils.createExternalFile(fname, PDFPackageTest.mFileList).absolutePath, false)
72 }
73 i.next()
74 ++counter
75 }
76 }
77 mOutputListener!!.println("Done.")
78 }
79 } catch (e: Exception) {
80 mOutputListener!!.printError(e.stackTrace)
81 }
82
83 for (file in mFileList) {
84 addToFileList(file!!)
85 }
86 printFooter(outputListener!!)
87 }
88
89 companion object {
90
91 private var mOutputListener: OutputListener? = null
92
93 private val mFileList = ArrayList<String>()
94
95 @Throws(PDFNetException::class)
96 fun addPackage(doc: PDFDoc, file: String, desc: String?) {
97 val files = NameTree.create(doc.sdfDoc, "EmbeddedFiles")
98 val fs = FileSpec.create(doc, file, true)
99 files.put(file.toByteArray(), fs.sdfObj)
100 fs.sdfObj.putText("Desc", desc)
101 var collection = doc.root.findObj("Collection")
102 if (collection == null) collection = doc.root.putDict("Collection")
103
104 // You could here manipulate any entry in the Collection dictionary.
105 // For example, the following line sets the tile mode for initial view mode
106 // Please refer to section '2.3.5 Collections' in PDF Reference for details.
107 collection!!.putName("View", "T")
108 }
109
110 @Throws(PDFNetException::class)
111 fun addCoverPage(doc: PDFDoc) {
112 // Here we dynamically generate cover page (please see ElementBuilder
113 // sample for more extensive coverage of PDF creation API).
114 val page = doc.pageCreate(Rect(0.0, 0.0, 200.0, 200.0))
115 val b = ElementBuilder()
116 val w = ElementWriter()
117 w.begin(page)
118 val font = Font.create(doc.sdfDoc, Font.e_helvetica)
119 w.writeElement(b.createTextBegin(font, 12.0))
120 val e = b.createTextRun("My PDF Collection")
121 e.setTextMatrix(1.0, 0.0, 0.0, 1.0, 50.0, 96.0)
122 e.gState.fillColorSpace = ColorSpace.createDeviceRGB()
123 e.gState.fillColor = ColorPt(1.0, 0.0, 0.0)
124 w.writeElement(e)
125 w.writeElement(b.createTextEnd())
126 w.end()
127 doc.pagePushBack(page)
128
129 // Alternatively we could import a PDF page from a template PDF document
130 // (for an example please see PDFPage sample project).
131 // ...
132 }
133 }
134
135}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales