ITextSharp 搜索关键字定位【转】

 

 

#region IText 模块
        private class MyRenderListener : IRenderListener
        {
            public void BeginTextBlock() { }
            public void EndTextBlock() { }
            public void RenderImage(ImageRenderInfo renderInfo) { }

            public void RenderText(TextRenderInfo textRenderInfo)
            {
                string text = textRenderInfo.GetText();
                if (text.Contains(KEY_WORD))
                {
                    RectangleJ boundRectange = textRenderInfo.GetBaseline().GetBoundingRectange();
                    page = cycler-1;
                    x = (int)boundRectange.X;
                    y = (int)boundRectange.Y;
                }
            }
        }

        private ErrorCode GetPositionByIText(string path)
        {
            ErrorCode err = ErrorCode.E_OK;
            try
            {
                PdfReader reader = new PdfReader(path);
                PdfReaderContentParser parser = new PdfReaderContentParser(reader);
                MyRenderListener listener = new MyRenderListener();
                int numPages = reader.NumberOfPages;
                for (cycler = 1; cycler <= numPages; cycler++)
                    parser.ProcessContent(cycler, listener);
                reader.Close();
            }
            catch (System.Exception ex)
            {
                err = ErrorCode.E_EXCEPTION;
                LogCrash("itext.GetPosition()--" + ex.Message);
            }
            return err;
        }
        #endregion IText 模块

  

转:https://www.cnblogs.com/aoun/p/4533266.html

转:https://blog.51cto.com/u_13002884/6236483

https://www.nuget.org/packages/iTextSharp

posted @ 2024-09-12 09:34  不经一番寒彻骨  阅读(42)  评论(0编辑  收藏  举报