PDFPackage

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales