Word控件Spire.Doc 转换教程(十二):如何将 Word 转换为 EPub、XPS、Emf

本文将介绍一种通过名为 Spire.Doc 的强大且独立的Word .NET 组件将 Word 转换为 EPub、XPS、Emf的简单方法,无需在机器上安装 Microsoft Word。它还支持将 Word 和HTML转换为常用的图像格式,如 Jpeg、Png、Gif、Bmp 和Tiff等。

Spire.Doc for.NET 最新下载

(一)在 C#、VB.NET 中将 Word 转换为 EPub

EPub是一种电子书格式,以最合适的阅读方式显示内容。因此,通常将Word文档转换为EPub,以给予读者更精彩的阅读效果。本指南演示了使用Spire.Doc轻松将 C# 和 VB.NET 中的 Word 转换为 EPub 的解决方案。以下屏幕截图显示了从 Word 转换后的 EPub 文件。


Spire.Doc for .NET 提供了Document类的SaveToFile(String fileName, FileFormat fileFormat)方法,用于将 Word 转换为 EPub 或其他文档格式。调用此方法将 Word 转换为 EPub 时,参数 String fileName 的扩展名应为 .epub,并且在选项中应选择 enum FileFormat 作为 EPub。下载并安装 Spire.Doc for .NET并遵循以下代码:

[C#]

using Spire.Doc;

namespace WordtoEPUB
{
class Epub
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\Spire.Doc for .NET.docx");
//Convert Word to EPub
document.SaveToFile("ToEpub.epub", FileFormat.EPub);
System.Diagnostics.Process.Start("ToEpub.epub");
}
}
}

[VB.NET]

Imports Spire.Doc

Namespace WordtoEPUB
Friend Class Epub
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\Spire.Doc for .NET.docx")
'Convert Word to EPub
document.SaveToFile("ToEpub.epub", FileFormat.EPub)
System.Diagnostics.Process.Start("ToEpub.epub")
End Sub
End Class
End Namespace

Spire.Doc 是无需自动化即可操作 MS Word 文档的专业独立组件,使开发人员能够在其 .NET、WPF 和 Silverlight 应用程序上生成、读取、写入、修改 Word 文档。


(二)在 C# 中将 Word 转换为 XPS

XML 纸张规范 (XPS) 为分页的可打印文档指定了一组文档布局功能。与 Adobe System 的 PDF 格式一样,XPS 是一种固定布局的文档,旨在保持文档保真度,提供与设备无关的文档外观。

我们的许多客户要求的转换是 DOC/DOCX 到 XPS in.NET:将 Word 文档转换为 XPS 文件。Spire.Doc for .NET 允许您将 Word(Doc/Docx) 转换为 XPS。您可以使用 spire.doc 汇编的方法转换 word 文档。

[C#]

using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
namespace Doc_to_xps
{
class Program
{
static void Main(string[] args)
{
//doc file
String file = "doc to xpsdoc.docx";
//open doc document
Document doc = new Document(file);
//convert to xps file.
doc.SaveToFile("doc to xpsSample.xps", FileFormat.XPS);
System.Diagnostics.Process.Start("doc to xpsSample.xps");

}
}
}

查看以下C#代码,首先实例化一个document对象,调用Load File方法加载一个doc文档。然后将此文档数据保存为 XPS 格式。

[C#]

//doc file
String file = "doc to xpsdoc.docx";
//open doc document
Document doc = new Document(file);
//convert to xps file.
doc.SaveToFile("doc to xpsSample.xps", FileFormat.XPS);
System.Diagnostics.Process.Start("doc to xpsSample.xps");

以下屏幕截图显示了将 Word 转换为 XPS 的结果。


(三)在 C# 中将 Word 转换为 Emf

Emf 是增强型 MetaFile 的文件扩展名,用作 Windows 操作系统的打印机驱动程序的图形语言。1993 年,带有附加命令的更新版本 32 位版本的 Win32/GDI 引入了增强元文件 (Emf)。Microsoft 还建议使用增强格式 (Emf) 函数,而不是很少使用的 Windows 格式 (WMF) 函数。

Spire.Doc 提供了通过以下 5 行简单代码将 Word 转换为 Emf 的几乎最简单的解决方案。

[C#]

using Spire.Doc;
using System.Drawing.Imaging;

namespace DOCEMF
{
class Program
{
static void Main(string[] args)
{
// create an instance of Spire.Doc.Document
Document doc = new Document();
// load the file base on a specified file name
doc.LoadFromFile(@"../../Original Word.docx", FileFormat.Docx);
//convert the first page of document to image
System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
// save the document object to Emf file
image.Save(@"../../Convert Word to Image.emf", ImageFormat.Emf);
//close the document
doc.Close();

}
}
}

查看下面的效果截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值