Skip to content
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

Public PDFFindController #260

Closed
viktorhajer opened this issue Jan 25, 2018 · 7 comments
Closed

Public PDFFindController #260

viktorhajer opened this issue Jan 25, 2018 · 7 comments
Milestone

Comments

@viktorhajer
Copy link

Bug Report or Feature Request (mark with an x)
- [ ] Regression (a behavior that used to work and stopped working in a new release)
- [ ] Bug report -> please search issues before submitting
- [ x] Feature request
- [ ] Documentation issue or request

It would be great to have public PDFFindController member in the component in order to use the find feature of the PDFjs. I cloned the project and the following code was working fine if the show-all and render-text were true:

public pdfFindController: any;

in the setupViewer:
this.pdfFindController = new (<any>PDFJS).PDFFindController({pdfViewer: this.pdfViewer}); this.pdfViewer.setFindController(this.pdfFindController);

In my test project:
this.pdfComponent.pdfFindController.executeCommand('find', { caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true, query: text });

Thanks in advance!

@nlundby
Copy link

nlundby commented Jan 30, 2018

Thanks! I've been trying to figure this one out - a great start!

@VadimDez
Copy link
Owner

VadimDez commented Feb 6, 2018

Relates to #115

@viktorhajer
Copy link
Author

Just a note:
I created my own implementation of the module because

  • the search and outline function was required and
  • and I had to use the show-all=false, because the length of the PDF was ~16.000 pages and the show-all=true wasn't effective (memory/CPU usage and also time-consuming)

For this I had to use the "newly" introduced PDFSinglePageViewer instead of the PDFViewer and it solved my problem also the search and outline function was working properly.

My init function (after that the I can load the PDF)

  private initPDFJS() {
    const container = this.getContainer(); // get html container
    (<any>PDFJS).disableTextLayer = false;
    this.pdfLinkService = new (<any>PDFJS).PDFLinkService();

    this.pdfViewer = new (<any>PDFJS).PDFSinglePageViewer({
      container: container,
      removePageBorders: false,
      linkService: this.pdfLinkService,
    });
    this.pdfLinkService.setViewer(this.pdfViewer);

    this.pdfFindController = new (<any>PDFJS).PDFFindController({
      pdfViewer: this.pdfViewer
    });
    this.pdfViewer.setFindController(this.pdfFindController);

    container.addEventListener('pagesinit', () => {
      this.pdfViewer.currentScaleValue = 'page-fit';
    });
  }

My simplified search functions:

  public search(text: string, caseSensitive: boolean = false) {
      const searchText = text ? text.trim() : '';
      if (!searchText) {
        return;
      }      
      this.pdfFindController.onUpdateResultsCount = this.onUpdateResultsCount.bind(this);
      this.pdfFindController.onUpdateState = this.onUpdateState.bind(this);
      this.pdfFindController.executeCommand('find', {
        caseSensitive: caseSensitive,
        findPrevious: false,
        highlightAll: true,
        phraseSearch: true,
        query: searchText
      });
    }
  public nextMatch() {
    if (this.pdfFindController.matchCount > 1) {
      this.pdfFindController.nextMatch();
      this.currentPage = this.pdfViewer._currentPageNumber;
    }
  }

I hope it may helpful.

@Demokumar
Copy link

Demokumar commented Feb 7, 2018

I try this got error:
Uncaught (in promise): TypeError: PDFJS.PDFSinglePageViewer is not a constructor

@viktorhajer
Copy link
Author

viktorhajer commented Feb 7, 2018

You may have to update the version of the PDFJS in the package.json, that interface was introduced in the following version:
"pdfjs-dist": "1.10.88",

(Releases: https://github.com/mozilla/pdf.js/releases)

VadimDez added a commit that referenced this issue Feb 13, 2018
@VadimDez VadimDez added this to the 4.1.1 milestone Feb 13, 2018
VadimDez added a commit that referenced this issue Feb 13, 2018
@VadimDez
Copy link
Owner

Made PDFFindController public in 4.1.1
https://github.com/VadimDez/ng2-pdf-viewer/blob/master/CHANGELOG.md#411

@androidovshchik
Copy link

@viktorhajer
cool library https://github.com/viktorhajer/simple-pdf-viewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants