itextsharp根据文字获取文字定位坐标

发布时间2018/8/15 14:48:48

下载次数 0 次 访问量915 次

免开发-应用中心上线啦,专注提供优质插件源码和产品应用!

    public enum ErrorCode
    {
        
            E_OK,
        E_EXCEPTION

    }
    public class MyRenderListener : IRenderListener
    {
        public void BeginTextBlock() { }
        public void EndTextBlock() { }
        public void RenderImage(ImageRenderInfo renderInfo) { }

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



class Program
    {

        public static 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 (int cycler = 1; cycler <= numPages; cycler++)
                {
                    parser.ProcessContent(cycler, listener);
                }
                reader.Close();
            }
            catch (System.Exception ex)
            {
                err = ErrorCode.E_EXCEPTION;
               
            }
            return err;
        }

}

网友评论

发表评论

欢迎评论哦!