Read & Write PDF in Memory Buffer - C++ 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
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <Filters/MappedFile.h>
9#include <Filters/FilterReader.h>
10#include <Filters/FilterWriter.h>
11#include <PDF/ElementWriter.h>
12#include <PDF/ElementReader.h>
13
14#include <iostream>
15#include <fstream>
16#include "../../LicenseKey/CPP/LicenseKey.h"
17
18using namespace std;
19using namespace pdftron;
20using namespace SDF;
21using namespace PDF;
22using namespace Filters;
23
24int main(int argc, char *argv[])
25{
26 int ret = 0;
27 PDFNet::Initialize(LicenseKey);
28
29 // Relative path to the folder containing test files.
30 string input_path = "../../TestFiles/";
31 string output_path = "../../TestFiles/Output/";
32
33
34 // The following sample illustrates how to read/write a PDF document from/to
35 // a memory buffer. This is useful for applications that work with dynamic PDF
36 // documents that don't need to be saved/read from a disk.
37 try
38 {
39 // Read a PDF document in a memory buffer.
40 MappedFile file((input_path + "tiger.pdf"));
41 size_t file_sz = file.FileSize();
42
43 FilterReader file_reader(file);
44
45 unsigned char* mem = new unsigned char[file_sz];
46 file_reader.Read((unsigned char*)mem, file_sz);
47 PDFDoc doc(mem, file_sz);
48 delete[] mem;
49
50 doc.InitSecurityHandler();
51 int num_pages = doc.GetPageCount();
52
53 ElementWriter writer;
54 ElementReader reader;
55 Element element;
56
57 // Create a duplicate of every page but copy only path objects
58 for(int i=1; i<=num_pages; ++i)
59 {
60 PageIterator itr = doc.GetPageIterator(2*i-1);
61
62 reader.Begin(itr.Current());
63 Page new_page = doc.PageCreate(itr.Current().GetMediaBox());
64 PageIterator next_page = itr;
65 next_page.Next();
66 doc.PageInsert(next_page, new_page );
67
68 writer.Begin(new_page);
69 while ((element = reader.Next()) !=0) // Read page contents
70 {
71 //if (element.GetType() == Element::e_path)
72 writer.WriteElement(element);
73 }
74
75 writer.End();
76 reader.End();
77 }
78
79 doc.Save((output_path + "doc_memory_edit.pdf").c_str(), SDFDoc::e_remove_unused, NULL);
80 // doc.Save((output_path + "doc_memory_edit.pdf").c_str(), Doc::e_linearized, NULL);
81
82 // Save the document to a memory buffer.
83 const char* buf = 0;
84 size_t buf_sz;
85
86 doc.Save(buf, buf_sz, SDFDoc::e_remove_unused, NULL);
87 // doc.Save(buf, buf_sz, Doc::e_linearized, NULL);
88
89 // Write the contents of the buffer to the disk
90 {
91 ofstream out((output_path + "doc_memory_edit.txt").c_str(), ofstream::binary);
92 out.write(buf, buf_sz);
93 out.close();
94 }
95
96 // Read some data from the file stored in memory
97 reader.Begin(doc.GetPage(1));
98 while ((element = reader.Next()) !=0) {
99 if (element.GetType() == Element::e_path) cout << "Path, ";
100 }
101 reader.End();
102
103 cout << "\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ..." << endl;
104 }
105 catch(Common::Exception& e)
106 {
107 cout << e << endl;
108 ret = 1;
109 }
110 catch(...)
111 {
112 cout << "Unknown Exception" << endl;
113 ret = 1;
114 }
115
116 PDFNet::Terminate();
117 return ret;
118}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales