使用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;
            }
        }
    }
}
相关推荐
聆风吟º23 分钟前
【C标准库】深入理解C语言pow函数:从入门到精通,一文搞定幂运算
c语言·开发语言·库函数·pow·幂运算
Vallelonga40 分钟前
Rust 中 Cargo.toml & Cargo.lock
开发语言·后端·rust
唐青枫1 小时前
终于不用手搓两级缓存了!C#.NET HybridCache 详解:L1 L2、标签失效与防击穿实战
c#·.net
bzmK1DTbd10 小时前
Git版本控制:Java项目中的分支管理与合并策略
java·开发语言·git
Rust研习社10 小时前
为什么 Rust 没有空指针?
开发语言·后端·rust
kyriewen1110 小时前
WebAssembly:前端界的“外挂”,让C++代码在浏览器里跑起来
开发语言·前端·javascript·c++·单元测试·ecmascript
其实防守也摸鱼12 小时前
CTF密码学综合教学指南--第九章
开发语言·网络·python·安全·网络安全·密码学·ctf
砚底藏山河12 小时前
Python量化开发:2026最佳实时股票数据API接口推荐与对比
开发语言·windows·python
AlunYegeer13 小时前
JAVA,以后端的视角理解前端。在全栈的路上迈出第一步。
java·开发语言·前端
hixiong12314 小时前
C# OpenvinoSharp使用DINOv2模型进行图像相似度计算
开发语言·c#