espresso3389 / pdfrx

pdfrx is yet another PDF viewer implementation that built on the top of pdfium. The plugin currently supports Android, iOS, Windows, macOS, Linux, and Web.
MIT License
60 stars 36 forks source link

Can pdfrx be displayed as a single page by default? #104

Open zengchanghuan opened 2 months ago

zengchanghuan commented 2 months ago

Because the number of pages in the PDF file may be large, I want to display only 1 page at a time, how do I set it?

espresso3389 commented 2 months ago

You can do that by using PdfViewerParams.layoutPages.

By the way,

Because the number of pages in the PDF file may be large, ...

It does not reduce the total memory consumption by restricting the viewer to show only one page. It at most caches only three pages. And, furthermore, all the PDF data is anyway loaded on the memory.

For the cache control, PdfViewerParams.maxImageBytesCachedOnMemory may help you.

zengchanghuan commented 2 months ago
            PdfViewer.asset(
              'assets/sample.pdf',
              passwordProvider: () => passwordDialog(context),
              controller: controller,
              params: PdfViewerParams(
                maxImageBytesCachedOnMemory: 50 * 1024 * 1024,
                enableTextSelection: true,
                maxScale: 8,
                // facing pages algoritm
                layoutPages: (pages, params) {
                  // They should be moved outside function
                  const isRightToLeftReadingOrder = false;
                  const needCoverPage = true;
                  final width = pages.fold(
                      0.0, (prev, page) => max(prev, page.width));

                  final pageLayouts = <Rect>[];
                  double y = params.margin;
                  for (int i = 0; i < pages.length; i++) {
                    const offset = needCoverPage ? 1 : 0;
                    final page = pages[i];
                    final pos = i + offset;
                    final isLeft = isRightToLeftReadingOrder
                        ? (pos & 1) == 1
                        : (pos & 1) == 0;

                    final otherSide = (pos ^ 1) - offset;
                    final h = 0 <= otherSide && otherSide < pages.length
                        ? max(page.height, pages[otherSide].height)
                        : page.height;

                    pageLayouts.add(
                      Rect.fromLTWH(
                        isLeft
                            ? width + params.margin - page.width
                            : params.margin * 2 + width,
                        y + (h - page.height) / 2,
                        page.width,
                        page.height,
                      ),
                    );
                    if (pos & 1 == 1 || i + 1 == pages.length) {
                      y += h + params.margin;
                    }
                  }
                  return PdfPageLayout(
                    pageLayouts: pageLayouts,
                    documentSize: Size(
                      (params.margin + width) * 2 + params.margin,
                      y,
                    ),
                  );
                },
                //
                // Scroll-thumbs example
                //
                viewerOverlayBuilder: (context, size) => [
                  // Show vertical scroll thumb on the right; it has page number on it
                  PdfViewerScrollThumb(
                    controller: controller,
                    orientation: ScrollbarOrientation.right,
                    thumbSize: const Size(40, 25),
                    thumbBuilder:
                        (context, thumbSize, pageNumber, controller) =>
                            Container(
                      color: Colors.black,
                      child: Center(
                        child: Text(
                          pageNumber.toString(),
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                  // Just a simple horizontal scroll thumb on the bottom
                  PdfViewerScrollThumb(
                    controller: controller,
                    orientation: ScrollbarOrientation.bottom,
                    thumbSize: const Size(80, 30),
                    thumbBuilder:
                        (context, thumbSize, pageNumber, controller) =>
                            Container(
                      color: Colors.red,
                    ),
                  ),
                ],
                //
                // Loading progress indicator example
                //
                loadingBannerBuilder:
                    (context, bytesDownloaded, totalBytes) => Center(
                  child: CircularProgressIndicator(
                    value: totalBytes != null
                        ? bytesDownloaded / totalBytes
                        : null,
                    backgroundColor: Colors.grey,
                  ),
                ),
                //
                // Link handling example
                //
                // FIXME: a link with several areas (link that contains line-break) does not correctly
                // show the hover status
                linkWidgetBuilder: (context, link, size) => Material(
                  color: Colors.blue.withOpacity(0.2),
                  child: InkWell(
                    onTap: () async {
                      if (link.url != null) {
                        navigateToUrl(link.url!);
                      } else if (link.dest != null) {
                        controller.goToDest(link.dest);
                      }
                    },
                    hoverColor: Colors.blue.withOpacity(0.2),
                  ),
                ),
                pagePaintCallbacks: [
                  textSearcher.pageTextMatchPaintCallback,
                  _paintMarkers,
                ],
                onDocumentChanged: (document) async {
                  if (document == null) {
                    documentRef.value = null;
                    outline.value = null;
                    _selectedText = null;
                    _markers.clear();
                  }
                },
                onViewerReady: (document, controller) async {
                  documentRef.value = controller.documentRef;
                  outline.value = await document.loadOutline();
                },
                onTextSelectionChange: (selection) {
                  _selectedText = selection;
                },
              ),

Because the file size is too large and causes carsh, I want to display only 1 page at a time, and then slide to turn the page. But the above code doesn't do the trick, please how do I need to do it, thanks! The sample.pdf is :https://files.acadsoc.com.cn/uploads/subdocument/official/20210202/b70a167f-66e3-4f47-a562-5696b563b925.pdf

bailyzheng commented 3 weeks ago

I need this feature too. It should auto play page by page on some display devices that not for interative.