C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐
一、下載iTextSharp.dll
下載iTextSharp.dll
可使用聯(lián)機(jī)方式或者文件下載方式。
命名空間引入
代碼開始時(shí)引入了一些命名空間,這些命名空間包含了程序運(yùn)行所需的類和方法。
- System、System.Collections.Generic、System.ComponentModel等是.NET框架的核心命名空間。
- iTextSharp.text 和 iTextSharp.text.pdf 是用于處理PDF文件的庫(kù)。
- System.IO 是用于文件和目錄操作的命名空間。
- Microsoft.Win32 是用于訪問Windows注冊(cè)表的命名空間。
- System.Diagnostics 是用于診斷和調(diào)試的命名空間。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using Microsoft.Win32; using System.Diagnostics;
二、界面設(shè)計(jì)
三、代碼
全局變量
定義了三個(gè)靜態(tài)字符串變量,用于存儲(chǔ)上次選擇的文件夾路徑、輸入文件夾路徑和輸出文件夾路徑。
// 全局變量 private static string lastFolderPath = ""; // 記錄上次選擇文件夾的路徑 private static string inputFolderPath = ""; // 輸入文件夾路徑 private static string outputFolderPath = ""; // 輸出文件夾路徑
選擇文件夾的按鈕
從Windows注冊(cè)表中讀取上次選擇的文件夾路徑。
顯示一個(gè)文件夾選擇對(duì)話框,讓用戶選擇包含PDF文件的文件夾。
如果用戶選擇了一個(gè)文件夾,將該路徑存儲(chǔ)在inputFolderPath變量中,并創(chuàng)建(如果不存在)一個(gè)名為"Output"的子文件夾作為輸出路徑。
將選擇的路徑顯示在文本框txtPath中。
/// <summary> /// 按鈕,選擇文件夾路徑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSelectPath_Click(object sender, EventArgs e) { // 讀取上次選擇的文件夾路徑 string registryKey = "Software\\PDF合并"; // 用您自己的應(yīng)用程序名稱替換YourAppName if (Registry.CurrentUser.OpenSubKey(registryKey) != null) { lastFolderPath = Registry.CurrentUser.OpenSubKey(registryKey).GetValue("LastFolder") as string; } // 創(chuàng)建并顯示一個(gè)選擇文件夾的對(duì)話框 FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "選擇包含PDF文件的文件夾:"; folderDialog.SelectedPath = lastFolderPath; // 設(shè)置默認(rèn)路徑為用戶上次選擇的文件夾 if (folderDialog.ShowDialog() == DialogResult.OK) { // 獲取用戶選擇的文件夾路徑 inputFolderPath = folderDialog.SelectedPath; // 創(chuàng)建輸出文件夾路徑(如果它不存在則創(chuàng)建它) outputFolderPath = Path.Combine(inputFolderPath, "Output"); if (!Directory.Exists(outputFolderPath)) { Directory.CreateDirectory(outputFolderPath); } } txtPath.Text = inputFolderPath; }
確認(rèn)合并的按鈕
簡(jiǎn)而言之,當(dāng)用戶點(diǎn)擊“確認(rèn)合并PDF”按鈕時(shí),此方法首先檢查用戶是否已選擇了一個(gè)路徑。如果沒有,它會(huì)提示用戶選擇一個(gè)路徑;如果已選擇,它會(huì)調(diào)用一個(gè)方法來合并PDF文件,并顯示一個(gè)消息告知用戶合并已完成,然后打開導(dǎo)出的文件夾供用戶查看。
/// <summary> /// 按鈕,確認(rèn)合并PDF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOK_Click(object sender, EventArgs e) { if (txtPath.Text == string.Empty) { MessageBox.Show("請(qǐng)選擇要合并的文件夾路徑!"); } else { // 調(diào)用合并PDF的方法 MergePDFs(inputFolderPath, outputFolderPath, "123"); MessageBox.Show("合并完成!"); // 打開導(dǎo)出的文件夾路徑 Process.Start(outputFolderPath); } }
- 合并PDF的處理函數(shù)
定義方法:public void MergePDFs(string inputFolderPath, string outputFolderPath, string outputPdfName)
獲取輸入文件夾中所有的PDF文件,并將它們存儲(chǔ)在inputFiles字符串?dāng)?shù)組中。
創(chuàng)建輸出PDF文件的路徑,使用Path.Combine方法將輸出文件夾路徑和輸出PDF文件名稱組合起來。
創(chuàng)建一個(gè)新的文件流stream,并使用FileStream類以寫入模式打開它,準(zhǔn)備寫入輸出PDF文件。
創(chuàng)建一個(gè)新的Document對(duì)象pdfDoc,表示輸出PDF文件。
創(chuàng)建一個(gè)新的PdfCopy對(duì)象pdf,用于將內(nèi)容復(fù)制到輸出PDF文件中。
打開輸出PDF文檔以進(jìn)行寫入。
遍歷所有輸入的PDF文件,并使用PdfReader對(duì)象讀取每個(gè)PDF文件。
對(duì)于每個(gè)輸入的PDF文件,獲取其頁(yè)面數(shù),并使用for循環(huán)遍歷每個(gè)頁(yè)面。
將每個(gè)輸入PDF文件的頁(yè)面添加到輸出PDF文件中。
檢查輸出PDF文檔是否為空,如果不為空則關(guān)閉它。
關(guān)閉文件流。
創(chuàng)建新輸出PDF文件的完整路徑。
檢查新輸出文件是否已存在,如果已存在則刪除它。
將原輸出文件移動(dòng)到新位置并重命名為"AllPDF_Merged.pdf"。
/// <summary> /// 合并多個(gè)PDF文件為一個(gè)PDF文件 /// </summary> /// <param name="inputFolderPath"></param> /// <param name="outputFolderPath"></param> /// <param name="outputPdfName"></param> public void MergePDFs(string inputFolderPath, string outputFolderPath, string outputPdfName) { // 獲取輸入文件夾中所有的PDF文件 string[] inputFiles = Directory.GetFiles(inputFolderPath, "*.pdf"); // 創(chuàng)建輸出PDF文件路徑 string outputPdfPath = Path.Combine(outputFolderPath, outputPdfName); // 創(chuàng)建輸出PDF文件 using (FileStream stream = new FileStream(outputPdfPath, FileMode.Create)) { Document pdfDoc = new Document(); PdfCopy pdf = new PdfCopy(pdfDoc, stream); pdfDoc.Open(); foreach (string file in inputFiles) { // 讀取每個(gè)PDF文件并將其頁(yè)面添加到輸出PDF中 PdfReader reader = new PdfReader(file); int n = reader.NumberOfPages; for (int i = 1; i <= n; i++) { pdf.AddPage(pdf.GetImportedPage(reader, i)); } reader.Close(); } if (pdfDoc != null) pdfDoc.Close(); stream.Close(); } string newOutputPdfPath = Path.Combine(outputFolderPath, "AllPDF_Merged.pdf"); // 檢查新輸出文件是否已存在 if (File.Exists(newOutputPdfPath)) { // 如果已存在,則刪除舊文件并創(chuàng)建新文件 File.Delete(newOutputPdfPath); } // 重命名輸出PDF文件 File.Move(outputPdfPath, newOutputPdfPath); }
四、導(dǎo)出結(jié)果
可將一個(gè)文件夾內(nèi)的所有PDF合并成一個(gè)PDF文件導(dǎo)出。
五、完整源碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using Microsoft.Win32; using System.Diagnostics; namespace PDF合并 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // 全局變量 private static string lastFolderPath = ""; // 記錄上次選擇文件夾的路徑 private static string inputFolderPath = ""; // 輸入文件夾路徑 private static string outputFolderPath = ""; // 輸出文件夾路徑 /// <summary> /// 按鈕,選擇文件夾路徑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSelectPath_Click(object sender, EventArgs e) { // 讀取上次選擇的文件夾路徑 string registryKey = "Software\\PDF合并"; // 用您自己的應(yīng)用程序名稱替換YourAppName if (Registry.CurrentUser.OpenSubKey(registryKey) != null) { lastFolderPath = Registry.CurrentUser.OpenSubKey(registryKey).GetValue("LastFolder") as string; } // 創(chuàng)建并顯示一個(gè)選擇文件夾的對(duì)話框 FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.Description = "選擇包含PDF文件的文件夾:"; folderDialog.SelectedPath = lastFolderPath; // 設(shè)置默認(rèn)路徑為用戶上次選擇的文件夾 if (folderDialog.ShowDialog() == DialogResult.OK) { // 獲取用戶選擇的文件夾路徑 inputFolderPath = folderDialog.SelectedPath; // 創(chuàng)建輸出文件夾路徑(如果它不存在則創(chuàng)建它) outputFolderPath = Path.Combine(inputFolderPath, "Output"); if (!Directory.Exists(outputFolderPath)) { Directory.CreateDirectory(outputFolderPath); } } txtPath.Text = inputFolderPath; } /// <summary> /// 按鈕,確認(rèn)合并PDF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOK_Click(object sender, EventArgs e) { if (txtPath.Text == string.Empty) { MessageBox.Show("請(qǐng)選擇要合并的文件夾路徑!"); } else { // 調(diào)用合并PDF的方法 MergePDFs(inputFolderPath, outputFolderPath, "123"); MessageBox.Show("合并完成!"); // 打開導(dǎo)出的文件夾路徑 Process.Start(outputFolderPath); } } /// <summary> /// 合并多個(gè)PDF文件為一個(gè)PDF文件 /// </summary> /// <param name="inputFolderPath"></param> /// <param name="outputFolderPath"></param> /// <param name="outputPdfName"></param> public void MergePDFs(string inputFolderPath, string outputFolderPath, string outputPdfName) { // 獲取輸入文件夾中所有的PDF文件 string[] inputFiles = Directory.GetFiles(inputFolderPath, "*.pdf"); // 創(chuàng)建輸出PDF文件路徑 string outputPdfPath = Path.Combine(outputFolderPath, outputPdfName); // 創(chuàng)建輸出PDF文件 using (FileStream stream = new FileStream(outputPdfPath, FileMode.Create)) { Document pdfDoc = new Document(); PdfCopy pdf = new PdfCopy(pdfDoc, stream); pdfDoc.Open(); foreach (string file in inputFiles) { // 讀取每個(gè)PDF文件并將其頁(yè)面添加到輸出PDF中 PdfReader reader = new PdfReader(file); int n = reader.NumberOfPages; for (int i = 1; i <= n; i++) { pdf.AddPage(pdf.GetImportedPage(reader, i)); } reader.Close(); } if (pdfDoc != null) pdfDoc.Close(); stream.Close(); } string newOutputPdfPath = Path.Combine(outputFolderPath, "AllPDF_Merged.pdf"); // 檢查新輸出文件是否已存在 if (File.Exists(newOutputPdfPath)) { // 如果已存在,則刪除舊文件并創(chuàng)建新文件 File.Delete(newOutputPdfPath); } // 重命名輸出PDF文件 File.Move(outputPdfPath, newOutputPdfPath); } } }
到此這篇關(guān)于C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)C# PDF合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用JArray和JObject封裝JSON對(duì)象
這篇文章介紹了C#使用JArray和JObject封裝JSON對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07c#實(shí)現(xiàn)圖片的平移和旋轉(zhuǎn)示例代碼
這篇文章主要給大家介紹了關(guān)于c#實(shí)現(xiàn)圖片的平移和旋轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08