To open a PDF document.
1// open document from the filesystem
2PDFDoc doc = new PDFDoc(filename);
3
4// optionally read a PDF document from a stream
5MappedFile file = new MappedFile(filename);
6PDFDoc doc_stream = new PDFDoc(file);
7
8// or pass-in a memory buffer
9long file_sz = file.FileSize();
10FilterReader file_reader = new FilterReader(file);
11byte[] mem = new byte[(int) file_sz];
12long bytes_read = file_reader.Read(mem);
13PDFDoc doc_mem = new PDFDoc(mem, file_sz);
14
15// load from a URL
16Uri url = new Uri("https://myserver.com/myfile.pdf");
17HttpClient client = new HttpClient();
18var file_content = client.GetByteArrayAsync(url);
19PDFDoc doc = new PDFDoc(new MemoryStream(file_content));
1// open document from the filesystem
2PDFDoc doc(filename);
3
4// optionally read a PDF document from a stream
5MappedFile file(filename);
6PDFDoc doc_stream(file);
7
8// or pass-in a memory buffer
9size_t file_sz = file.FileSize();
10FilterReader file_reader(file);
11unsigned char* mem = new unsigned char[file_sz];
12file_reader.Read((unsigned char*)mem, file_sz);
13PDFDoc doc_mem(mem, file_sz);
14
15// loading from a URL requires an additional
16// library, e.g. "urdl" to use istream
17string file_content;
18int file_sz;
19istream url("https://myserver.com/myfile.pdf", ios::binary);
20getline(url, file_content, char_traits<char>::eof());
21file_sz = url.tellg();
22PDFDoc doc(file_content, file_sz);
1// open document from the filesystem
2doc := NewPDFDoc(filename)
3
4// optionally read a PDF document from a stream
5file := NewMappedFile(filename)
6docStream := NewPDFDoc(file)
7
8// or pass-in a memory buffer
9fileSZ := file.FileSize()
10fileReader := NewFilterReader(file)
11mem := fileReader.Read(fileSZ)
12memBytes := make([]byte, int(mem.Size()))
13for i := 0; i < int(mem.Size()); i++{
14 memBytes[i] = mem.Get(i)
15}
16doc := NewPDFDoc(&memBytes[0], fileSZ)
17
18// load from a URL
19resp, _ := http.Get("https://myserver.com/myfile.pdf")
20body, _ := ioutil.ReadAll(resp.Body)
21doc := NewPDFDoc(&body[0], int64(len(body)))
1// open document from the filesystem
2PDFDoc doc = new PDFDoc(filename);
3
4// optionally read a PDF document from a stream
5MappedFile file = new MappedFile(filename);
6PDFDoc doc_stream = new PDFDoc(file);
7
8// or pass-in a memory buffer
9long file_sz = file.fileSize();
10FilterReader file_reader = new FilterReader(file);
11byte[] mem = new byte[(int) file_sz];
12long bytes_read = file_reader.read(mem);
13PDFDoc doc_mem = new PDFDoc(mem, file_sz);
14
15// load from a URL
16URL url = new URL("https://myserver.com/myfile.pdf");
17PDFDoc doc = new PDFDoc(url.openStream(), url.getContentLength());
1// open document from the filesystem or URL
2async 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}
15PDFNet.runWithCleanup(main);
1// open document from the filesystem
2PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
3
4// optionally read a PDF document from a stream
5PTMappedFile *file = [[PTMappedFile alloc] initWithFilename: filename];
6PTPDFDoc *doc_stream = [[PTPDFDoc alloc] initWithStream: file];
7
8// or pass-in a memory buffer
9unsigned long file_sz = [file FileSize];
10PTFilterReader *file_reader = [[PTFilterReader alloc] initWithFilter: file];
11NSData *mem = [file_reader Read: file_sz];
12PTPDFDoc *doc_mem = [[PTPDFDoc alloc] initWithBuf: mem buf_size: file_sz];
13
14// load from a URL
15NSString *string_url = "https://myserver.com/myfile.pdf";
16NSString* url = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:string_url];
17NSData* file_content = [NSData dataWithContentsOfURL:url];
18PTPDFDoc *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
2doc = PDFDoc(filename)
3
4# optionally read a PDF document from a stream
5file = MappedFile(filename)
6doc_stream = PDFDoc(file)
7
8# or pass-in a memory buffer
9file_sz = file.FileSize()
10file_reader = FilterReader(file)
11mem = file_reader.Read(file_sz)
12doc_mem = PDFDoc(bytearray(mem), file_sz)
13
14# loading from a URL requires an additional
15# module, e.g. "requests"
16url = 'https://myserver.com/myfile.pdf'
17file_content = requests.get(url)
18doc = PDFDoc(bytearray(file_content.content), len(file_content.content))
1# open document from the filesystem
2doc = PDFDoc.new(filename)
3
4# optionally read a PDF document from a stream
5file = MappedFile(filename)
6doc_stream = PDFDoc.new(file)
7
8# or pass-in a memory buffer
9file_sz = file.FileSize()
10file_reader = FilterReader.new(file)
11mem = file_reader.Read(file_sz)
12doc_mem = PDFDoc.new(mem, file_sz)
13
14# loading from a URL requires the
15# standard module "open-uri"
16file_content = open('https://myserver.com/myfile.pdf') { |f| f.read }
17doc = PDFDoc.new(file_content, file_content.size);
1' open document from the filesystem
2Dim doc as PDFDoc = New PDFDoc(filename)
3
4' optionally read a PDF document from a stream
5Dim file As MappedFile = New MappedFile(filename)
6Dim doc_stream As PDFDoc = New PDFDoc(file)
7
8' or pass-in a memory buffer
9Dim file_sz As Long = file.FileSize()
10Dim file_reader As FilterReader = New FilterReader(file)
11Dim mem(file_sz) As Byte
12Dim bytes_read As Long = file_reader.Read(mem)
13Dim doc_mem As PDFDoc = New PDFDoc(mem, file_sz)
14
15' load from a URL
16Dim url As String = "https://myserver.com/myfile.pdf"
17Dim web_client As WebClient = New WebClient()
18Dim stream_reader As StreamReader = New StreamReader(web_client.OpenRead(url))
19Dim mem() As Byte = System.Text.Encoding.UTF8.GetBytes(stream_reader.ReadToEnd)
20Dim 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:
When you are finished with a PDFDoc object, the PDFDoc.Close() method should be called to clean up memory, file handles, and resources.
1PDFDoc new_doc = new PDFDoc();
1PDFDoc doc;
1doc := NewPDFDoc()
1PDFDoc doc = new PDFDoc();
1async function main() {
2 const doc = await PDFNet.PDFDoc.create();
3}
4PDFNet.runWithCleanup(main);
1PTPDFDoc *doc = [[PTPDFDoc alloc] init];
1$doc = new PDFDoc();
1doc = PDFDoc()
1doc = PDFDoc.new()
1Dim 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:
1PDFDoc new_doc = new PDFDoc(filename);
1PDFDoc doc(filename);
1doc := NewPDFDoc(filename)
1PDFDoc doc = new PDFDoc(filename);
1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3}
4PDFNet.runWithCleanup(main);
1PTPDFDoc doc = [[PTPDFDoc alloc] initWithFilePath: filename];
1$doc = new PDFDoc($filename);
1doc = PDFDoc(filename)
1doc = PDFDoc.new(filename)
1Dim doc as PDFDoc = New PDFDoc(filename)
Here's how to open an existing PDF document from a memory buffer:
1MappedFile file = new MappedFile(filename);
2long file_sz = file.FileSize();
3FilterReader file_reader = new FilterReader(file);
4byte[] mem = new byte[(int) file_sz];
5long bytes_read = file_reader.Read(mem);
6PDFDoc doc_mem = new PDFDoc(mem, file_sz);
1MappedFile file(filename);
2size_t file_sz = file.FileSize();
3FilterReader file_reader(file);
4unsigned char* mem = new unsigned char[file_sz];
5file_reader.Read((unsigned char*)mem, file_sz);
6PDFDoc doc(mem, file_sz);
1file := NewMappedFile(filename)
2fileSZ := file.FileSize()
3fileReader := NewFilterReader(file)
4mem := fileReader.Read(fileSZ)
5memBytes := make([]byte, int(mem.Size()))
6for i := 0; i < int(mem.Size()); i++{
7 memBytes[i] = mem.Get(i)
8}
9doc := NewPDFDoc(&memBytes[0], fileSZ)
1MappedFile file = new MappedFile(filename);
2long file_sz = file.fileSize();
3FilterReader file_reader = new FilterReader(file);
4byte[] mem = new byte[(int) file_sz];
5long bytes_read = file_reader.read(mem);
6PDFDoc doc_mem = new PDFDoc(mem, file_sz);
1async 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}
8PDFNet.runWithCleanup(main);
1PTMappedFile *file = [[PTMappedFile alloc] initWithFilename: filename];
2unsigned long file_sz = [file FileSize];
3PTFilterReader *file_reader = [[PTFilterReader alloc] initWithFilter: file];
4NSData *mem = [file_reader Read: file_sz];
5PTPDFDoc *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);
1file = MappedFile(filename)
2file_sz = file.FileSize()
3file_reader = FilterReader(file)
4mem = file_reader.Read(file_sz)
5doc = PDFDoc(bytearray(mem), file_sz)
1file = MappedFile(filename)
2file_sz = file.FileSize()
3file_reader = FilterReader.new(file)
4mem = file_reader.Read(file_sz)
5doc = PDFDoc.new(mem, file_sz)
1Dim file As MappedFile = New MappedFile(filename)
2Dim file_sz As Long = file.FileSize()
3Dim file_reader As FilterReader = New FilterReader(file)
4Dim mem(file_sz) As Byte
5Dim bytes_read As Long = file_reader.Read(mem)
6Dim 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.
1PDFDoc doc = new PDFDoc(filename);
2if (!doc.InitSecurityHandler())
3{
4 Console.WriteLine("Document authentication error...");
5 return;
6}
1PDFDoc doc(filename);
2if (!doc.InitSecurityHandler())
3{
4 printf("Document authentication error...");
5 return;
6}
1doc := NewPDFDoc(filename)
2if !doc.InitSecurityHandler()
3{
4 fmt.Println("Document authentication error...");
5 return;
6}
1PDFDoc doc = new PDFDoc(filename);
2if (!doc.initSecurityHandler())
3{
4 println("Document authentication error...");
5 return;
6}
1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromURL(filename);
3 if (!doc.initSecurityHandler()) {
4 console.log("Document authentication error...");
5 return;
6 }
7}
8PDFNet.runWithCleanup(main);
1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilePath: filename];
2if (![doc InitSecurityHandler])
3{
4 printf("Document authentication error...");
5 return;
6}
1$doc = new PDFDoc($filename);
2if (!$doc->InitSecurityHandler())
3{
4 echo "Document authentication error...";
5 return;
6}
1doc = PDFDoc(filename)
2if not doc.InitSecurityHandler():
3 print("Document authentication error...")
4 return
1doc = PDFDoc.new(filename)
2if !doc.InitSecurityHandler()
3 puts "Document authentication error..."
4 return
5end
1Dim doc as PDFDoc = New PDFDoc(filename)
2If Not doc.InitSecurityHandler() Then
3 Console.Write("Document authentication error...")
4 Return
5End If
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales