PDFDocMemory

Sample Java code for using Apryse SDK to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk. Learn more about our Android SDK.

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.FilterReader;
14import com.pdftron.filters.MappedFile;
15import com.pdftron.pdf.Element;
16import com.pdftron.pdf.ElementReader;
17import com.pdftron.pdf.ElementWriter;
18import com.pdftron.pdf.PDFDoc;
19import com.pdftron.pdf.Page;
20import com.pdftron.pdf.PageIterator;
21import com.pdftron.sdf.SDFDoc;
22
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileNotFoundException;
26import java.io.FileOutputStream;
27import java.io.IOException;
28import java.util.ArrayList;
29
30public class PDFDocMemoryTest extends PDFNetSample {
31
32 private static OutputListener mOutputListener;
33
34 private static ArrayList<String> mFileList = new ArrayList<>();
35
36 public PDFDocMemoryTest() {
37 setTitle(R.string.sample_pdfdocmemory_title);
38 setDescription(R.string.sample_pdfdocmemory_description);
39 }
40
41 @Override
42 public void run(OutputListener outputListener) {
43 super.run(outputListener);
44 mOutputListener = outputListener;
45 mFileList.clear();
46 printHeader(outputListener);
47
48
49 // The following sample illustrates how to read/write a PDF document from/to
50 // a memory buffer. This is useful for applications that work with dynamic PDF
51 // documents that don't need to be saved/read from a disk.
52 try {
53 // Read a PDF document in a memory buffer.
54 MappedFile file = new MappedFile((Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath()));
55 long file_sz = file.fileSize();
56
57 FilterReader file_reader = new FilterReader(file);
58
59 byte[] mem = new byte[(int) file_sz];
60
61 long bytes_read = file_reader.read(mem);
62 try (PDFDoc doc = new PDFDoc(mem)) {
63
64 doc.initSecurityHandler();
65 int num_pages = doc.getPageCount();
66
67 ElementWriter writer = new ElementWriter();
68 ElementReader reader = new ElementReader();
69 Element element;
70
71 // Create a duplicate of every page but copy only path objects
72
73 for (int i = 1; i <= num_pages; ++i) {
74 PageIterator itr = doc.getPageIterator(2 * i - 1);
75 Page current = itr.next();
76 reader.begin(current);
77 Page new_page = doc.pageCreate(current.getMediaBox());
78 doc.pageInsert(itr, new_page);
79
80 writer.begin(new_page);
81 while ((element = reader.next()) != null) // Read page contents
82 {
83 //if (element.getType() == Element.e_path)
84 writer.writeElement(element);
85 }
86
87 writer.end();
88 reader.end();
89 }
90
91 doc.save(Utils.createExternalFile("doc_memory_edit.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
92
93 // Save the document to a memory buffer.
94
95 byte[] buf = doc.save(SDFDoc.SaveMode.REMOVE_UNUSED, null);
96 // doc.Save(buf, buf_sz, Doc::e_linearized, NULL);
97
98 // Write the contents of the buffer to the disk
99 {
100 File outfile = new File(Utils.createExternalFile("doc_memory_edit.txt", mFileList).getAbsolutePath());
101 // output "doc_memory_edit.txt"
102 FileOutputStream fop = new FileOutputStream(outfile);
103 if (!outfile.exists()) {
104 outfile.createNewFile();
105 }
106 fop.write(buf);
107 fop.flush();
108 fop.close();
109 }
110
111 // Read some data from the file stored in memory
112 reader.begin(doc.getPage(1));
113 while ((element = reader.next()) != null) {
114 if (element.getType() == Element.e_path) mOutputListener.print("Path, ");
115 }
116 reader.end();
117
118 mOutputListener.println("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...");
119 }
120 }
121 catch (PDFNetException e)
122 {
123 mOutputListener.printError(e.getStackTrace());
124 mOutputListener.printError(e.getStackTrace());
125 }
126 catch (Exception e)
127 {
128 mOutputListener.printError(e.getStackTrace());
129 }
130
131 for (String file : mFileList) {
132 addToFileList(file);
133 }
134 printFooter(outputListener);
135 }
136
137}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales