Open a PDF in UWP

To open a PDF document.

C#

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 documentStream = new PDFDoc(file);
7
8// or pass-in a memory buffer
9ulong fileSize = file.WinRTGetFileSize();
10FilterReader fileReader = new FilterReader(file);
11byte[] memoryBuffer = new byte[(int)fileSize];
12long bytesRead = fileReader.Read(memoryBuffer);
13PDFDoc documentMemory = new PDFDoc(memoryBuffer);
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));

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.

About opening a document

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#

1PDFDoc new_doc = 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#

1PDFDoc new_doc = new PDFDoc(filename);

Here's how to open an existing PDF document from a memory buffer:

C#

1MappedFile file = new MappedFile(filename);
2ulong fileSize = file.WinRTGetFileSize();
3FilterReader fileReader = new FilterReader(file);
4byte[] memoryBuffer = new byte[(int)fileSize];
5long bytesRead = fileReader.Read(memoryBuffer);
6PDFDoc doc = new PDFDoc(memoryBuffer);

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#

1PDFDoc doc = new PDFDoc(filename);
2if (!doc.InitSecurityHandler())
3{
4 Console.WriteLine("Document authentication error...");
5 return;
6}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales