コード例 #1
0
ファイル: PdfToolBarSearch.cs プロジェクト: yiqideren/Pdf.Wpf
        private void _searchTimer_Tick(object sender, EventArgs e)
        {
            if (_searchPageIndex < 0)
            {
                return;
            }
            var doc = PdfViewer.Document;
            int cnt = doc.Pages.Count;

            if (_searchPageIndex >= cnt)
            {
                _searchTimer.Stop();
                return;
            }

            IntPtr page = Pdfium.FPDF_LoadPage(doc.Handle, _searchPageIndex);

            if (page == IntPtr.Zero)
            {
                _searchTimer.Stop();
                return;
            }

            IntPtr text = Pdfium.FPDFText_LoadPage(page);

            if (text == IntPtr.Zero)
            {
                _searchTimer.Stop();
                return;
            }

            var sh = Pdfium.FPDFText_FindStart(text, _searchText, _searchFlags, 0);

            if (sh == IntPtr.Zero)
            {
                _searchTimer.Stop();
                return;
            }

            while (Pdfium.FPDFText_FindNext(sh))
            {
                int idx = Pdfium.FPDFText_GetSchResultIndex(sh);
                int len = Pdfium.FPDFText_GetSchCount(sh);
                var ft  = new PdfSearch.FoundText()
                {
                    CharIndex = idx, CharsCount = len, PageIndex = _searchPageIndex
                };
                _foundText.Add(ft);
                _forHighlight.Add(ft);
            }
            Pdfium.FPDFText_FindClose(sh);
            Pdfium.FPDFText_ClosePage(text);
            Pdfium.FPDF_ClosePage(page);
            UpdateResults();
            _searchPageIndex++;
        }