使用C#读取PDF中所有文本内容

先安装如下包

csharp 复制代码
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ReadPdfText
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "0017_审判流程管理信息表2.pdf";
            var text = ReadPFD2(path);
            Console.WriteLine(text);
            Console.ReadKey();
        }

        public static string OnCreated(string filepath)
        {
            try
            {
                string pdffilename = filepath;
                PdfReader pdfReader = new PdfReader(pdffilename);
                int numberOfPages = pdfReader.NumberOfPages;
                string text = string.Empty;

                for (int i = 1; i <= numberOfPages; ++i)
                {
                    iTextSharp.text.pdf.parser.ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                    text += iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i, strategy);
                }
                pdfReader.Close();

                return text;
            }
            catch (Exception ex)
            {
                throw ex;
                //StreamWriter wlog = File.AppendText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\mylog.log");
                //wlog.WriteLine("出错文件:" + ex.FullPath + "原因:" + ex.ToString());
                //wlog.Flush();
                //wlog.Close(); return null;
            }
        }

        public static string ReadPFD2(string path)
        {
            // string path = path;// @"D:\ydfile\d4bab8ff-26ff-4ddf-a602-872f6988db86_.pdf";
            string text = string.Empty;
            try
            {
                string pdffilename = path;
                StringBuilder buffer = new StringBuilder();
                //Create a pdf document.
                using (Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument())
                {
                    // Load the PDF Document
                    doc.LoadFromFile(pdffilename);
                    // String for hold the extracted text

                    foreach (Spire.Pdf.PdfPageBase page in doc.Pages)
                    {
                        buffer.Append(page.ExtractText());
                    }
                    doc.Close();
                }
                //save text
                text = buffer.ToString();
                return text;
            }
            catch (Exception ex)
            {
                //DHC.EAS.Common.LogInfo.Debug("读取PDF文件返回=" + text);
                //DHC.EAS.Common.LogInfo.Debug("读取PDF文件错误", ex);
                return null;
            }
        }
    }
}
相关推荐
Bio Coder几秒前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z1 分钟前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
企鹅侠客2 小时前
开源免费文档翻译工具 可支持pdf、word、excel、ppt
人工智能·pdf·word·excel·自动翻译
life_time_2 小时前
C语言(22)
c语言·开发语言
Minner-Scrapy2 小时前
DApp 开发入门指南
开发语言·python·web app
孤雪心殇3 小时前
简单易懂,解析Go语言中的Map
开发语言·数据结构·后端·golang·go
庸俗今天不摸鱼3 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
菠菠萝宝3 小时前
【Java八股文】10-数据结构与算法面试篇
java·开发语言·面试·红黑树·跳表·排序·lru
奔跑吧邓邓子3 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程