使用C#實(shí)現(xiàn)讀取PDF中所有文本內(nèi)容
先安裝如下包
實(shí)現(xiàn)代碼
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("出錯(cuò)文件:" + 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文件錯(cuò)誤", ex); return null; } } } }
到此這篇關(guān)于使用C#實(shí)現(xiàn)讀取PDF中所有文本內(nèi)容的文章就介紹到這了,更多相關(guān)C#讀取PDF內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)格式化SQL語(yǔ)句的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)格式化SQL語(yǔ)句的功能,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08Unity幸運(yùn)轉(zhuǎn)盤(pán)實(shí)戰(zhàn)項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Unity幸運(yùn)轉(zhuǎn)盤(pán)實(shí)戰(zhàn)項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04解析XPath語(yǔ)法之在C#中使用XPath的示例詳解
本篇文章是對(duì)在C#中使用XPath的示例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中is,as,using關(guān)鍵字的使用說(shuō)明
這篇文章主要介紹了C#中is,as,using關(guān)鍵字的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12深入c# 類(lèi)和結(jié)構(gòu)的區(qū)別總結(jié)詳解
本篇文章是對(duì)c#中類(lèi)和結(jié)構(gòu)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05