Read & Write PDF in Memory Buffer - Go 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-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "os"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15func main(){
16 PDFNetInitialize(PDFTronLicense.Key)
17
18 // Relative path to the folder containing the test files.
19 inputPath := "../../TestFiles/"
20 outputPath := "../../TestFiles/Output/"
21
22 // The following sample illustrates how to read/write a PDF document from/to
23 // a memory buffer. This is useful for applications that work with dynamic PDF
24 // documents that don't need to be saved/read from a disk.
25
26 // Read a PDF document in a memory buffer.
27 file := NewMappedFile(inputPath + "tiger.pdf")
28 fileSZ := file.FileSize()
29
30 fileReader := NewFilterReader(file)
31
32 mem := fileReader.Read(fileSZ)
33 memBytes := make([]byte, int(mem.Size()))
34 for i := 0; i < int(mem.Size()); i++{
35 memBytes[i] = mem.Get(i)
36 }
37 doc := NewPDFDoc(&memBytes[0], fileSZ)
38 doc.InitSecurityHandler()
39 numPages := doc.GetPageCount()
40
41 writer := NewElementWriter()
42 reader := NewElementReader()
43 element := NewElement()
44
45 // Create a duplicate of every page but copy only path objects
46
47 i := 1
48 for i <= numPages{
49 itr := doc.GetPageIterator(uint(2*i - 1))
50
51 reader.Begin(itr.Current())
52 new_page := doc.PageCreate(itr.Current().GetMediaBox())
53 next_page := itr
54 next_page.Next()
55 doc.PageInsert(next_page, new_page)
56
57 writer.Begin(new_page)
58 element = reader.Next()
59 for element.GetMp_elem().Swigcptr() != 0 { // Read page contents
60 //if element.GetType() == Element.e_path:
61 writer.WriteElement(element)
62 element = reader.Next()
63 }
64 writer.End()
65 reader.End()
66 i = i + 1
67 }
68 doc.Save(outputPath + "doc_memory_edit.pdf", uint(SDFDocE_remove_unused))
69
70 // Save the document to a memory buffer
71 buffer := (doc.Save(uint(SDFDocE_remove_unused))).(VectorUnChar)
72
73 // Write the contents of the buffer to the disk
74 bufferBytes := make([]byte, int(buffer.Size()))
75 for i := 0; i < int(buffer.Size()); i++{
76 bufferBytes[i] = buffer.Get(i)
77 }
78 f, err := os.Create(outputPath + "doc_memory_edit.txt")
79
80 if err != nil {
81 fmt.Println(err)
82 }
83 defer f.Close()
84 _, err2 := f.Write(bufferBytes)
85 if err2 != nil {
86 fmt.Println(err2)
87 }
88
89 // Read some data from the file stored in memory
90 reader.Begin(doc.GetPage(1))
91 element = reader.Next()
92 for element.GetMp_elem().Swigcptr() != 0{
93 if element.GetType() == ElementE_path{
94 os.Stdout.Write([]byte("Path, "))
95 }
96 element = reader.Next()
97 }
98 reader.End()
99
100 PDFNetTerminate()
101 fmt.Println("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...")
102}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales