众所周知,Word是我们日常工作中常用的办公软件之一,有时出于某种需求我们需要将Word文档转换为PDF以及Text。那么如何以C#,VB.NET编程的方式来实现这一功能呢?
下面我将分开介绍如何运用免费版的Spire.Doc for .NET组件来实现Word到PDF以及Text的转换。
Free Spire.Doc for .NET组件概述
Free Spire.Doc for .NET 是Spire.Doc for .NET的免费版,它是一款完全免费且可供商业和个人使用的Word组件。通过该组件,开发人员可以在应用程序中创建、读、写、保存、打印以及转换Word文档。特色功能:可以将Word文档(Word 97-2003,Word 2007,Word 2010,Word 2013)转换为常用的文件格式,例如:PDF、TXT、XML、RTF、XPS、EPUB、HTML和图像等等。
作为一款独立的.NET组件,Free Spire.Doc for .NET的运行无需安装Microsoft Office。
- 如何将Word转换为PDF:
步骤1:新建一个文档并加载需要转换的Word文档
document.LoadFromFile(@"C:\Users\Administrator\Desktop\小王子内容简介.docx");
步骤2:将Word文档以PDF格式保存到文件夹中
document.SavetoFile("result.PDF",FileFormat.PDF);
步骤3:运行文档
System.Diagnostics.Process.Start(");
以下是转换前和转换后的对比图:
转换前
转换后
C#完整代码
using Spire.Doc; System; System.Collections.Generic; System.Linq; System.Text; namespace convert { class Program { static void Main(string[] args) { //新建一个文档并加载需要转换的Word文档 Document document = new Document(); document.LoadFromFile("); //将Word文档以PDF格式保存到文件夹中 document.SavetoFile(,FileFormat.PDF); 运行文档 System.Diagnostics.Process.Start(); } } }
VB.NET完整代码
Imports Spire.Doc System.Collections.Generic System.Linq System.Text Namespace convert Class Program Private Shared Sub Main(args As String()) '新建一个文档并加载需要转换的Word文档 Dim document New Document() document.LoadFromFile() ') End Sub End Class End Namespace
- 如何将Word转换为Text:
将Word文档转换为Text后可以节省磁盘空间,同时也可以兼容几乎所有的应用程序。
步骤1:新建一个文档并加载需要转换的word文档
Document document = Document(); document.LoadFromFile(C:\Users\Administrator\Desktop\小王子出版版本.docx");
步骤2:将文档以TXT格式保存到文件夹中
document.SavetoFile(Sample.txt
WordDocViewer(");
步骤4:将下列完整的代码写入您的项目中,运行后将自动生成一个TXT文档
word_to_text { 新建一个文档并加载需要转换的word文档 Document document = 将文档以TXT格式保存到文件夹中 document.SavetoFile(运行文档 WordDocViewer(); } private void WordDocViewer( fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } } } }