用 C# 将 EPUB 转换为 DOCX

DOCX 是一种著名的 Microsoft Word 文档格式。这种格式之所以流行,是因为它支持多种格式化功能,为用户编写任何类型的文档提供了多种选择。DOCX 文件可以用 Word 2007 和横向版本打开,但不能用支持 DOC 文件扩展名的 MS Word 早期版本打开。EPUB到DOCX的转换通常需要利用DOCX格式来完成特定的用户任务。

本文将介绍如何使用 Converter 类的 ConvertEPUB() 方法将 EPUB 转换为 DOCX 以及如何应用 DocSaveOptionsICreateStreamProvider 参数。

在线 EPUB 转换器

您可以使用 Aspose.HTML for .NET API 实时将 EPUB 转换为其他格式。首先,从本地驱动器加载 EPUB 文件,然后运行示例。示例中的保存选项为默认设置。您将立即以单独文件的形式收到转换结果。

                
            

如果你想以编程方式将 EPUB 转换为 DOCX,请参阅以下 C# 代码示例。

通过两行代码将 EPUB 转换为 DOCX

Converter 类的静态方法主要用作将 EPUB 文件转换为各种格式的最简单方法。例如,只需两行代码,您就可以在 C# 应用程序中将EPUB转换为DOCX

1// Convert EPUB to DOCX using C#
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Invoke the ConvertEPUB() method to convert EPUB to DOCX
7Converter.ConvertEPUB(stream, new DocSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.docx"));

将 EPUB 转换为 DOCX

要将 EPUB 转换为 DOCX,您需要遵循以下几个步骤:

  1. 打开现有的 EPUB 文件。在示例中,我们使用System.IO.FileStream类的OpenRead()方法从指定路径的文件系统中打开并读取EPUB文件。
  2. 创建 DocSaveOptions 类的实例。
  3. 使用 Converter 类的 ConvertEPUB() 方法将 EPUB 保存为 DOCX 文件。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、DocSaveOptions 和输出文件路径,以实现 EPUB 到 DOCX 的转换。

下面的 C# 代码片段显示了如何使用 Aspose.HTML for .NET 将 EPUB 转换为 DOCX。

 1// Convert EPUB to DOCX using C#
 2
 3// Open an existing EPUB file for reading
 4using FileStream stream = File.OpenRead(DataDir + "input.epub");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "input-output.docx");
 8
 9// Create an instance of the DocSaveOptions class
10DocSaveOptions options = new DocSaveOptions();
11
12// Call the ConvertEPUB() method to convert EPUB to DOCX
13Converter.ConvertEPUB(stream, options, savePath);

保存选项 – DocSaveOptions 类

Aspose.HTML for .NET 允许使用默认或自定义保存选项将 EPUB 转换为 DOCX。使用 DocSaveOptions 可以调整渲染过程;可以指定页面大小、页边距、分辨率、CSS 等。

PropertyDescription
FontEmbeddingRuleThis property gets or sets the font embedding rule. Available values are Full and None. The default value is None.
CssGets a CssOptions object which is used for configuration of CSS properties processing.
DocumentFormatThis property gets or sets the file format of the output document. The default value is DOCX.
PageSetupThis property gets a page setup object and uses it for configuration output page-set.
HorizontalResolutionSets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionSets vertical resolution for output images in pixels per inch. The default value is 300 dpi.

要了解有关 DocSaveOptions 的更多信息,请阅读 微调转换器 一文。

使用 DocSaveOptions 将 EPUB 转换为 DOCX

使用Aspose.HTML for .NET,您可以通过程序将EPUB转换为DOCX格式,并完全控制各种转换参数。要使用指定的 DocSaveOptions 将 EPUB 转换为 DOCX,您需要遵循以下几个步骤:

  1. 打开现有 EPUB 文件
  2. 创建一个新的 DocSaveOptions 对象,并指定所需的保存选项。
  3. 使用 ConvertEPUB() 方法将 EPUB 保存为 DOCX 文件。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、DocSaveOptions 和输出文件路径,以便将 EPUB 转换为 DOCX。

下面的示例显示了如何使用 DocSaveOptions 并创建带有自定义保存选项的 DOCX 文件:

 1// Convert EPUB to DOCX with custom settings using C#
 2
 3// Open an existing EPUB file for reading
 4using FileStream stream = File.OpenRead(DataDir + "input.epub");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "input-options.docx");
 8
 9// Create an instance of DocSaveOptions. Set A5 as a page-size
10DocSaveOptions options = new DocSaveOptions();
11options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
12
13// Call the ConvertEPUB() method to convert EPUB to DOCX
14Converter.ConvertEPUB(stream, options, savePath);

在示例中,我们使用 System.IO.FileStream 类的 OpenRead() 方法打开并从指定路径的文件系统中读取源文件。DocSaveOptions() 构造函数初始化了一个 DocSaveOptions 类实例,并将其传递给 ConvertEPUB() 方法。ConvertEPUB() 方法接收 streamoptions、输出文件路径 savePath 并执行转换操作。DocSaveOptions 类提供了大量属性,让你可以全面控制各种参数,并改进 EPUB 到 DOCX 格式的转换过程。在示例中,我们使用了指定 DOCX 文档页面大小的 PageSetup 属性。

输出流提供商

如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。

Aspose.HTML for .NET为渲染操作提供了多种输出格式。其中一些格式只生成一个输出文件(例如 PDFXPS),其他格式则生成多个文件(图像格式 JPGPNG等)。

下面的示例展示了如何在应用程序中实现和使用自己的MemoryStreamProvider

 1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
 2
 3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
 4{
 5    // List of MemoryStream objects created during the document rendering
 6    public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
 7
 8    public Stream GetStream(string name, string extension)
 9    {
10        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11        MemoryStream result = new MemoryStream();
12        Streams.Add(result);
13        return result;
14    }
15
16    public Stream GetStream(string name, string extension, int page)
17    {
18        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19        MemoryStream result = new MemoryStream();
20        Streams.Add(result);
21        return result;
22    }
23
24    public void ReleaseStream(Stream stream)
25    {
26        // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27    }
28
29    public void Dispose()
30    {
31        // Releasing resources
32        foreach (MemoryStream stream in Streams)
33            stream.Dispose();
34    }
35}

以下代码片段演示了如何使用内存流将 EPUB 文件转换为 DOCX 文件。

 1// Convert EPUB to DOCX in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Open an existing EPUB file for reading
 7using FileStream stream = File.OpenRead(DataDir + "input.epub");
 8
 9// Prepare a path to save the converted file 
10string savePath = Path.Combine(OutputDir, "stream-provider.docx");
11
12// Convert EPUB to DOCX by using the MemoryStreamProvider class
13Converter.ConvertEPUB(stream, new DocSaveOptions(), streamProvider);
14
15// Get access to the memory stream that contains the result data
16MemoryStream memory = streamProvider.Streams.First();
17memory.Seek(0, SeekOrigin.Begin);
18
19// Flush the result data to the output file
20using (FileStream fs = File.Create(savePath))
21{
22    memory.CopyTo(fs);
23}

您可以从 GitHub 下载完整的示例和数据文件。

Aspose.HTML提供免费在线 EPUB到DOCX转换器,可将EPUB转换为高质量、简单快捷的DOCX。只需上传、转换文件并在几秒钟内获得结果!

文本 “EPUB到DOCX转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.