C#,VB.NET如何将Word转换为PDF和Text

mac2022-06-30  64

 

众所周知,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("result.PDF");

 

以下是转换前和转换后的对比图:

                                                    转换前                                                                                                 

              

                                                         转换后

 

C#完整代码

using Spire.Doc; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace convert { class Program { static void Main(string[] args) { //新建一个文档并加载需要转换的Word文档 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\小王子内容简介.docx"); //将Word文档以PDF格式保存到文件夹中 document.SaveToFile("result.PDF", FileFormat.PDF); //运行文档 System.Diagnostics.Process.Start("result.PDF"); } } }

VB.NET完整代码

Imports Spire.Doc Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace convert Class Program Private Shared Sub Main(args As String()) '新建一个文档并加载需要转换的Word文档 Dim document As New Document() document.LoadFromFile("C:\Users\Administrator\Desktop\小王子内容简介.docx") '将Word文档以PDF格式保存到文件夹中 document.SaveToFile("result.PDF", FileFormat.PDF) '运行文档 System.Diagnostics.Process.Start("result.PDF") End Sub End Class End Namespace

 

如何将Word转换为Text:

将Word文档转换为Text后可以节省磁盘空间,同时也可以兼容几乎所有的应用程序。

步骤1:新建一个文档并加载需要转换的word文档

Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\小王子出版版本.docx");

步骤2:将文档以TXT格式保存到文件夹中

document.SaveToFile("Sample.txt", FileFormat.Txt);

步骤3:运行文档

WordDocViewer("Sample.txt");

步骤4:将下列完整的代码写入您的项目中,运行后将自动生成一个TXT文档

C#完整代码

using Spire.Doc; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace word_to_text { class Program { static void Main(string[] args) { //新建一个文档并加载需要转换的word文档 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\小王子出版版本.docx"); //将文档以TXT格式保存到文件夹中 document.SaveToFile("Sample.txt", FileFormat.Txt); //运行文档 WordDocViewer("Sample.txt"); } private static void WordDocViewer(string fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } } } }

VB.NET完整代码

Imports Spire.Doc Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace word_to_text Class Program Private Shared Sub Main(args As String()) '新建一个文档并加载需要转换的word文档 Dim document As New Document() document.LoadFromFile("C:\Users\Administrator\Desktop\小王子出版版本.docx") '将文档以TXT格式保存到文件夹中 document.SaveToFile("Sample.txt", FileFormat.Txt) '运行文档 WordDocViewer("Sample.txt") End Sub Private Shared Sub WordDocViewer(fileName As String) Try System.Diagnostics.Process.Start(fileName) Catch End Try End Sub End Class End Namespace

以下是转换前和转换后的对比图:

                                                   转换前                                                                                                   

   

                                                   转换后

 

 

今天就跟大家分享到这里,希望能给您带来一定的帮助。

转载于:https://www.cnblogs.com/Yesi/p/6756979.html

相关资源:VB.NET 读写 XML TXT源码
最新回复(0)