Sample Obj-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 iOS 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#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9int main(int argc, char *argv[])
10{
11 @autoreleasepool {
12 int ret = 0;
13 [PTPDFNet Initialize: 0];
14
15 // Relative path to the folder containing test files.
16 NSString *input_path = @"../../TestFiles/";
17 NSString *output_path = @"../../TestFiles/Output/";
18
19
20 // The following sample illustrates how to read/write a PDF document from/to
21 // a memory buffer. This is useful for applications that work with dynamic PDF
22 // documents that don't need to be saved/read from a disk.
23 @try
24 {
25 // Read a PDF document in a memory buffer.
26 PTMappedFile *file = [[PTMappedFile alloc] initWithFilename: @"../../TestFiles/tiger.pdf"];
27 unsigned long file_sz = [file FileSize];
28
29 PTFilterReader *file_reader = [[PTFilterReader alloc] initWithFilter: file];
30
31 NSData *mem = [file_reader Read: file_sz];
32 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithBuf: mem buf_size: file_sz];
33
34 [doc InitSecurityHandler];
35 int num_pages = [doc GetPageCount];
36
37 PTElementWriter *writer = [[PTElementWriter alloc] init];
38 PTElementReader *reader = [[PTElementReader alloc] init];
39 PTElement *element;
40
41 // Create a duplicate of every page but copy only path objects
42 int i;
43 for(i=1; i<=num_pages; ++i)
44 {
45 PTPageIterator *itr = [doc GetPageIterator: 2*i-1];
46
47 [reader Begin: [itr Current]];
48 PTPage *new_page = [doc PageCreate: [[itr Current] GetMediaBox]];
49 PTPageIterator *next_page = itr;
50 [next_page Next];
51 [doc PageInsert: next_page page: new_page];
52
53 [writer WriterBeginWithPage: new_page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];
54 while ((element = [reader Next]) != NULL) // Read page contents
55 {
56 //if ([element GetType] == e_ptpath)
57 [writer WriteElement: element];
58 }
59
60 [writer End];
61 [reader End];
62 }
63
64 [doc SaveToFile: @"../../TestFiles/Output/doc_memory_edit.pdf" flags: e_ptremove_unused];
65 //[doc SaveToFile: @"../../TestFiles/Output/doc_memory_edit.pdf" flags: e_ptlinearized];
66
67 // Save the document to a memory buffer.
68 NSData *buf = [doc SaveToBuf: e_ptremove_unused];
69 // NSData *buf = [doc SaveToBuf: e_ptlinearized];
70
71 // Write the contents of the buffer to the disk
72 {
73 [buf writeToFile: @"../../TestFiles/Output/doc_memory_edit.txt" atomically: NO];
74 }
75
76 // Read some data from the file stored in memory
77 [reader Begin: [doc GetPage: 1]];
78 while ((element = [reader Next]) !=0) {
79 if ([element GetType] == e_ptpath) printf("%s", "Path, ");
80 }
81 [reader End];
82
83 printf("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...\n");
84 }
85 @catch(NSException *e)
86 {
87 NSLog(@"%@", e.reason);
88 ret = 1;
89 }
90 [PTPDFNet Terminate: 0];
91 return ret;
92
93 }
94}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9func runPDFDocMemoryTest() -> Int {
10 return autoreleasepool {
11 var ret = 0
12
13
14 // The following sample illustrates how to read/write a PDF document from/to
15 // a memory buffer. This is useful for applications that work with dynamic PDF
16 // documents that don't need to be saved/read from a disk.
17 do {
18 try PTPDFNet.catchException {
19 // Read a PDF document in a memory buffer.
20 let file: PTMappedFile = PTMappedFile(filename: Bundle.main.path(forResource: "tiger", ofType: "pdf"))
21 let file_sz: UInt = file.fileSize()
22
23 let file_reader: PTFilterReader = PTFilterReader(filter: file)
24
25 let mem: Data = file_reader.read(file_sz)
26 let doc: PTPDFDoc = PTPDFDoc(buf: mem, buf_size: file_sz)
27
28 doc.initSecurityHandler()
29 let num_pages = doc.getPageCount()
30
31 let writer: PTElementWriter = PTElementWriter()
32 let reader: PTElementReader = PTElementReader()
33
34 // Create a duplicate of every page but copy only path objects
35 for i in 1...num_pages {
36 let itr: PTPageIterator = doc.getPageIterator(UInt32(2 * i - 1))
37
38 reader.begin(itr.current())
39 let new_page: PTPage = doc.pageCreate(itr.current().getMediaBox())
40 let next_page: PTPageIterator = itr
41 next_page.next()
42 doc.pageInsert(next_page, page: new_page)
43
44 writer.writerBegin(with: new_page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)
45 while let element = reader.next() {
46 //if element.getType() == e_ptpath
47 writer.write(element)
48 }
49
50 writer.end()
51 reader.end()
52 }
53
54 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("doc_memory_edit.pdf").path, flags: e_ptremove_unused.rawValue)
55 //doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("doc_memory_edit.pdf").path, flags: e_ptlinearized)
56
57 // Save the document to a memory buffer.
58 let buf: Data = doc.save(toBuf: e_ptremove_unused.rawValue)
59 //let buf: Data = doc.save(toBuf: e_ptlinearized.rawValue)
60
61 // Write the contents of the buffer to the disk
62 do {
63 try buf.write(to: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("doc_memory_edit.txt"), options: .atomic)
64 } catch {
65
66 }
67
68 // Read some data from the file stored in memory
69 reader.begin(doc.getPage(1))
70 while let element = reader.next() {
71 if element.getType() == e_ptpath {
72 print("\("Path, ")")
73 }
74 }
75 reader.end()
76
77 print("Done. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...")
78 }
79 } catch let e as NSError {
80 print("\(e)")
81 ret = 1
82 }
83 return ret
84 }
85}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales