Beispiel #1
0
        public PdfMatches Search(string text, bool matchCase, bool wholeWord, int startPage, int endPage)
        {
            var matches = new List <PdfMatch>();

            for (int page = startPage; page <= endPage; page++)
            {
                using (var pageData = new PageData(_document, _form, page))
                {
                    NativePdfiumMethods.FPDF_SEARCH_FLAGS flags = 0;
                    if (matchCase)
                    {
                        flags |= NativePdfiumMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHCASE;
                    }
                    if (wholeWord)
                    {
                        flags |= NativePdfiumMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHWHOLEWORD;
                    }

                    var handle = NativePdfiumMethods.FPDFText_FindStart(pageData.TextPage, FPDFEncoding.GetBytes(text), flags, 0);

                    try
                    {
                        while (NativePdfiumMethods.FPDFText_FindNext(handle))
                        {
                            int index = NativePdfiumMethods.FPDFText_GetSchResultIndex(handle);

                            int matchLength = NativePdfiumMethods.FPDFText_GetSchCount(handle);

                            var result = new byte[(matchLength + 1) * 2];
                            NativePdfiumMethods.FPDFText_GetText(pageData.TextPage, index, matchLength, result);
                            string match = FPDFEncoding.GetString(result, 0, matchLength * 2);

                            double left, right, bottom, top;
                            NativePdfiumMethods.FPDFText_GetCharBox(pageData.TextPage, index, out left, out right, out bottom, out top);

                            matches.Add(new PdfMatch(
                                            new PointF((float)left, (float)top),
                                            match,
                                            page
                                            ));
                        }
                    }
                    finally
                    {
                        NativePdfiumMethods.FPDFText_FindClose(handle);
                    }
                }
            }

            return(new PdfMatches(startPage, endPage, matches));
        }