使用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;
            }
        }
    }
}
相关推荐
还梦呦42 分钟前
2025年09月计算机二级Java选择题每日一练——第一期
java·开发语言
与火星的孩子对话1 小时前
Unity高级开发:反射原理深入解析与实践指南 C#
java·unity·c#·游戏引擎·lucene·反射
♞沉寂1 小时前
信号以及共享内存
linux·c语言·开发语言
答题卡上的情书1 小时前
java第一个接口
java·开发语言
Dm_dotnet1 小时前
回顾一下WPF原生实现命令
c#
王廷胡_白嫖帝2 小时前
Qt密码生成器项目开发教程 - 安全可靠的随机密码生成工具
开发语言·qt
程序设计实验室2 小时前
纯 C#实现+AOT 打造的智能PDF目录提取工具 PdfTocExtractor
c#·aot
Kyln.Wu2 小时前
【python实用小脚本-190】Python一键删除PDF任意页:输入页码秒出干净文件——再也不用在线裁剪排队
服务器·python·pdf
hllqkbb3 小时前
从 SGD 到梯度累积:Epoch、Batch、Step 的关系全解析
开发语言·人工智能·opencv·计算机视觉·batch
风吹落叶32573 小时前
深入解析JVM内存管理与垃圾回收机制
java·开发语言·jvm