Word转PDF网上有很多实现方式。本文主要介绍一个多个Word或者多张图片转为同一个PDF的方法。希望对大家有所帮助。
此种实现方式需要下载个第三方DLL Aspose ,iTextSharp。 可到个人网盘下载Aspose DLL下载。
本文通过一个简单的Winform程序,给大家演示下如何使用该类库,以及如何生成PDF。
1.图片转PDF,首先选择图片导入到程序列表中:
#region 选择图片
private void btnSelectPics_Click(object sender, EventArgs e)
{
string[] imagesPath;//图片路径数据
//打开文件
OpenFileDialog ofd = new OpenFileDialog();
//ofd.InitialDirectory = "C:";//默认初始目录
ofd.Filter = "图片 (*.jpg,*.jpeg,*.bmp)|*.jpg;*.jpeg;*.bmp";
ofd.Multiselect = true;//可以多选文件
ofd.RestoreDirectory = false;//不还原当前目录,方便下次继续从相同地方添加图片
if (ofd.ShowDialog() == DialogResult.OK)
{
imagesPath = ofd.FileNames;
if (imagesPath != null && imagesPath.Length < 1)
return;
//将图片加入图片列表
lbPicAdd(imagesPath);
}
}
/// <summary> 将图片加入图片列表 </summary>
private void lbPicAdd(string[] strAdd)
{
if (strAdd.Length < 1) return;
for (int i = 0; i < strAdd.Length; i++)
{
lbPic.Items.Add(strAdd[i]);
}
}
#endregion
将图片列表转换为PDF:
#region 转换PDF
private void btnTurn_Click(object sender, EventArgs e)
{
//保存PDF的路径
string savePath = string.Empty;
//保存文件
SaveFileDialog sfd = new SaveFileDialog();
//sfd.InitialDirectory = "C:";//默认初始目录
sfd.Filter = "PDF文件 (*.pdf)|*.pdf";
sfd.RestoreDirectory = false;//记住保存目录
if (sfd.ShowDialog() == DialogResult.OK)
{
savePath = sfd.FileName;
}
if (string.IsNullOrEmpty(savePath)) return;
//图片转换PDF
try
{
Cursor = Cursors.WaitCursor;
string[] jpgs = GetPicsPath();//获取列表中已排序的路径数组
if (jpgs == null || jpgs.Length < 1) { Cursor = Cursors.Default; return; }
ImagesToPDF.ConvertJPG2PDF(jpgs, savePath);
MessageBox.Show("转换完成!", "提示信息");
}
catch (Exception ex)
{
MessageBox.Show("程序出错!错误信息:\r\n" + ex.Message, "提示信息");
}
finally
{
Cursor = Cursors.Default;
}
}
private string[] GetPicsPath()
{
string[] pics = new string[lbPic.Items.Count];
for (int i = 0; i < pics.Length; i++)
{
pics[i] = lbPic.GetItemText(lbPic.Items[i]);
}
return pics;
}
#endregion
图片上移、下移、移除代码:
private void moveUp(ListBox lb)
{
//第一项则不动
if (lb.SelectedIndex == 0) return;
int sIndex = lb.SelectedIndex;//选择项索引
object obj1 = lb.Items[sIndex - 1];
lb.Items[sIndex - 1] = lbPic.SelectedItem;//交换
lb.Items[sIndex] = obj1;
//设置焦点
lb.SelectedIndex = --sIndex;
}
private void MoveDown(ListBox lb)
{
//最后一项则不动
if (lb.SelectedIndex == lb.Items.Count - 1) return;
int sIndex = lb.SelectedIndex;//选择项索引
object obj1 = lb.Items[sIndex + 1];
lb.Items[sIndex + 1] = lb.SelectedItem;//交换
lb.Items[sIndex] = obj1;
//设置焦点
lb.SelectedIndex = ++sIndex;
}
private void Delete(ListBox lb)
{
//未选中任何项则返回
if (lb.SelectedItem == null) return;
int sIndex = lb.SelectedIndex;
//删除当前选中项
lb.Items.RemoveAt(sIndex);
//设置焦点
lb.SelectedIndex = sIndex > lb.Items.Count - 1 ? --sIndex : sIndex;
}
#region 图片列表控制
//上移
private void btnMoveUp_Click(object sender, EventArgs e)
{
try
{
moveUp(lbPic);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//下移
private void btnMoveDown_Click(object sender, EventArgs e)
{
try
{
MoveDown(lbPic);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//删除
private void btnDeletePic_Click(object sender, EventArgs e)
{
try
{
Delete(lbPic);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
其中图片转为PDF用了ItextSharp dll,此处封装的PicToPDF类代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using OfficeWordAddInsDom;
using System.IO;
namespace PicsToPDF
{
class ImagesToPDF
{
public static void ConvertJPG2PDF(string[] jpgs, string pdf)
{
if (jpgs.Length < 1 || string.IsNullOrEmpty(pdf)) return;
Document document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
using (FileStream stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
for (int i = 0; i < jpgs.Length; i++)
{
string jpgfile = jpgs[i];
using (FileStream imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Image image = Image.GetInstance(imageStream);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 0)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 0, iTextSharp.text.PageSize.A4.Height - 0);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 0)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 0, iTextSharp.text.PageSize.A4.Height - 0);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.Add(image);
}
}
document.Close();
}
}
public static void ConvertWord2PDF(string Word, string SavePath)
{
if (string.IsNullOrEmpty(SavePath)) return;
Document document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
using (FileStream stream = new FileStream(SavePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
string Wordfile = Word;
using (FileStream Stream = new FileStream(Wordfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
}
document.Close();
}
}
}
}
2.Word转PDF的代码实现方式如下,主要添加Aspose.Word的引用即可。
#region 选择word文档
private void button1_Click(object sender, EventArgs e)
{
string[] FilePath;//文档数据路径
//打开文件
OpenFileDialog ofd = new OpenFileDialog();
//ofd.InitialDirectory = "C:";//默认初始目录
ofd.Filter = "Word(*.doc,*.docx,*.dot)|*.doc;*.docx;*.dot";
ofd.Multiselect = true;//可以多选文件
ofd.RestoreDirectory = false;//不还原当前目录,方便下次继续从相同地方添加图片
if (ofd.ShowDialog() == DialogResult.OK)
{
FilePath = ofd.FileNames;
if (FilePath == null)
return;
lbWordAdd(FilePath);
}
}
#endregion
#region 转换为PDF
private void button2_Click(object sender, EventArgs e)
{
string savePath = string.Empty;
//保存文件
SaveFileDialog sfd = new SaveFileDialog();
//sfd.InitialDirectory = "C:";//默认初始目录
sfd.Filter = "PDF文件 (*.pdf)|*.pdf";
sfd.RestoreDirectory = false;//记住保存目录
if (sfd.ShowDialog() == DialogResult.OK)
{
savePath = sfd.FileName;
}
if (string.IsNullOrEmpty(savePath)) return;
try
{
Document doc = new Document(lbWord.GetItemText(lbWord.Items[0]));
doc.RemoveAllChildren();
string[] filepath = new string[lbWord.Items.Count];
for (int i = 0; i < filepath.Length; i++)
{
filepath[i] = lbWord.GetItemText(lbWord.Items[i]);
Document srcDoc = new Document(filepath[i]);
doc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
}
doc.Save(savePath, SaveFormat.Pdf);
MessageBox.Show("转换完成!", "提示信息");
}
catch (Exception ex)
{
MessageBox.Show("程序出错!错误信息:\r\n" + ex.Message, "提示信息");
}
finally
{
Cursor = Cursors.Default;
}
}
#endregion
/// <summary> 将word文档加入列表 </summary>
private void lbWordAdd(string[] strAdd)
{
if (strAdd.Length < 1) return;
for (int i = 0; i < strAdd.Length; i++)
{
lbWord.Items.Add(strAdd[i]);
}
}
界面如下所示:
源代码的下载路径如下:源代码和DLL下载