在本教程中,您将学习如何在没有互操作的情况下使用 C# 将 PDF 转换为 Word。 我们将使用简单的控制台应用程序使用 C# 从 PDF 生成 DOCX。 通过使用相同的方法,您也可以从 PDF 导出 FlatOpc。 从 PDF 到 Word 文件格式的转换只需两行代码,可用于桌面和 Web 应用程序。
在 C# 中将 PDF 转换为 DOCX 的步骤
- 从 NuGet.org 安装 Aspose.Words for .NET 包
- 添加对 Aspose.Words 和 Aspose.Words.Saving 命名空间的引用
- 在导入 PDF 之前使用 License.SetLicense 方法设置许可证
- 将输入的 PDF 文档导入 Aspose.Words 的 DOM
- 将文档保存为 Word DOCX 文件格式
在上一个操作方法主题中,我们研究了 如何在 C# 中将 Word 文档转换为图像。 本主题介绍了在 C# 中从 PDF 保存 DOCX 的所有步骤。
在 C# 中将 PDF 转换为 DOCX 的代码
using Aspose.Words; | |
using System; | |
namespace PdftoWord | |
{ | |
class how_to_convert_pdf_to_word_in_c_sharp_without_interop | |
{ | |
public static void ConvertPDFtoWord(String pdftoword_directory) | |
{ | |
//Set Aspose license before PDF to Word conversion | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(pdftoword_directory + @"Aspose.Words.lic"); | |
//Import the PDF into Aspose.Words DOM. | |
Document doc = new Document(pdftoword_directory + "input.pdf"); | |
//Save PDF to Word document | |
doc.Save(pdftoword_directory + @"output.docx", SaveFormat.Docx); | |
} | |
} | |
} |
您还可以将 PDF 转换为其他文件格式,例如 WordML、DOC、RTF 等。要将 PDF 保存为其他文件格式,您需要在 Document.Save 方法中更改输出文件扩展名和 SaveFormat。