Save PDFs to a custom data provider in UWP

Nutrient UWP SDK includes support for saving DocumentSources to data providers.

A data provider defines a common interface for Nutrient to read and write PDF documents from custom sources. This is especially helpful if you want to support your own encryption or compression scheme.

Existing data providers

Nutrient UWP SDK ships with two predefined providers:

You can also write your own custom data providers by implementing the IDataProvider interface. The Catalog project contains another two examples of custom providers:

Custom data provider

Creating your own custom data provider is a straightforward process. You’ll have to create a class that implements the IDataProvider interface and all its methods. These methods should help you structure your data in a way that’s compatible with Nutrient UWP SDK.

Writing

Writing is done separately from reading, and it uses data sinks. These are classes specifically for writing data. They represent a destination to write to and allow you to export documents directly to streams or other desired mediums.

Nutrient UWP SDK ships with two default data sinks, which support DataWriters and IRandomAccessStreams. Similar to data providers, you can also implement your own data sinks through IDataSink.

The WriteDataAsync method allows you to control this process:

public IRandomAccessStream Stream { get; set; }

private async Task<bool> WriteDataAsync(IBuffer data)
{
    using (var outputStream = Stream.GetOutputStreamAt(Stream.Size))
    {
        await outputStream.WriteAsync(data);
    }

    return true;
}

Depending on the medium being used and your settings, the DataSinkOption class tells your data sink whether to start at the beginning of the stream or to append to it. For example, when incremental saves or annotation flattening are enabled, IDataSink must be empty, as the entire document is written out and appending is not supported.

Finally, documents can be exported to your IDataSink with the Document.ExportToDataSinkAsync method:

await PDFView.Document.ExportToDataSinkAsync(new ExampleDataSink(option) { Stream = _fileStream }, exportOptions);

Our Catalog application includes two examples of IDataSink implementations.