使用VB.NET或C#中的itextsharp dll读取PDF内容

如何使用带有Pdfreader类的itextsharp读取PDF内容。我的PDF可能包含纯文本或文本图像。



慕容708150
浏览 1091回答 3
3回答

月关宝盒

您无法像使用iTextSharp一样阅读和解析PDF的内容。来自iTextSharp的SourceForge教程:您无法使用iText“解析”现有PDF文件,您只能在每页“读取”该页面。这是什么意思?pdf格式只是一个画布,其中放置文本和图形时没有任何结构信息。因此,PDF文件中没有任何“iText-objects”。在每个页面中可能会有许多“字符串”,但您无法使用这些字符串重建短语或段落。可能绘制了许多线条,但您无法根据这些线条检索表格对象。简而言之:使用iText解析PDF文件的内容是不可能的。在新闻组新闻://comp.text.pdf上发布您的问题,也许您会从那些已经构建了可以解析PDF并提取其内容的工具的人那里得到一些答案,但是不要指望能够执行子弹的工具 - 结构化文本的转换。

不负相思意

LGPL / FOSS iTextSharp 4.xvar pdfReader = new PdfReader(path); //other filestream etcbyte[] pageContent = _pdfReader .GetPageContent(pageNum); //not zero basedbyte[] utf8 = Encoding.Convert(Encoding.Default, Encoding.UTF8, pageContent);string textFromPage = Encoding.UTF8.GetString(utf8);其他答案都没有对我有用,它们似乎都针对iTextSharp的AGPL v5。我再也找不到任何参考SimpleTextExtractionStrategy或LocationTextExtractionStrategy在FOSS版本。与此相关的其他可能非常有用的东西:const string PdfTableFormat = @"\(.*\)Tj";Regex PdfTableRegex = new Regex(PdfTableFormat, RegexOptions.Compiled);List<string> ExtractPdfContent(string rawPdfContent){&nbsp; &nbsp; var matches = PdfTableRegex.Matches(rawPdfContent);&nbsp; &nbsp; var list = matches.Cast<Match>()&nbsp; &nbsp; &nbsp; &nbsp; .Select(m => m.Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Substring(1) //remove leading (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Remove(m.Value.Length - 4) //remove trailing )Tj&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Replace(@"\)", ")") //unencode parens&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Replace(@"\(", "(")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Trim()&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; return list;}这将从PDF中提取仅文本数据,如果显示的文本Foo(bar)将在PDF中编码,则(Foo\(bar\))Tj此方法将按Foo(bar)预期返回。此方法将从原始pdf内容中删除许多其他信息,例如位置坐标。
打开App,查看更多内容
随时随地看视频慕课网APP