Articles in this section

How to Preserve the Existing Annotation in Flutter SfPdfViewer?

This article explains how to preserve the existing annotations in Syncfusion® Flutter PDF Viewer.

 

In the Syncfusion® Flutter SfPdfViewer widget, only document link annotations are supported. Unsupported annotations will not be displayed in the SfPdfViewer widget. All unsupported annotations must be flattened using the Syncfusion® Flutter PDF package to view them in the SfPdfViewer widget. In the _getPDFBytes() method, we flatten the unsupported annotations (Rectangle Annotation, Ellipse Annotation, Line Annotation, Polygon Annotation, Cloud Annotation, Digital Signature, PDF form field annotation) from the document and save them as bytes, then load those bytes using the SfPdfViewer.memory(). The following code explains the same.

 

Uint8List? documentBytes;
@override
void initState() {
  super.initState();
  _getPDFBytes();
}

void _getPDFBytes() async {
  final ByteData bytes =
      await DefaultAssetBundle.of(context).load('assets/sample.pdf');
  documentBytes = bytes.buffer.asUint8List();
  final PdfDocument document = PdfDocument(inputBytes: documentBytes);
  // Gets all the pages
  for (int i = 0; i < document.pages.count; i++) {
    // Loads the existing PDF page
    PdfPage page = document.pages[i];
    // Gets the annotation collection from the page
    PdfAnnotationCollection collection = page.annotations;
    // Gets all the annotations in the page
    for (int j = 0; j < collection.count; j++) {
      // Gets the annotation from the annotation collection
      PdfAnnotation annotation = collection[j];
      // Ignore the supported DocumentLinkAnnotation from flattening.
      if (annotation.runtimeType != PdfDocumentLinkAnnotation) {
        // Flatten the annotation
        annotation.flatten();
      }
    }
  }

  // Flatten the PDF form field annotations
  document.form.flattenAllFields();

  documentBytes = Uint8List.fromList(document.saveSync());

  setState(() {});
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Syncfusion Flutter PDF Viewer'),
    ),
    body: documentBytes != null
        ? SfPdfViewer.memory(
            documentBytes!,
          )
        : Container(),
  );
}

 

Annotations-SfPdfViewer-Flutter.

 

A complete working sample can be downloaded from here.


Conclusion

I hope you enjoyed learning about how to preserve the existing annotations in Flutter SfPdfViewer.

You can refer to our Flutter SfPdfViewer featuretour page to learn about its other groundbreaking feature representations and documentation, to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied