Flutter: How to open PDF document with specific page range

别等时光非礼了梦想. 提交于 2021-02-10 23:15:23

问题


I want to open a PDF document with specific page ranges. Like if a PDF have 10 pages. then i want to open from Page 3 to Page 7.

I have tried multiple packages which are available online. which are not providing this functionality.

For example:

  • flutter_full_pdf_viewer 1.0.6

  • flutter_plugin_pdf_viewer 1.0.7 - This provides the options, but this has a lot of dependency issues, Therefore i don't want to use this.

  • pdf_viewer_plugin 1.0.0+2

Therefore please recommend me some library, or if somebody has some code related to this, please provide me that. Or if there is any other best approach to meet the given requirement, then suggest me also.


回答1:


This plugin helps me to open the pdf with the specific page number.

Step 1: Load the document

Future<PDFDocument> _getDocument() async {
    if (await new File('../path_of_file').exists()) {
      file = 1; // exist
      return PDFDocument.openFile('../path_of_file');
    } else {
      setState(() {
        showAppBar = true;
        file = 0; // Does not exist
      });
    }
  }

Step 2 : Initialize with the specific page number

@override
  void initState() {
    super.initState();
    showAppBar = false;
    pageController = PageController(
      initialPage: 1,  //page number in the initializer
    );
  }

Step 3 : Access in the build widget with the following

return FutureBuilder<PDFDocument>(
                        future: _getDocument(),
                        builder: (_, snapshot) {
                          if (snapshot.hasData) {
                            return PDFView(
                              controller: pageController,
                              document: snapshot.data,
                            );
                          }

                          if (snapshot.hasError) {
                            return Center(
                              child: Text(
                                'PDF Rendering does not '
                                'support on the system of this version',
                              ),
                            );
                          }

                          return Center(child: CircularProgressIndicator());
                        },
                      );


来源:https://stackoverflow.com/questions/58390033/flutter-how-to-open-pdf-document-with-specific-page-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!