How can I help you?
Save a Document in .NET MAUI PDF Viewer (SfPdfViewer)
25 Mar 20263 minutes to read
After annotating, filling forms, or applying redactions, use the PDF Viewer’s save methods to persist all changes back to a file stream. You can save synchronously with SaveDocument or asynchronously with SaveDocumentAsync — and optionally flatten annotations or form fields into static content at the same time.
The following example explains how to save the modified document if the document needs to be saved in the application’s data directory.
private void SaveButton_Clicked(object sender, EventArgs e)
{
SaveDocument();
}
private void SaveDocument()
{
// Create a file stream to save the PDF document.
// The file "ModifiedDocument.pdf" is written to the app's sandboxed data directory.
string fileName = Path.Combine(FileSystem.Current.AppDataDirectory, "ModifiedDocument.pdf");
using FileStream fileStream = File.Create(fileName);
// Save the PDF document to the stream.
PdfViewer.SaveDocument(fileStream);
}Save a document asynchronously
To save a document without blocking the UI thread, use the SaveDocumentAsync method. This is the recommended approach for saving from event handlers or UI interactions. An optional CancellationToken can be passed to cancel the operation if needed.
private async void SaveButton_Clicked(object sender, EventArgs e)
{
await SaveDocumentAsync();
}
private async Task SaveDocumentAsync()
{
// Create a file stream to save the PDF document asynchronously.
string fileName = Path.Combine(FileSystem.Current.AppDataDirectory, "ModifiedDocument.pdf");
using FileStream fileStream = File.Create(fileName);
// Save the PDF document without blocking the UI thread.
await PdfViewer.SaveDocumentAsync(fileStream);
}Flatten annotations and form fields on save
Flattening refers to the process of converting interactive elements, such as annotations and form fields, into a static, non-editable format within a PDF document. The SfPdfViewer allows you to save the annotations and form fields by flattening using the FlattenOnSave API.
Flatten the annotation on save
The FlattenOnSave API helps you to flatten the specified annotation in a PDF document. The following code sample explains how to flatten the first annotation from the annotation collection.
// Obtain the annotation collection using [SfPdfViewer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.SfPdfViewer.html) instance.
ReadOnlyObservableCollection<Annotation> annotations = PdfViewer.Annotations;
// Obtain the first annotation in the annotation collection.
Annotation annotation = annotations[0];
// set the FlattenOnSave true to flatten the annotation on save
annotation.FlattenOnSave = true;Flatten form field on save
The FlattenOnSave API helps you to flatten the specified form field in a PDF document. The following code sample explains how to flatten the first form field from the form field collection.
// Obtain the form field collection using [SfPdfViewer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.SfPdfViewer.html) instance.
ReadOnlyObservableCollection<FormField> formFields = PdfViewer.FormFields;
// Obtain the first form field in the form fields collection.
FormField formField = formFields[0];
// set the FlattenOnSave true to flatten the formfield on save
formField.FlattenOnSave = true;Limitation
Currently, when saving a document by flattening that contains sticky note annotations, the sticky note icon always appears as the default comment icon appearance in the saved document.