使用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;
            }
        }
    }
}
相关推荐
亚空间仓鼠7 分钟前
Python学习日志(四):实例
开发语言·python·学习
Fanfanaas9 分钟前
Linux 系统编程 进程篇 (二)
linux·运维·服务器·c语言·开发语言·学习
油丶酸萝卜别吃13 分钟前
高效处理数组差异:JS中新增、删除、交集的最优解(Set实现)
开发语言·前端·javascript
HoneyMoose14 分钟前
Npmp 安装时候提示警告: error (ERR_INVALID_THIS)
开发语言
gskyi15 分钟前
时间格式化神器:智能显示相对时间
开发语言·javascript·ecmascript
其实秋天的枫18 分钟前
【考研408】考研计算机408统考历年真题及答案解析PDF电子版(2009-2026年)
经验分享·pdf
古城小栈21 分钟前
Rust在当下AI领域的用武之地:从底层加速到上层应用全解析
开发语言·人工智能·rust
lsx20240621 分钟前
SQLite Where 子句详解
开发语言
专注VB编程开发20年24 分钟前
VBA/VB6 ADO数据库查询jet+只读更快
开发语言·数据库·ado·vb
jiayong2325 分钟前
第 11 课:把筛选条件同步到 URL
开发语言·前端·javascript