原創|使用教程|編輯:王香|2018-01-11 11:31:45.000|閱讀 1971 次
概述:Spire.PDF 是一個專業的PDF組件,能夠獨立地創建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、WPF和Silverlight三個版本,本文介紹了如何通過Spire.PDF使用C#從PDF中的特定矩形區域中提取文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
使用Spire.PDF,程序員可以從PDF文檔中的特定矩形區域提取文本,本文演示如何使用Spire.PDF和C#實現此功能。
示例文件:
詳細步驟:
Step 1: 初始化PdfDocument類的對象并加載PDF文件。
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Stories.pdf");
Step 2: 獲取第一頁。
PdfPageBase page = pdf.Pages[0];
Step 3: 從頁面中的特定矩形區域中提取文本,之后將文本保存為.txt文件。
string text = page.ExtractText(new RectangleF(50, 50, 500, 100) ); StringBuilder sb = new StringBuilder(); sb.AppendLine(text); File.WriteAllText("Extract.txt", sb.ToString());
輸出:
完整代碼:
//Initialize an object of PdfDocument class PdfDocument pdf = new PdfDocument(); //Load the PDF file pdf.LoadFromFile("Stories.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; // Extract text from a specific rectangular area within the page string text = page.ExtractText(new RectangleF(50, 50, 500, 100) ); //Save the text to a .txt file StringBuilder sb = new StringBuilder(); sb.AppendLine(text); File.WriteAllText("Extract.txt", sb.ToString());
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn