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

            if (texts is null)
            {
                return(new PdfMatches(startPage, endPage, matches));
            }

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

                    foreach (var text in texts)
                    {
                        var handle = NativeMethods.FPDFText_FindStart(pageData.TextPage, FPDFEncoding.GetBytes(text), flags, 0);

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

                                int matchLength = NativeMethods.FPDFText_GetSchCount(handle);

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

                                matches.Add(new PdfMatch(
                                                match,
                                                new PdfTextSpan(page, index, matchLength),
                                                page
                                                ));
                            }
                        }
                        finally
                        {
                            NativeMethods.FPDFText_FindClose(handle);
                        }
                    }
                }
            }

            return(new PdfMatches(startPage, endPage, matches));
        }
Example #2
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))
                {
                    NativeMethods.FPDF_SEARCH_FLAGS flags = 0;
                    if (matchCase)
                    {
                        flags |= NativeMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHCASE;
                    }
                    if (wholeWord)
                    {
                        flags |= NativeMethods.FPDF_SEARCH_FLAGS.FPDF_MATCHWHOLEWORD;
                    }

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

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

                            int matchLength = NativeMethods.FPDFText_GetSchCount(handle);

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

                            double left, right, bottom, top;
                            NativeMethods.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
                    {
                        NativeMethods.FPDFText_FindClose(handle);
                    }
                }
            }

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