Opening and Saving PDFs

This guide covers how to open and save PDFs with the PSPDFKit .NET Library. We will cover some simple examples and give an overview of saving options.

Opening PDFs

The simplest input source for opening a document is a file path. Document expects an IDataProvider to read from, so to pass in a file path, you are required to provide a FileDataProvider that wraps the path you desire:

1
var document = new Document(new FileDataProvider("Assets/default.pdf"));

To ensure maximum flexibility of document sources, PSPDFKit comes with an extensible class to provide a custom data provider.

Writing a data provider inheriting from IDataProvider will allow you to provide data from any source possible (e.g. cloud providers, in-memory, encrypted sources).

Saving PDFs

Saving a document is as simple as calling Save, but there are more options for finer-grained control:

1
document.Save(new DocumentSaveOptions());

Incremental Saving

By default, PSPDFKit saves to files incrementally. This is a feature of PDFs and means that when saving, the initial content of the original document is not altered and all changes are appended to the file. This can result in a significantly faster save when working with a large file, as changes are typically quite small.

However, you should bear in mind that since the changes are always appended, the file will constantly increase in size with every save, regardless of the changes being made. Since this is sometimes undesirable, you can specify that the PDF should be completely rewritten upon saving.

Here is an example of saving incrementally:

1
2
3
4
document.Save(new DocumentSaveOptions
{
    incremental = true
});

Here is how to rewrite the PDF when saving:

1
2
3
4
document.Save(new DocumentSaveOptions
{
    incremental = false
});

Again, to ensure maximum flexibility of document sources, PSPDFKit comes with an extensible class to provide a custom data provider that is writable.

Writing a data provider inheriting from IWriteableDataProvider will allow you to sink data to any source possible (e.g. cloud providers, in-memory, encrypted sources).