使用Spire.PDF,程序员能够从PDF文档中的特定矩形区域提取文本,本文演示如何使用Spire.PDF和C#实现此功能。程序员
示例文件:ui
详细步骤:对象
Step 1: 初始化PdfDocument类的对象并加载PDF文件。blog
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Stories.pdf");
Step 2: 获取第一页。图片
PdfPageBase page = pdf.Pages[0];
Step 3: 从页面中的特定矩形区域中提取文本,以后将文本保存为.txt文件。ci
string text = page.ExtractText(new RectangleF(50, 50, 500, 100) ); StringBuilder sb = new StringBuilder(); sb.AppendLine(text); File.WriteAllText("Extract.txt", sb.ToString());
输出:文档
完整代码:get
//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());