Skip to content

How to save text markup annotations in the Flutter PDF Viewer (SfPdfViewer) so that they appear again when reopened. #2332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
minhto2811 opened this issue Apr 11, 2025 · 5 comments
Labels
pdf viewer PDF viewer component

Comments

@minhto2811
Copy link

Use case

In the Flutter PDF Viewer (SfPdfViewer), users can add text markup annotations (such as highlights, underlines, and strikethroughs) to a document. To ensure these annotations persist after the app is closed and reopened, it is necessary to save the annotations data separately. Later, when the document is loaded again, the saved annotations must be re-imported into the viewer so they reappear as before.

Proposal

Support saving and restoring text markup annotations in the Flutter PDF Viewer (SfPdfViewer), allowing users to view previously added annotations when reopening the document.

@VijayakumarMariappan VijayakumarMariappan added pdf viewer PDF viewer component open Open labels Apr 14, 2025
@immankumarsync
Copy link
Contributor

Hi @minhto2811,

We can save the annotation made in the PDF document using the PdfViewerController.saveDocument method.

You can use the bytes obtained from this method to replace the document from the original location.
Please try with the attached sample to achieve your requirement.

sfpdfviewer_gh2332.zip

@Deepak1799 Deepak1799 added waiting for customer response Cannot make further progress until the customer responds. and removed open Open labels Apr 15, 2025
@minhto2811
Copy link
Author

minhto2811 commented Apr 16, 2025

Image

I followed your instructions, but the saveDocument() function returns an empty array.

@Deepak1799 Deepak1799 removed the waiting for customer response Cannot make further progress until the customer responds. label Apr 22, 2025
@minhto2811 minhto2811 reopened this Apr 24, 2025
@minhto2811
Copy link
Author

Image

I followed your instructions, but the saveDocument() function returns an empty array.

@immankumarsync
Copy link
Contributor

Hi @minhto2811,
We suspect that the PdfViewerController is not attached to the SfPdfViewer widget. Could you please check add the PdfViewerController property referring the below code snippet?

Image

@minhto2811
Copy link
Author

minhto2811 commented Apr 25, 2025

Update: I just tried again and it works. As you said, it wasn't attached to the view. Sorry for the inconvenience, and sincerely thank you for your support.


I'm certain that the PdfViewerController is attached to the SfPdfViewer widget — the debug values confirm that. Here's the relevant part of my code:

class PdfView extends StatelessWidget {
  const PdfView(
      {super.key,
      required this.path,
      required this.initialPageNumber,
      this.onPageChanged,
      required this.key1,
      this.onHyperlinkClicked,
      required this.controller});

  final String path;
  final int initialPageNumber;
  final void Function(PdfPageChangedDetails)? onPageChanged;
  final GlobalKey<SfPdfViewerState> key1;
  final Function(PdfHyperlinkClickedDetails)? onHyperlinkClicked;
  final PdfViewerController controller;



  @override
  Widget build(BuildContext context) {
    return SfPdfViewer.file(
      key: key1,
      File(path),
      initialPageNumber: initialPageNumber,
      onPageChanged: onPageChanged,
      onHyperlinkClicked: onHyperlinkClicked,
      controller: controller,
      canShowPageLoadingIndicator: false,
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pdf viewer PDF viewer component
Projects
None yet
Development

No branches or pull requests

4 participants