在 .NET 中将 PDF 转换为 Microsoft Word 文档

概述

本文解释了如何 使用 C# 将 PDF 转换为 Microsoft Word 文档。它涵盖了以下主题。

格式: DOC

格式: DOCX

格式: Word

以下代码片段也适用于 Aspose.PDF.Drawing 库。

PDF 转 DOC 和 DOCX 转换

最受欢迎的功能之一是 PDF 转 Microsoft Word DOC 转换,这使内容管理变得更加轻松。Aspose.PDF for .NET 允许您快速高效地将 PDF 文件转换为 DOC 和 DOCX 格式。

将 PDF 转换为 DOC (Microsoft Word 97-2003) 文件

轻松且完全控制地将 PDF 文件转换为 DOC 格式。Aspose.PDF for .NET 灵活且支持多种转换。例如,将 PDF 文档的页面转换为图像是一个非常受欢迎的功能。

我们的许多客户请求将 PDF 转换为 DOC:将 PDF 文件转换为 Microsoft Word 文档。客户希望这样做是因为 PDF 文件不易于编辑,而 Word 文档则可以。一些公司希望其用户能够在以 PDF 开始的文件中操作文本、表格和图像。

延续简化和易于理解的传统,Aspose.PDF for .NET 让您只需两行代码即可将源 PDF 文件转换为 DOC 文件。为了实现此功能,我们引入了一个名为 SaveFormat 的枚举,其值 .Doc 允许您将源文件保存为 Microsoft Word 格式。

以下 C# 代码片段展示了如何将 PDF 文件转换为 DOC 格式。

步骤:在 C# 中将 PDF 转换为 DOC

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 通过调用 Document.Save() 方法将其保存为 SaveFormat.Doc 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoWord()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    usnig (var document = new Aspose.Pdf.Document(dataDir + "PDFToDOC.pdf"))
    {
        // Save the file into MS document format
        document.Save(dataDir + "PDFToDOC_out.doc", SaveFormat.Doc);
    }
}

使用 DocSaveOptions 类

DocSaveOptions 类提供了许多属性,可以改善将 PDF 文件转换为 DOC 格式的效果。在这些属性中,Mode 使您能够指定 PDF 内容的识别模式。您可以为此属性选择 RecognitionMode 枚举中的任何值。每个值都有特定的优点和限制:

  • Textbox 模式速度快,适合保留 PDF 文件的原始外观,但生成文档的可编辑性可能有限。原始 PDF 中的每个视觉分组文本块都转换为输出文档中的文本框。这实现了与原始文档的最大相似性,因此输出文档看起来不错,但完全由文本框组成,这在 Microsoft Word 中编辑起来相当困难。
  • Flow 是完全识别模式,引擎执行分组和多级分析,以根据作者的意图恢复原始文档,同时生成易于编辑的文档。限制是输出文档可能与原始文档看起来不同。

RelativeHorizontalProximity 属性可用于控制文本元素之间的相对接近度。这意味着距离是以字体大小为标准的。较大的字体可能在音节之间有更大的间距,但仍被视为一个整体。它以字体大小的百分比表示;例如,1 = 100%。这意味着两个 12pt 的字符相距 12 pt 是接近的。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoWordDocAdvanced()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDF-to-DOC.pdf"))
    {
        var saveOptions = new Aspose.Pdf.DocSaveOptions
        {
            // Set format to save MS document
            Format = Aspose.Pdf.DocSaveOptions.DocFormat.Doc,
            // Set the recognition mode as Flow
            Mode = Aspose.Pdf.DocSaveOptions.RecognitionMode.Flow,
            // Set the Horizontal proximity as 2.5
            RelativeHorizontalProximity = 2.5f,
            // Enable the value to recognize bullets during the conversion process
            RecognizeBullets = true
        };
        // Save the file into MS document with save options
        document.Save(dataDir + "PDFtoDOC_out.doc", saveOptions);
    }
}

将 PDF 转换为 DOCX (Microsoft Word 2007-2024) 文件

Aspose.PDF for .NET API 允许您使用 C# 和任何 .NET 语言读取和转换 PDF 文档为 DOCX。DOCX 是 Microsoft Word 文档的知名格式,其结构从简单的二进制文件更改为 XML 和二进制文件的组合。Docx 文件可以在 Word 2007 及更高版本中打开,但不能在早期版本的 MS Word 中打开,后者支持 DOC 文件扩展名。

以下 C# 代码片段展示了如何将 PDF 文件转换为 DOCX 格式。

步骤:在 C# 中将 PDF 转换为 DOCX

  1. 创建一个 Document 对象的实例,使用源 PDF 文档。
  2. 通过调用 Document.Save() 方法将其保存为 SaveFormat.DocX 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoWord_DOCX_Format()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFToDOC.pdf"))
    {
        // Save the file into MS document format
        document.Save(dataDir + "PDFtoDOC_out.docx", SaveFormat.DocX);
    }
}

在增强模式下将 PDF 转换为 DOCX

为了获得更好的 PDF 转 DOCX 转换结果,您可以使用 EnhancedFlow 模式。 Flow 和 Enhanced Flow 之间的主要区别在于表格(无论是否带边框)被识别为真实表格,而不是带有背景图片的文本。 还可以识别编号列表和许多其他小细节。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoWord_Advanced_DOCX_Format()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFToDOC.pdf"))
    {
        // Instantiate DocSaveOptions object
        DocSaveOptions saveOptions = new Aspose.Pdf.DocSaveOptions
        {
            // Set format to save MS document
            Format = Aspose.Pdf.DocSaveOptions.DocFormat.DocX,
            // Set the recognition mode as EnhancedFlow
            Mode = Aspose.Pdf.DocSaveOptions.RecognitionMode.EnhancedFlow
        };

        // Save the file into MS document format
        document.Save(dataDir + "PDFToDOC_out.docx", saveOptions);
    }
}

另请参阅

本文还涵盖了以下主题。代码与上述相同。

格式: Word

格式: DOC

格式: DOCX