PDFDocMemory

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales