Read & Write PDF in Memory Buffer - Java Sample Code

Sample code for using Apryse SDK to read/write a PDF document from/to memory buffer; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. 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 Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11
12import com.pdftron.common.PDFNetException;
13import com.pdftron.filters.FilterReader;
14import com.pdftron.filters.FilterWriter;
15import com.pdftron.filters.MappedFile;
16import com.pdftron.pdf.*;
17import com.pdftron.sdf.SDFDoc;
18
19public class PDFDocMemoryTest {
20
21 public static void main(String[] args) {
22
23 PDFNet.initialize(PDFTronLicense.Key());
24
25 // Relative path to the folder containing test files.
26 String input_path = "../../TestFiles/";
27 String output_path = "../../TestFiles/Output/";
28
29 // The following sample illustrates how to read/write a PDF document from/to
30 // a memory buffer. This is useful for applications that work with dynamic PDF
31 // documents that don't need to be saved/read from a disk.
32 try {
33 // Read a PDF document in a memory buffer.
34 MappedFile file = new MappedFile((input_path + "tiger.pdf"));
35 long file_sz = file.fileSize();
36
37 FilterReader file_reader = new FilterReader(file);
38
39 byte[] mem = new byte[(int) file_sz];
40
41 long bytes_read = file_reader.read(mem);
42 try (PDFDoc doc = new PDFDoc(mem)) {
43
44 doc.initSecurityHandler();
45 int num_pages = doc.getPageCount();
46
47 ElementWriter writer = new ElementWriter();
48 ElementReader reader = new ElementReader();
49 Element element;
50
51 // Create a duplicate of every page but copy only path objects
52
53 for (int i = 1; i <= num_pages; ++i) {
54 PageIterator itr = doc.getPageIterator(2 * i - 1);
55 Page current = itr.next();
56 reader.begin(current);
57 Page new_page = doc.pageCreate(current.getMediaBox());
58 doc.pageInsert(itr, new_page);
59
60 writer.begin(new_page);
61 while ((element = reader.next()) != null) // Read page contents
62 {
63 //if (element.getType() == Element.e_path)
64 writer.writeElement(element);
65 }
66
67 writer.end();
68 reader.end();
69 }
70
71 doc.save(output_path + "doc_memory_edit.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
72
73 // Save the document to a memory buffer.
74
75
76 byte[] buf = doc.save(SDFDoc.SaveMode.REMOVE_UNUSED, null);
77 // doc.Save(buf, buf_sz, Doc::e_linearized, NULL);
78
79 // Write the contents of the buffer to the disk
80 {
81 File outfile = new File(output_path + "doc_memory_edit.txt");
82 // output "doc_memory_edit.txt"
83 FileOutputStream fop = new FileOutputStream(outfile);
84 if (!outfile.exists()) {
85 outfile.createNewFile();
86 }
87 fop.write(buf);
88 fop.flush();
89 fop.close();
90 }
91
92 // Read some data from the file stored in memory
93 reader.begin(doc.getPage(1));
94 while ((element = reader.next()) != null) {
95 if (element.getType() == Element.e_path) System.out.print("Path, ");
96 }
97 reader.end();
98
99 System.out.println("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...");
100 }
101 }
102 catch (PDFNetException e)
103 {
104 e.printStackTrace();
105 System.out.println(e);
106 }
107 catch (Exception e)
108 {
109 e.printStackTrace();
110 }
111 PDFNet.terminate();
112 }
113}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales