Spire.Doc for .NET 运用(三)

来源:互联网 发布:武汉软件工程学院地址 编辑:程序博客网 时间:2024/06/18 12:26

WordTo 第二篇

WordToPDF 用 Spire.Doc 将 Word 文档转换为 PDF 格式文件

1、创建 WinForm 项目,添加 Button;




2、安装和添加引用
在“程序包管理控制台”中键入“install-package Spire.Doc”,将“Spire.Doc”安装到项目中;



3、添加代码
双击 Button ,添加如下代码:

using System;using System.Windows.Forms;using Spire.Doc;using Spire.Doc.Documents;namespace WordToPDF{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Document doc = new Document();            doc.LoadFromFile(@"Sample.doc");            doc.SaveToFile("Sample.pdf", FileFormat.PDF);            WordDocViewer("Sample.pdf");        }        private void WordDocViewer(string fileName)        {            try            {                System.Diagnostics.Process.Start(fileName);            }            catch { }        }    }}

4、添加文件
在目录 “Spire.Doc\WordToXML\WordToXML\WordToPDF\bin\Debug” 文件夹下添加上一篇生成的 Word 文档 ”Sample.doc“ ;


5、运行项目
启动项目,点击 Button ,即可在PDF阅读器中看到 PDF 格式加载的页面了;



这样就完成了 Word 到 PDF 的格式转换。