用C#写一个读取pdf文档内容的库

安装这两个库,第二个库一定要安装否则有些pdf文件读取会出现异常

读取

csharp 复制代码
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;

namespace TestReadPdf
{
    public static class PdfHelper
    {
        public static IEnumerable<string> ExtractText(string filename)
        {
            using (var r = new PdfReader(filename))
            using (var doc = new PdfDocument(r))
            {
                for (int i = 1; i < doc.GetNumberOfPages(); i++)
                {
                    ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
                    string text = PdfTextExtractor.GetTextFromPage(doc.GetPage(i), strategy);
                    yield return text;
                }
            }
        }
    }
}
相关推荐
专注VB编程开发20年7 小时前
c#语法和java相差多少
java·开发语言·microsoft·c#
有一个好名字7 小时前
设计模式-单例模式
java·单例模式·设计模式
2301_797312267 小时前
学习Java26天
java·开发语言
cike_y7 小时前
JSP原理详解
java·开发语言·jsp
invicinble7 小时前
关于springboot引入traceid来保障可观测型
java·spring boot·后端
SmoothSailingT7 小时前
C#——Lazy<T>懒加载机制
开发语言·单例模式·c#·懒加载
精神病不行计算机不上班7 小时前
[Java Web]在IDEA中完整实现Servlet的示例
java·servlet·tomcat·html·intellij-idea·web
chushiyunen7 小时前
javadoc规范、idea生成javadoc等
java·ide
小羊学伽瓦7 小时前
ThreadLocal
java·jvm·算法
czhc11400756637 小时前
c# 1216
windows·microsoft·c#