本基本教程指导您如何使用 C#** 将 PDF 转换为文本文件,包括配置设置和可运行的代码片段。它演示了如何通过几个 API 调用来创建使用 C# PDF to Text 转换器。您只需要加载源 PDF 文档并保存输出 Text 文件。
使用 C# 将 PDF 转换为文本文件的步骤
- 在您的应用程序中添加对 Aspose.PDF for .NET 的引用,以将 PDF 转换为文本文件
- 使用 Document 类实例加载源 PDF 文件以创建文本文件
- 创建 TextAbsorber 类的实例并从所有页面中提取文本
- 保存输出文本文件
为了在 .NET 框架中编写基于 PDF 到文本转换器 C# 的应用程序配置,此处提供了分步过程。第一步,快速配置 API 并加载输入的 PDF 文件。接下来,我们继续从其所有页面中提取文本,并根据要求将提取的文本写入文件或流。
使用 C# 将 PDF 转换为文本的代码片段
using System.IO; | |
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
namespace ConvertPdfToTextUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Instantiate the license to avoid evaluation limitations while converting a PDF to Text | |
License PdfToTextLicense = new License(); | |
PdfToTextLicense.SetLicense("Aspose.pdf.lic"); | |
// Open document | |
Document pdfDocument = new Document("PDFtoText.pdf"); | |
// Instantiate a TextAbsorber class object for extracting the text | |
TextAbsorber textAbsorber = new TextAbsorber(new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure),new TextSearchOptions(new Rectangle(5,5,50,50))); | |
// Call the Accept() function to parse all the pages for reading text | |
pdfDocument.Pages.Accept(textAbsorber); | |
// Get extracted text as string | |
string ExtractedText = textAbsorber.Text; | |
// Save the text file | |
File.WriteAllText("PDFtoText.txt" , ExtractedText); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
使用 *C# 将 PDF 转换为文本 * 功能可以集成到您的应用程序中,同时控制从源 PDF 读取文本,就像您可以从所有页面或指定页面读取文本一样。同样,如果您想从 PDF 页面上的特定矩形区域读取文本,您也可以自由定义该矩形区域。还可以定义不同的模式来将 PDF 转换为纯文本、原始文本和 MemorySaving 等文本。
在本文中,我们了解了如何在您的 .NET 应用程序中使用 PDF to Text C# 代码。但是,如果您想探索 PDF 到 HTML 文档的转换,请参阅 如何使用 C# 将 PDF 转换为 HTML 上的文章。