Skip to content

PDF viewer does not update current page index when changing pages #2349

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

Open
minhto2811 opened this issue May 8, 2025 · 2 comments
Open
Labels
pdf viewer PDF viewer component waiting for customer response Cannot make further progress until the customer responds.

Comments

@minhto2811
Copy link

Bug description

When navigating between pages in the PDF viewer, the currentPage index does not update accordingly. As a result, the app continues to show the old page index even after the user has moved to a different page.

Steps to reproduce

1.Install and set up syncfusion_flutter_pdfviewer in your app.
2.Open any multi-page PDF using the PDF viewer widget.
3.Swipe or navigate to another page in the PDF viewer.
4.Observe that the currentPage index does not update correctly.

Code sample

Code sample
SfPdfViewer.memory(
      bytes,
      key: key1,
      initialPageNumber: initialPageNumber,
      onPageChanged: onPageChanged,
      onHyperlinkClicked: onHyperlinkClicked,
      controller: controller,
      canShowPageLoadingIndicator: false,
      canShowTextSelectionMenu: false,
    );

Screenshots or Video

Screenshots / Video demonstration

video

Stack Traces

Stack Traces
[Add the Stack Traces here]

On which target platforms have you observed this bug?

iOS, Android

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.3775], locale en-US)
[√] Windows Version (Windows 11 or higher, 24H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2024.3)
[√] VS Code (version 1.99.3)
[√] Connected device (4 available)
[√] Network resources
@LavanyaGowtham2021 LavanyaGowtham2021 added pdf viewer PDF viewer component open Open labels May 9, 2025
@minhto2811
Copy link
Author

Incorrect sticky note position due to PDF page index not updating

final _controller = PdfViewerController();
final pageNumber = _controller.pageNumber;
final position = Offset(selectedLines.first.bounds.topLeft.dx - 24, selectedLines.first.bounds.topLeft.dy);
annotation = StickyNoteAnnotation(
    pageNumber: pageNumber, 
    text: ' ',
    position: position,
    icon: PdfStickyNoteIcon.help);
_controller.addAnnotation(annotation);

@immankumarsync
Copy link
Contributor

Hi @minhto2811,
The pageNumber API of the PdfViewerController class is updated only when the top of the page reaches the top of the viewport. Please find the below image for reference. This is the current behavior of the SfPdfViewer widget.

Image

Screen.Recording.2025-05-09.185040.mp4

For your case of adding Sticky note annotation at the start of the selected text you can use the pageNumber property of the PdfTextLine class.

Please find the code snippet below to achieve your requirement.

class PDFViewerPage extends StatefulWidget {
  const PDFViewerPage({super.key});

  @override
  PDFViewerPageState createState() => PDFViewerPageState();
}

class PDFViewerPageState extends State<PDFViewerPage> {
  final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
  final PdfViewerController _pdfViewerController = PdfViewerController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter PDF Viewer'),
        actions: <Widget>[
          IconButton(icon: const Icon(Icons.help), onPressed: _help),
        ],
      ),
      body: SfPdfViewer.asset(
        'assets/flutter-succinctly.pdf',
        key: _pdfViewerKey,
        controller: _pdfViewerController,
      ),
    );
  }

  void _help() {
    List<PdfTextLine>? selectedLines =
        _pdfViewerKey.currentState?.getSelectedTextLines();

    if (selectedLines == null || selectedLines.isEmpty) {
      return;
    }

    int pageNumber = selectedLines.first.pageNumber;

    final position = Offset(
      selectedLines.first.bounds.topLeft.dx - 24,
      selectedLines.first.bounds.topLeft.dy,
    );
    Annotation annotation = StickyNoteAnnotation(
      pageNumber: pageNumber,
      text: ' ',
      position: position,
      icon: PdfStickyNoteIcon.help,
    );
    _pdfViewerController.addAnnotation(annotation);
  }
}

Please find the video reference below.

Screen.Recording.2025-05-20.155029.mp4

Kindly confirm us whether the above solution works for your case.

Regards,
Imman Kumar P

@Deepak1799 Deepak1799 added waiting for customer response Cannot make further progress until the customer responds. and removed open Open labels May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pdf viewer PDF viewer component waiting for customer response Cannot make further progress until the customer responds.
Projects
None yet
Development

No branches or pull requests

4 participants