Importing and exporting data in Xamarin Pdf Viewer (SfPdfViewer)

PDF Viewer provides options to import and export data like form fields and annotations in the PDF document.

Importing and exporting form data

The import and export of form data supports the following extensions.

  • fdf
  • xfdf
  • json
  • xml

The required file type can be chosen from the DataFormat enumeration. In the following sections only the fdf file type is explained for brevity.

Exporting form data

The ExportFormData method exports the current data filled in the form fields into a stream in the specified file format. The name of the PDF (with extension) from which the data are exported should be given as the second argument to the method.

//Export form data to "fdf" data format
Stream fdfStreamToSave = pdfViewerControl.ExportFormData(DataFormat.Fdf, "PdfFileName.fdf");

NOTE

While saving the stream returned by the ExportFormData method, the file name must have the same extension as the file type that was given as the first argument. For example, in the above case the file type provided is fdf, so the saved file extension should be .fdf.

Importing form data

The ImportFormData method imports the data from a file of specified type and fills the saved data into the form fields.

//Import form data from "fdf" data format
Stream fdfStreamToImport = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleDefaultNamespace.Assets.PdfFileName.fdf");
pdfViewerControl.ImportFormData(fdfStreamToImport, DataFormat.Fdf);

Importing and exporting annotations

The import and export of annotations support following extensions.

  • fdf
  • xfdf

The required file type can be chosen from the DataFormat enumeration. In the following sections only the fdf file type is explained for brevity.

Exporting annotations

The ExportAnnotations method exports the current annotations added to the document into a stream in the specified file format. The name of the PDF (with extension) from which the annotations are exported should be given as the second argument to the method.

//Export annotations to "fdf" data format
Stream fdfStreamToSave = pdfViewerControl.ExportAnnotations(AnnotationDataFormat.Fdf, "PdfFileName.fdf");

Export annotations asynchronously

The PDF Viewer allows you to export the current annotations added to the document into a stream asynchronously using the ExportAnnotationsAsync method. You can also cancel the asynchronous annotation exporting when it is in progress.

//Export the annotations asynchronously to the "fdf" data format 
Stream fdfStreamToSave = await pdfViewerControl.ExportAnnotationsAsync(AnnotationDataFormat.Fdf, cancellationTokenSource);

In the above code sample, the cancellationTokenSource enables you to cancel the asynchronous annotation exporting when it is in progress.

NOTE

While saving the stream returned by the ExportAnnotations and ExportAnnotationsAsync method, the file name must have the same extension as the file type that was given as the first argument. e.g. In the above case the file type provided is fdf, so the saved file extension should be .fdf.

Cancel the asynchronous annotations exporting

You can raise the cancel request when the annotations exporting is in progress

private void cancelButton_Clicked(object sender, EventArgs e)
 { 
   cancellationTokenSource.Cancel();
 }

In the above code sample, the cancellationTokenSource instance is the same as the one given as the argument when exporting the annotations asynchronously.

NOTE

Calling the above method will not have any effect once the annotations are completely exported. It will stop the exporting process only when it is in progress.

Importing annotations

The ImportAnnotations method imports the annotations from the file of specified type and fills the saved annotations into the loaded PDF document.

//Import annotations from "fdf" data format
Stream fdfStreamToImport = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleDefaultNamespace.Assets.PdfFileName.fdf");
pdfViewerControl.ImportAnnotations(fdfStreamToImport, AnnotationDataFormat.Fdf);