最近一直在忙论文,今天闲下来换了VS2019,顺便更新一篇博客~在系统功能的开发过程中,各类文档之间的转换是一项常规功能,现在就来说一下如何将Word文档转换为Pdf文档。转换的代码十分简单,只需要用到Aspose就OK了。Aspose包含两个dll,如下图所示:工程中引入这两个dll即可。
链接:https://pan.baidu.com/s/19GyuXsZ1TRcNrg_asYBfmg
提取码:l0vb
先来看一段简单的代码,了解一下Aspose转换的过程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Pdf;
using Aspose.Words;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
ConvertWordToPdf(@"E:\Users\DSF\Desktop\a.docx", @"E:\Users\DSF\Desktop\测试.pdf");
Console.WriteLine("转换成功");
Console.ReadKey();
}
static void ConvertWordToPdf(string wordFilePath, string pdfFilePath)
{
Aspose.Words.Document document = new Aspose.Words.Document(wordFilePath);
document.Save(pdfFilePath, Aspose.Words.SaveFormat.Pdf);
}
}
}
是不是十分简单?其实就是两步,首先创建文档对象,然后定义输出路径就行了。那么现在我想要实现批量的Word文档转换Pdf,文件数量一多,难免会造成卡顿,造成较差的用户体验,因此我还是选择使用BackGroundWorker组件实现多线程。软件界面如下:导入文件选择需要转换的Word文档所在的文件夹,导出文件选择转换后生成的Pdf文件所在文件夹。(PS:有点丑)
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinDocumentConvertion
{
public partial class MainForm : Form
{
private List<string> wordList = new List<string>();
// 构造函数
public MainForm()
{
InitializeComponent();
}
// DoWork事件
private void Bg_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < wordList.Count; i++)
{
if (bg.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
string pdfFilePath = txtExportFolder.Text + "\\" + System.IO.Path.GetFileNameWithoutExtension(wordList[i]) + ".pdf";
if (System.IO.File.Exists(pdfFilePath))
{
System.IO.File.Delete(pdfFilePath);
}
Aspose.Words.Document document = new Aspose.Words.Document(wordList[i]);
document.Save(pdfFilePath, Aspose.Words.SaveFormat.Pdf);
bg.ReportProgress(i + 1);
}
}
}
// ProgressChanged事件
private void Bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbConvertStatus.Value = e.ProgressPercentage;
}
// RunWorkerCompleted事件
private void Bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("转换发生异常!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (e.Cancelled)
{
MessageBox.Show("转换终止!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("转换完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
btnStart.Enabled = true;
btnStop.Enabled = false;
}
// 导入文件
private void BtnImportFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
txtImportFolder.Text = folderBrowserDialog.SelectedPath;
}
}
// 导出文件
private void BtnExportFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
txtExportFolder.Text = folderBrowserDialog.SelectedPath;
}
}
// 开始转换
private void BtnStart_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtImportFolder.Text))
{
MessageBox.Show("请选择导入文件夹!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(txtExportFolder.Text))
{
MessageBox.Show("请选择导出文件夹!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (bg.IsBusy)
{
return;
}
// 读取文件列表
DirectoryInfo directoryInfo = new DirectoryInfo(txtImportFolder.Text);
foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.docx"))
{
wordList.Add(fileInfo.FullName);
}
if (wordList.Count == 0)
{
MessageBox.Show("该文件夹没有可转换的Word文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 开始转换文件
btnStart.Enabled = false;
btnStop.Enabled = true;
pgbConvertStatus.Maximum = wordList.Count;
bg.RunWorkerAsync();
}
// 停止转换
private void BtnStop_Click(object sender, EventArgs e)
{
btnStart.Enabled = true;
btnStop.Enabled = false;
bg.CancelAsync();
}
// 关闭窗体
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
bg.CancelAsync();
}
}
}
利用BackGroundWorker组件可避免UI假死,转换过程中随意拖动窗体也无任何影响,转换的效果如图所示:转换前的Word
转换后的Pdf