iTextSharp 根据PDF内容对文档进行分类

近期有一个需求,在众多不规则命名的PDF文档中,将不同人员的文档根据不同文档类型进行归档,并统计对应人员各类文档内容中的指标数值。

每个文档内容中都含有可识别的唯一编号,并可以根据文档内容中的标题截取字符对照到所属的文档类别进行分类归档,每个类别的文档格式是固定的。

功能相对简单,这里决定使用较轻便的 iTextSharp 对PDF进行读取。使用 iTextSharp.text.pdf.parser.ITextExtractionStrategy 来进行读取判断。上代码:

    public class PdfCheckHelper {

        /// <summary>
        /// 获取文件夹下的目标PDF文件,并拷贝到导出路径
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="toFilePath"></param>
        /// <param name="docCode">文档编号</param> 
        /// <param name="docType">文档类别</param> 
        /// <returns></returns>
        public static bool CopyTargetPdfFile(string dirPath, string toFilePath, string docCode, string docType)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
            if (!dirInfo.Exists) return false;
            FileInfo[] files = dirInfo.GetFiles();
            if (files.Length = 0) return false;
            foreach (var file in files.OrderByDescending(x=>x.LastWriteTime))
            {
                if (IsPdfExistKeys(file.FullName, patName))
                {
                    FileInfo fi = new FileInfo(file.FullName);
                    FileStream fileStream = fi.OpenRead(); //尝试读取PDF文件
                    fileStream.Close();
                    fileStream.Dispose();
                    fi.CopyTo(toFilePath, true); //覆盖到导出路径
                    fi.Delete(); //删除当前文件
                    return true;
                }
            }
        }
        
        // <summary>
        /// 判断字符是否在PDF中 
        /// </summary> 
        /// <param name="fileName">PDF文件路径</param> 
        /// <param name="docCode">文档编号</param> 
        /// <param name="docType">文档类别</param> 
        /// <returns></returns> 
        public static bool IsPdfExistKeys(string fileName, string docCode, string docType)
        {
            bool exist = false;
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(fileName);
            iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
            iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy;
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                strategy = parser.ProcessContent<iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy>(i, new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy());
                if (strategy.GetResultantText().IndexOf("编号:" + docCode) != -1)
                {
                    if (strategy.GetResultantText().IndexOf(": " + docType) != -1)
                    {
                        exist = true;
                        break;
                    }
                }
            }
            reader.Close();
            reader = null;
            return exist;
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值