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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5package com.pdftron.android.pdfnetsdksamples.samples
6
7import com.pdftron.android.pdfnetsdksamples.OutputListener
8import com.pdftron.android.pdfnetsdksamples.PDFNetSample
9import com.pdftron.android.pdfnetsdksamples.R
10import com.pdftron.android.pdfnetsdksamples.util.Utils
11import com.pdftron.common.PDFNetException
12import com.pdftron.filters.MappedFile
13import com.pdftron.pdf.ElementWriter
14import com.pdftron.pdf.PDFDoc
15import com.pdftron.pdf.PageIterator
16import com.pdftron.sdf.SDFDoc
17
18class PDFDocMemoryTest : PDFNetSample() {
19 override fun run(outputListener: OutputListener?) {
20 super.run(outputListener)
21 mOutputListener = outputListener
22 mFileList.clear()
23 printHeader(outputListener!!)
24
25 // The following sample illustrates how to read/write a PDF document from/to
26 // a memory buffer. This is useful for applications that work with dynamic PDF
27 // documents that don't need to be saved/read from a disk.
28 try {
29 // Read a PDF document in a memory buffer.
30 val file = MappedFile(Utils.getAssetTempFile(INPUT_PATH.toString() + "tiger.pdf")!!.getAbsolutePath())
31 val file_sz: Long = file.fileSize()
32 val file_reader: com.pdftron.filters.FilterReader = com.pdftron.filters.FilterReader(file)
33 val mem = ByteArray(file_sz.toInt())
34 val bytes_read: Long = file_reader.read(mem)
35 PDFDoc(mem).use { doc ->
36 doc.initSecurityHandler()
37 val num_pages: Int = doc.getPageCount()
38 val writer = ElementWriter()
39 val reader: com.pdftron.pdf.ElementReader = com.pdftron.pdf.ElementReader()
40 var element: com.pdftron.pdf.Element?
41
42 // Create a duplicate of every page but copy only path objects
43 for (i in 1..num_pages) {
44 val itr: PageIterator = doc.getPageIterator(2 * i - 1)
45 val current: com.pdftron.pdf.Page? = itr.next()
46 reader.begin(current)
47 val new_page: com.pdftron.pdf.Page = doc.pageCreate(current!!.getMediaBox())
48 doc.pageInsert(itr, new_page)
49 writer.begin(new_page)
50 // Read page contents
51 while (true) {
52 element = reader.next()
53 if (element == null) {
54 break
55 }
56 //if (element.getType() == Element.e_path)
57 writer.writeElement(element)
58 }
59 writer.end()
60 reader.end()
61 }
62 doc.save(Utils.createExternalFile("doc_memory_edit.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null)
63
64 // Save the document to a memory buffer.
65 val buf: ByteArray = doc.save(SDFDoc.SaveMode.REMOVE_UNUSED, null)
66 // doc.Save(buf, buf_sz, Doc::e_linearized, NULL);
67
68 // Write the contents of the buffer to the disk
69 run({
70 val outfile: java.io.File = java.io.File(Utils.createExternalFile("doc_memory_edit.txt", mFileList).getAbsolutePath())
71 val fop: java.io.FileOutputStream = java.io.FileOutputStream(outfile)
72 if (!outfile.exists()) {
73 outfile.createNewFile()
74 }
75 fop.write(buf)
76 fop.flush()
77 fop.close()
78 })
79
80 // Read some data from the file stored in memory
81 reader.begin(doc.getPage(1))
82 while (true) {
83 element = reader.next()
84 if (element == null) {
85 break
86 }
87 if (element.getType() == com.pdftron.pdf.Element.e_path) mOutputListener!!.print("Path, ")
88 }
89 reader.end()
90 mOutputListener!!.println("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...")
91 }
92 } catch (e: PDFNetException) {
93 mOutputListener!!.printError(e.getStackTrace())
94 mOutputListener!!.printError(e.getStackTrace())
95 } catch (e: java.lang.Exception) {
96 mOutputListener!!.printError(e.getStackTrace())
97 }
98 for (file in mFileList) {
99 addToFileList(file)
100 }
101 printFooter(outputListener)
102 }
103
104 companion object {
105 private var mOutputListener: OutputListener? = null
106 private val mFileList: java.util.ArrayList<String> = java.util.ArrayList<String>()
107 }
108
109 init {
110 setTitle(R.string.sample_pdfdocmemory_title)
111 setDescription(R.string.sample_pdfdocmemory_description)
112 }
113}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales