To open a PDF document.
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 // open document from the filesystem
2 PDFDoc doc = new PDFDoc (filename);
3
4 // optionally read a PDF document from a stream
5 MappedFile file = new MappedFile (filename);
6 PDFDoc doc_stream = new PDFDoc (file);
7
8 // or pass-in a memory buffer
9 long file_sz = file. FileSize ();
10 FilterReader file_reader = new FilterReader (file);
11 byte [] mem = new byte [( int ) file_sz];
12 long bytes_read = file_reader. Read (mem);
13 PDFDoc doc_mem = new PDFDoc (mem, file_sz);
14
15 // load from a URL
16 Uri url = new Uri ( " https://myserver.com/myfile.pdf " );
17 HttpClient client = new HttpClient ();
18 var file_content = client. GetByteArrayAsync (url);
19 PDFDoc doc = new PDFDoc ( new MemoryStream (file_content));
1 // open document from the filesystem
2 PDFDoc doc ( filename );
3
4 // optionally read a PDF document from a stream
5 MappedFile file ( filename );
6 PDFDoc doc_stream ( file );
7
8 // or pass-in a memory buffer
9 size_t file_sz = file. FileSize ();
10 FilterReader file_reader ( file );
11 unsigned char* mem = new unsigned char [file_sz];
12 file_reader. Read (( unsigned char* )mem, file_sz);
13 PDFDoc doc_mem ( mem , file_sz );
14
15 // loading from a URL requires an additional
16 // library, e.g. "urdl" to use istream
17 string file_content;
18 int file_sz;
19 istream url ( " https://myserver.com/myfile.pdf " , ios :: binary );
20 getline (url, file_content, char_traits< char > :: eof ());
21 file_sz = url. tellg ();
22 PDFDoc doc ( file_content , file_sz );
1 // open document from the filesystem
2 doc := NewPDFDoc (filename)
3
4 // optionally read a PDF document from a stream
5 file := NewMappedFile (filename)
6 docStream := NewPDFDoc (file)
7
8 // or pass-in a memory buffer
9 fileSZ := file. FileSize ()
10 fileReader := NewFilterReader (file)
11 mem := fileReader. Read (fileSZ)
12 memBytes := make ([] byte , int (mem. Size ()))
13 for i := 0 ; i < int (mem. Size ()); i ++ {
14 memBytes[i] = mem. Get (i)
15 }
16 doc := NewPDFDoc ( & memBytes[ 0 ], fileSZ)
17
18 // load from a URL
19 resp, _ := http. Get ( " https://myserver.com/myfile.pdf " )
20 body, _ := ioutil. ReadAll (resp.Body)
21 doc := NewPDFDoc ( & body[ 0 ], int64 ( len (body)))
1 // open document from the filesystem
2 PDFDoc doc = new PDFDoc (filename);
3
4 // optionally read a PDF document from a stream
5 MappedFile file = new MappedFile (filename);
6 PDFDoc doc_stream = new PDFDoc (file);
7
8 // or pass-in a memory buffer
9 long file_sz = file. fileSize ();
10 FilterReader file_reader = new FilterReader (file);
11 byte [] mem = new byte [( int ) file_sz];
12 long bytes_read = file_reader. read (mem);
13 PDFDoc doc_mem = new PDFDoc (mem, file_sz);
14
15 // load from a URL
16 URL url = new URL ( " https://myserver.com/myfile.pdf " );
17 PDFDoc doc = new PDFDoc (url. openStream (), url. getContentLength ());
1 // open document from the filesystem or URL
2 async function main () {
3 const doc = await PDFNet.PDFDoc. createFromURL (filename);
4
5 // optionally read a PDF document from a stream
6 const file = await PDFNet.Filter. createURLFilter (filename);
7 const doc_stream = await PDFNet.PDFDoc. createFromFilter (file);
8
9 // or pass-in a memory buffer
10 const file_sz = await file. size ();
11 const file_reader = await PDFNet.FilterReader. create (file);
12 const mem = await file_reader. read (file_sz);
13 const doc_mem = await PDFNet.PDFDoc. createFromBuffer (mem);
14 }
15 PDFNet. runWithCleanup (main);
1 // open document from the filesystem
2 PTPDFDoc * doc = [[PTPDFDoc alloc ] initWithFilepath : filename];
3
4 // optionally read a PDF document from a stream
5 PTMappedFile * file = [[PTMappedFile alloc ] initWithFilename : filename];
6 PTPDFDoc * doc_stream = [[PTPDFDoc alloc ] initWithStream : file];
7
8 // or pass-in a memory buffer
9 unsigned long file_sz = [file FileSize ];
10 PTFilterReader * file_reader = [[PTFilterReader alloc ] initWithFilter : file];
11 NSData * mem = [file_reader Read : file_sz];
12 PTPDFDoc * doc_mem = [[PTPDFDoc alloc ] initWithBuf : mem buf_size : file_sz];
13
14 // load from a URL
15 NSString * string_url = " https://myserver.com/myfile.pdf " ;
16 NSString * url = [[ NSBundle mainBundle ].bundlePath stringByAppendingPathComponent :string_url];
17 NSData * file_content = [ NSData dataWithContentsOfURL :url];
18 PTPDFDoc * doc = [[PTPDFDoc alloc ] initWithBuf : file_content buf_size : file_content.length];
1 // open document from the filesystem
2 $doc = new PDFDoc ($filename);
3
4 // optionally read a PDF document from a stream
5 $file = new MappedFile ($filename);
6 $doc_stream = new PDFDoc ($file);
7
8 // or pass-in a memory buffer
9 $file_sz = $file -> FileSize ();
10 $file_reader = new FilterReader ($file);
11 $mem = $file_reader -> Read ($file_sz);
12 $doc_mem = new PDFDoc ($mem, $file_sz);
13
14 // load from a URL
15 $url = ' https://myserver.com/myfile.pdf '
16 $file_content = file_get_contents ($url);
17 $doc = new PDFDoc ($file_content, strlen ($file_content));
1 # open document from the filesystem
2 doc = PDFDoc(filename)
3
4 # optionally read a PDF document from a stream
5 file = MappedFile(filename)
6 doc_stream = PDFDoc(file)
7
8 # or pass-in a memory buffer
9 file_sz = file.FileSize()
10 file_reader = FilterReader(file)
11 mem = file_reader.Read(file_sz)
12 doc_mem = PDFDoc( bytearray (mem), file_sz)
13
14 # loading from a URL requires an additional
15 # module, e.g. "requests"
16 url = ' https://myserver.com/myfile.pdf '
17 file_content = requests.get(url)
18 doc = PDFDoc( bytearray (file_content.content), len (file_content.content))
1 # open document from the filesystem
2 doc = PDFDoc . new (filename)
3
4 # optionally read a PDF document from a stream
5 file = MappedFile (filename)
6 doc_stream = PDFDoc . new (file)
7
8 # or pass-in a memory buffer
9 file_sz = file. FileSize ()
10 file_reader = FilterReader . new (file)
11 mem = file_reader. Read (file_sz)
12 doc_mem = PDFDoc . new (mem, file_sz)
13
14 # loading from a URL requires the
15 # standard module "open-uri"
16 file_content = open ( ' https://myserver.com/myfile.pdf ' ) { |f| f.read }
17 doc = PDFDoc . new (file_content, file_content.size);
1 ' open document from the filesystem
2 Dim doc as PDFDoc = New PDFDoc (filename)
3
4 ' optionally read a PDF document from a stream
5 Dim file As MappedFile = New MappedFile (filename)
6 Dim doc_stream As PDFDoc = New PDFDoc (file)
7
8 ' or pass-in a memory buffer
9 Dim file_sz As Long = file. FileSize ()
10 Dim file_reader As FilterReader = New FilterReader (file)
11 Dim mem(file_sz) As Byte
12 Dim bytes_read As Long = file_reader. Read (mem)
13 Dim doc_mem As PDFDoc = New PDFDoc (mem, file_sz)
14
15 ' load from a URL
16 Dim url As String = " https://myserver.com/myfile.pdf "
17 Dim web_client As WebClient = New WebClient ()
18 Dim stream_reader As StreamReader = New StreamReader (web_client. OpenRead (url))
19 Dim mem() As Byte = System.Text.Encoding.UTF8. GetBytes (stream_reader.ReadToEnd)
20 Dim doc As PDFDoc = New PDFDoc (mem, Len (mem))
Read & write a PDF file from/to memory buffer Full source code which illustrates how to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDF documents that don't need to be saved/read from a disk.
The PDFDoc constructor creates a PDF document from scratch:
PDFDoc.Close() When you are finished with a PDFDoc object, the PDFDoc.Close() method should be called to clean up memory, file handles, and resources.
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 PDFDoc new_doc = new PDFDoc ();
1 PDFDoc doc = new PDFDoc ();
1 async function main () {
2 const doc = await PDFNet.PDFDoc. create ();
3 }
4 PDFNet. runWithCleanup (main);
1 PTPDFDoc * doc = [[PTPDFDoc alloc ] init ];
1 Dim doc As PDFDoc = New PDFDoc ()
A newly-created document does not yet contain any pages. See the accessing pages section for details on creating new pages and working with existing pages.
Using Apryse SDK, you can open a document from a serialized file, from a memory buffer, or from a Filter stream.
To open an existing PDF document from a file, specify its file path in the PDFDoc constructor:
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 PDFDoc new_doc = new PDFDoc (filename);
1 doc := NewPDFDoc (filename)
1 PDFDoc doc = new PDFDoc (filename);
1 async function main () {
2 const doc = await PDFNet.PDFDoc. createFromURL (filename);
3 }
4 PDFNet. runWithCleanup (main);
1 PTPDFDoc doc = [[PTPDFDoc alloc ] initWithFilePath : filename];
1 $doc = new PDFDoc ($filename);
1 doc = PDFDoc . new (filename)
1 Dim doc as PDFDoc = New PDFDoc (filename)
Here's how to open an existing PDF document from a memory buffer:
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 MappedFile file = new MappedFile (filename);
2 long file_sz = file. FileSize ();
3 FilterReader file_reader = new FilterReader (file);
4 byte [] mem = new byte [( int ) file_sz];
5 long bytes_read = file_reader. Read (mem);
6 PDFDoc doc_mem = new PDFDoc (mem, file_sz);
1 MappedFile file ( filename );
2 size_t file_sz = file. FileSize ();
3 FilterReader file_reader ( file );
4 unsigned char* mem = new unsigned char [file_sz];
5 file_reader. Read (( unsigned char* )mem, file_sz);
6 PDFDoc doc ( mem , file_sz );
1 file := NewMappedFile (filename)
2 fileSZ := file. FileSize ()
3 fileReader := NewFilterReader (file)
4 mem := fileReader. Read (fileSZ)
5 memBytes := make ([] byte , int (mem. Size ()))
6 for i := 0 ; i < int (mem. Size ()); i ++ {
7 memBytes[i] = mem. Get (i)
8 }
9 doc := NewPDFDoc ( & memBytes[ 0 ], fileSZ)
1 MappedFile file = new MappedFile (filename);
2 long file_sz = file. fileSize ();
3 FilterReader file_reader = new FilterReader (file);
4 byte [] mem = new byte [( int ) file_sz];
5 long bytes_read = file_reader. read (mem);
6 PDFDoc doc_mem = new PDFDoc (mem, file_sz);
1 async function main () {
2 const file = await PDFNet.Filter. createURLFilter (filename);
3 const file_sz = await file. size ();
4 const file_reader = await PDFNet.FilterReader. create (file);
5 const mem = await file_reader. read (file_sz);
6 const doc_mem = await PDFNet.PDFDoc. createFromBuffer (mem);
7 }
8 PDFNet. runWithCleanup (main);
1 PTMappedFile * file = [[PTMappedFile alloc ] initWithFilename : filename];
2 unsigned long file_sz = [file FileSize ];
3 PTFilterReader * file_reader = [[PTFilterReader alloc ] initWithFilter : file];
4 NSData * mem = [file_reader Read : file_sz];
5 PTPDFDoc * doc_mem = [[PTPDFDoc alloc ] initWithBuf : mem buf_size : file_sz];
1 $file = new MappedFile ($filename);
2 $file_sz = $file -> FileSize ();
3 $file_reader = new FilterReader ($file);
4 $mem = $file_reader -> Read ($file_sz);
5 $doc_mem = new PDFDoc ($mem, $file_sz);
1 file = MappedFile(filename)
2 file_sz = file.FileSize()
3 file_reader = FilterReader(file)
4 mem = file_reader.Read(file_sz)
5 doc = PDFDoc( bytearray (mem), file_sz)
1 file = MappedFile (filename)
2 file_sz = file. FileSize ()
3 file_reader = FilterReader . new (file)
4 mem = file_reader. Read (file_sz)
5 doc = PDFDoc . new (mem, file_sz)
1 Dim file As MappedFile = New MappedFile (filename)
2 Dim file_sz As Long = file. FileSize ()
3 Dim file_reader As FilterReader = New FilterReader (file)
4 Dim mem(file_sz) As Byte
5 Dim bytes_read As Long = file_reader. Read (mem)
6 Dim doc_mem As PDFDoc = New PDFDoc (mem, file_sz)
It's also easy to open a PDF document from a MemoryFilter or a custom Filter .
After creating a PDFDoc object, it's good practice to call InitSecurityHandler()
on it. If the document is encrypted, calling the method will decrypt it. If the document is not encrypted, calling the method is harmless.
C# C++ Go Java JavaScript Obj-C PHP Python Ruby VB
1 PDFDoc doc = new PDFDoc (filename);
2 if ( ! doc. InitSecurityHandler ())
3 {
4 Console. WriteLine ( " Document authentication error... " );
5 return ;
6 }
1 PDFDoc doc ( filename );
2 if ( ! doc. InitSecurityHandler ())
3 {
4 printf ( " Document authentication error... " );
5 return ;
6 }
1 doc := NewPDFDoc (filename)
2 if ! doc. InitSecurityHandler ()
3 {
4 fmt. Println ( " Document authentication error... " );
5 return ;
6 }
1 PDFDoc doc = new PDFDoc (filename);
2 if ( ! doc. initSecurityHandler ())
3 {
4 println ( " Document authentication error... " );
5 return ;
6 }
1 async function main () {
2 const doc = await PDFNet.PDFDoc. createFromURL (filename);
3 if ( ! doc. initSecurityHandler ()) {
4 console. log ( " Document authentication error... " );
5 return ;
6 }
7 }
8 PDFNet. runWithCleanup (main);
1 PTPDFDoc * doc = [[PTPDFDoc alloc ] initWithFilePath : filename];
2 if ( ! [doc InitSecurityHandler ])
3 {
4 printf ( " Document authentication error... " );
5 return ;
6 }
1 $doc = new PDFDoc ($filename);
2 if ( ! $doc -> InitSecurityHandler ())
3 {
4 echo " Document authentication error... " ;
5 return ;
6 }
1 doc = PDFDoc(filename)
2 if not doc.InitSecurityHandler():
3 print ( " Document authentication error... " )
4 return
1 doc = PDFDoc . new (filename)
2 if ! doc. InitSecurityHandler ()
3 puts " Document authentication error... "
4 return
5 end
1 Dim doc as PDFDoc = New PDFDoc (filename)
2 If Not doc. InitSecurityHandler () Then
3 Console. Write ( " Document authentication error... " )
4 Return
5 End If