PDFDocMemory

Sample C# 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 Server SDK.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using pdftron;
8using pdftron.Common;
9using pdftron.Filters;
10using pdftron.SDF;
11using pdftron.PDF;
12
13// The following sample illustrates how to read/write a PDF document from/to
14// a memory buffer. This is useful for applications that work with dynamic PDF
15// documents that don't need to be saved/read from a disk.
16namespace PDFDocMemoryTestCS
17{
18 class Class1
19 {
20 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
21 static Class1() {}
22
23 [STAThread]
24 static void Main(string[] args)
25 {
26 PDFNet.Initialize(PDFTronLicense.Key);
27
28 // Relative path to the folder containing test files.
29 string input_path = "../../../../TestFiles/";
30 string output_path = "../../../../TestFiles/Output/";
31
32 try
33 {
34 // Read a PDF document from a stream or pass-in a memory buffer...
35 FileStream istm = new FileStream(input_path + "tiger.pdf", FileMode.Open, FileAccess.Read);
36 using (PDFDoc doc = new PDFDoc(istm))
37 using (ElementWriter writer = new ElementWriter())
38 using (ElementReader reader = new ElementReader())
39 {
40 doc.InitSecurityHandler();
41
42 int num_pages = doc.GetPageCount();
43
44 Element element;
45
46 // Perform some document editing ...
47 // Here we simply copy all elements from one page to another.
48 for(int i = 1; i <= num_pages; ++i)
49 {
50 Page pg = doc.GetPage(2 * i - 1);
51
52 reader.Begin(pg);
53 Page new_page = doc.PageCreate(pg.GetMediaBox());
54 doc.PageInsert(doc.GetPageIterator(2*i), new_page);
55
56 writer.Begin(new_page);
57 while ((element = reader.Next()) != null) // Read page contents
58 {
59 writer.WriteElement(element);
60 }
61
62 writer.End();
63 reader.End();
64 }
65
66 doc.Save(output_path + "doc_memory_edit.pdf", SDFDoc.SaveOptions.e_remove_unused);
67
68 // Save the document to a stream or a memory buffer...
69 using (FileStream ostm = new FileStream(output_path + "doc_memory_edit.txt", FileMode.Create, FileAccess.Write)) {
70 doc.Save(ostm, SDFDoc.SaveOptions.e_remove_unused);
71 }
72
73 // Read some data from the file stored in memory
74 reader.Begin(doc.GetPage(1));
75 while ((element = reader.Next()) != null) {
76 if (element.GetType() == Element.Type.e_path)
77 Console.Write("Path, ");
78 }
79 reader.End();
80
81 Console.WriteLine("");
82 Console.WriteLine("");
83 Console.WriteLine("Done. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...");
84 }
85 }
86 catch (PDFNetException e)
87 {
88 Console.WriteLine(e.Message);
89 }
90 PDFNet.Terminate();
91 }
92 }
93}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales