多個Word文件,多張圖片轉PDF方式實現
阿新 • • 發佈:2018-11-23
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下載