Save a PDF to Local Storage in MAUI

PSPDFKit for MAUI enables exporting documents in a variety of ways. This guide covers the simplest one: saving to local storage.

Note that there’s a platform-specific setup required for FileSaver. It’s worth reading about the setup here if you’re not familiar with it.

Exporting a Document

In our APIs, the word export is synonymous with save. The snippet below allows you to export a currently open document to its original source:

    var exportConfiguration = _document.CreateExportConfiguration();
    // Update the configuration here.

    var exportedDocumentContent = await _document.ExportDocumentAsync(exportConfiguration);
    var result = await FileSaver.Default.SaveAsync(
        "download.pdf", new MemoryStream(exportedDocumentContent), CancellationToken.None);
    if (!string.IsNullOrEmpty(result.FilePath) && !result.IsSuccessful)
        throw result.Exception;
catch (Exception ex)
    // Handle the exception.

For more control over exporting documents, use IExportConfiguration. If you pass null instead, the default configuration will be used:

var exportOptions = _document.CreateExportConfiguration();
    ExcludeAnnotations = true,
    ExportForPrinting = true,
    ExportIncrementally = false,
    Flatten = true,

var exportedDocumentContent = await _document.ExportDocumentAsync(exportConfiguration);