PDF files are primarily intended for viewing rather than editing. They were developed to present document content (text, images, and other elements) in the same manner, regardless of the operating system, device, or software application on which they are viewed. To add more information to a PDF document, annotations are used.
Annotations are additional details that include explanations, clarifications, review comments, and highlights (key points) on the original PDF file’s contents.
Let’s consider the case of an online PDF library, where the users access different PDF books and want to highlight the key points and save them. Usually, to save highlighted points in a PDF file, the entire file has to be saved along with the annotations. For large PDF files, maintaining multiple copies for each user would consume a lot of memory. The more effective way to handle this would be to export just the annotations as separate files for individual users and import them when the users load the PDF file. For a PDF document megabytes in size, the size of the exported annotations will be in kilobytes.
In this blog, we will look at how to export and import annotations in PDF documents using the WPF PDF Viewer.
Syncfusion WPF PDF Viewer
Syncfusion’s WPF PDF Viewer control lets you view, review, and print PDF files in WPF applications. The thumbnail, bookmark, hyperlink, and table of contents support provide easy navigation within and outside the PDF files. The form-filling support provides a platform to fill, flatten, save, and print PDF files with AcroForm.
You can review PDF files with abundant annotation tools. You can add, modify, and remove these annotations using the WPF PDF Viewer. Also, we can easily save, print, and export them.
Annotation import and export formats
The WPF PDF Viewer supports exporting and importing the annotation data from a PDF in FDF and XFDF formats.
FDF (forms data format): This format allows us to export forms data to stand-alone files. We can save and transmit the exported data electronically and then import it back into the corresponding PDF file.
XFDF: This is an XML version of the forms data format (FDF). It is the simplified version of PDF for representing forms data and annotations. Its document structure is simpler than that of PDF. The file size is much smaller in terms of KBs because they contain just the form’s data and annotations, not all the PDF content.
Refer to the following GIF image.
Export annotations from a PDF
Exporting annotations will only extract the annotation details from the PDF files, leaving all other PDF content and document structural information behind. The exported data is simple and you can save it either in a file or a stream. We can also easily transfer it between documents and applications (PDF readers) as the size of the exported annotations is much smaller than the entire PDF.
As described earlier, you can easily export annotations from a PDF loaded in the WPF PDF Viewer. To do so, pass the file name (name used to save the exported annotations) and the format details as parameters to the ExportAnnotations method of the PDF Viewer.
Refer to the following code example to export annotations from a PDF to an FDF file.
// Export the annotation data to an FDF file in the 'D' folder named "Annotations.fdf". pdfViewer.ExportAnnotations(@"D:\Annotations.fdf", AnnotationDataFormat.Fdf);
Refer to the following code example to export annotations from a PDF to an XFDF file.
// Export the annotation data to an XFDF file in the 'D' folder named "Annotations.xfdf". pdfViewer.ExportAnnotations(@"D:\Annotations.xfdf", AnnotationDataFormat.XFdf);
You can also export the annotations to a Stream instead of saving them to a file. To do so, pass the stream and the required format (FDF or XFDF) as parameters to the ExportAnnotations method of the WPF PDF Viewer. Refer to the following code example.
Stream stream = new MemoryStream(); // Export the annotations to a stream in XFDF format. pdfViewer.ExportAnnotations(stream, AnnotationDataFormat.XFdf);
Import annotations to a PDF
Similar to exporting, you can import annotations either from a file or a stream in the WPF PDF Viewer. On import, the file or stream containing annotation details will be quickly transformed to annotations and placed over the corresponding PDF pages. You can also easily import annotations from various PDF reader applications. That is, an annotation exported using Adobe Acrobat can be imported in Syncfusion WPF PDF Viewer and vice versa.
As described earlier, you can import annotations to a PDF from FDF and XFDF formats. To do so, pass the file name and the required format as parameters to the ImportAnnotations method of the PDF Viewer.
Refer to the following code example to import annotations to a PDF from an FDF file.
// Import the annotations from an FDF file in the 'D' folder named "Annotations.fdf". pdfViewer.ImportAnnotations(@"D:\Annotations.fdf", AnnotationDataFormat.Fdf);
Similarly, refer to the following code example to import annotations to a PDF from an XFDF file.
// Import annotations from an XFDF file in the 'D' folder named "Annotations.xfdf". pdfViewer.ImportAnnotations(@"D:\Annotations.xfdf", AnnotationDataFormat.XFdf);
To import the annotations from a Stream, pass the stream and the required format (FDF or XFDF) as parameters to the ImportAnnotations method of the WPF PDF Viewer. Refer to the following code example, where the stream object is created from a file.
Stream stream = new FileStream(@"D:\Annotations.xfdf", FileMode.Open); // Imports annotations from a stream object. pdfViewer.ImportAnnotations(stream, annotationDataFormat);
Note: To revert the import operation, perform an undo operation using the shortcut key Ctrl + Z or the UndoCommand of the WPF PDF Viewer.
For more information, refer to the following references:
- [Demo project] Export and Import Annotations in WPF PDF Viewer
- [Documentation] Annotations supported by WPF PDF Viewer
- [Documentation] Export and import annotations in WPF PDF Viewer
Thank you for reading this blog. We have seen how to export and import annotations in PDF files using the Syncfusion WPF PDF Viewer. With these features, we can save a lot of time and memory by saving only the annotations instead of saving and transferring an entire PDF file. Try using our PDF Viewer in your app and let us know what you think in the comment section below.