使用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;
            }
        }
    }
}
相关推荐
笨笨马甲24 分钟前
Qt Quick模块功能及架构
开发语言·qt
夜晚回家39 分钟前
「Java基本语法」代码格式与注释规范
java·开发语言
YYDS31443 分钟前
C++动态规划-01背包
开发语言·c++·动态规划
安木夕1 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
前端页面仔1 小时前
易语言是什么?易语言能做什么?
开发语言·安全
树叶@1 小时前
Python数据分析7
开发语言·python
wydaicls1 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
Biomamba生信基地2 小时前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹2 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
奇树谦2 小时前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt