Home > Net >  How to load different pdf page on initial load
How to load different pdf page on initial load

Time:07-16

I'm using syncfusion_flutter_pdfviewer package for my flutter project, and I'm trying to pass a specific page number from my Main Page to my Details page. After that it'll load that PDF page number immediacy when the Detail page is open, but I'm stuck, so any help or suggestion would be really appreciated.

I have tried like this, but I'm getting an error on "jumpToPage"

This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.

      body: SfPdfViewer.asset(
        'data/hymn_pdf/full-songs.pdf',
        controller: _pdfViewerController.jumpToPage(widget.number),
      ),

Full Details page Code.

class DisplayScreen extends StatefulWidget {
  final int number;
  const DisplayScreen(this.number);
  @override
  _DisplayScreen createState() => _DisplayScreen();
}

class _DisplayScreen extends State<DisplayScreen> {
  final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();

  late PdfViewerController _pdfViewerController;

  @override
  void initState() {
    _pdfViewerController = PdfViewerController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Syncfusion Flutter PdfViewer'),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.arrow_drop_down_circle,
              color: Colors.white,
            ),
            onPressed: () {
              _pdfViewerController.jumpToPage(widget.number); // I got this from synfusion doc and I'm trying to copy it in my controller but I'm getting an error.
            },
          ),
        ],
      ),
      body: SfPdfViewer.asset(
        'data/hymn_pdf/full-songs.pdf',
        controller: _pdfViewerController.jumpToPage(widget.number), //I'm getting an error here
      ),
    );
  }
}


CodePudding user response:

You cannot just call jumpToPage when assigning the controller. Wrong example:

  body: SfPdfViewer.asset(
    'data/hymn_pdf/full-songs.pdf',
    controller: _pdfViewerController.jumpToPage(widget.number), // <-- nope
  ),

You need to call the method after the pdf is loaded. For example, call it in the onDocumentLoaded callback function.

Example code:

  SfPdfViewer.asset(
    'data/hymn_pdf/full-songs.pdf',
    controller: _pdfViewerController,
    onDocumentLoaded: (details) {   // as soon as the doc is loaded
      _pdfController.jumpToPage(3); // jump to page 3
    },
  )
  • Related