原創(chuàng)|使用教程|編輯:王香|2018-01-11 11:31:45.000|閱讀 1971 次
概述:Spire.PDF 是一個(gè)專(zhuān)業(yè)的PDF組件,能夠獨(dú)立地創(chuàng)建、編寫(xiě)、編輯、操作和閱讀PDF文件,支持 .NET、WPF和Silverlight三個(gè)版本,本文介紹了如何通過(guò)Spire.PDF使用C#從PDF中的特定矩形區(qū)域中提取文本。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
使用Spire.PDF,程序員可以從PDF文檔中的特定矩形區(qū)域提取文本,本文演示如何使用Spire.PDF和C#實(shí)現(xiàn)此功能。
示例文件:
詳細(xì)步驟:
Step 1: 初始化PdfDocument類(lèi)的對(duì)象并加載PDF文件。
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Stories.pdf");
Step 2: 獲取第一頁(yè)。
PdfPageBase page = pdf.Pages[0];
Step 3: 從頁(yè)面中的特定矩形區(qū)域中提取文本,之后將文本保存為.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());
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn