Save a Document in .NET MAUI PDF Viewer (SfPdfViewer)

13 Jun 20242 minutes to read

The SfPdfViewer allows you to save a modified document to the specified file stream. It helps you to prevent your work from being lost. You need to provide the PDF file stream as a parameter to the SaveDocument method of the SfPdfViewer. The modified document will be saved to the given file stream.

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 SaveDocument()
{
	// Create a file stream to save the PDF document. Here a file named "ModifiedDocument.pdf" is created in the application's data directory.
	string fileName = Path.Combine(FileSystem.Current.AppDataDirectory, "ModifiedDocument.pdf");
	using FileStream fileStream = File.Create(fileName);
	
	// Save the PDF document.
	PdfViewer.SaveDocument(fileStream);
}

To save a document asynchronously, you may use the SaveDocumentAsync method of the SfPdfViewer.

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 Flatten using FlattenOnSave API.

Flatten 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 Flatten that contains sticky note annotations, the sticky note icon always appears as the default comment icon appearance in the saved document.