Setting initial page

Which product are you using?
PDF.js Express Viewer

PDF.js Express Version

Detailed description of issue
Is there a way to set initial page number when opening a document?

Expected behaviour
I want to save last viewed page number and next time when opening the same document, it should start from that page.

Does your issue happen with every document, or just one?
Yes

Link to document
Below is the code of useEffect() hook in one of my Next.js components:

useEffect(() => {
      import("@pdftron/pdfjs-express-viewer").then(() => {
        WebViewer(
          {
            path: "/webviewer/lib",
            licenseKey: process.env.NEXT_PUBLIC_PDFJS_KEY,
          },
          viewer.current
        ).then((inst: any) => {
          instance.current = inst;

          // Set the document
          const blob = new Blob([bookBuffer], {
            type: "application/octet-stream",
          });
          inst.UI.loadDocument(blob);

          // Set the language
          inst.UI.setLanguage(language);

          // Disable elements
          const elements = [
            "downloadButton",
            "printButton",
            "languageButton",
            "coverLayoutButton",
            "contextMenuPopup",
          ];
          if (contentIsScanned) {
            elements.push("searchButton");
          }
          inst.UI.disableElements(elements);

          // Get page number
          const { documentViewer } = inst.Core;
          documentViewer.addEventListener('pageNumberUpdated', (pageNumber: number) => {
            currentPage = pageNumber;
            console.log("PAGE NUMBER", pageNumber)
          });
        });
      });
  }, [bookId]);

Hi there,

Thank you for reaching out to pdf.js express forums,

One way could be to save the page number (i.e. database) as you have in the above code, then use that page number to set the current document page on documentLoad:
https://pdfjs.express/api/Core.DocumentViewer.html#setCurrentPage

Best regards,
Kevin Kim

1 Like

Wow, it worked.
I’ve been calling setCurrentPage() on the inst instead of documentViewer.
Anyways, I appreciate your help, thanks a lot!