C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐
一、下載iTextSharp.dll
下載iTextSharp.dll
可使用聯(lián)機(jī)方式或者文件下載方式。

命名空間引入
代碼開(kāi)始時(shí)引入了一些命名空間,這些命名空間包含了程序運(yùn)行所需的類和方法。
- System、System.Collections.Generic、System.ComponentModel等是.NET框架的核心命名空間。
- iTextSharp.text 和 iTextSharp.text.pdf 是用于處理PDF文件的庫(kù)。
- System.IO 是用于文件和目錄操作的命名空間。
- Microsoft.Win32 是用于訪問(wèn)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è)路徑。如果沒(méi)有,它會(huì)提示用戶選擇一個(gè)路徑;如果已選擇,它會(huì)調(diào)用一個(gè)方法來(lái)合并PDF文件,并顯示一個(gè)消息告知用戶合并已完成,然后打開(kāi)導(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("合并完成!");
// 打開(kāi)導(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文件名稱組合起來(lái)。
創(chuàng)建一個(gè)新的文件流stream,并使用FileStream類以寫入模式打開(kāi)它,準(zhǔn)備寫入輸出PDF文件。
創(chuàng)建一個(gè)新的Document對(duì)象pdfDoc,表示輸出PDF文件。
創(chuàng)建一個(gè)新的PdfCopy對(duì)象pdf,用于將內(nèi)容復(fù)制到輸出PDF文件中。
打開(kāi)輸出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("合并完成!");
// 打開(kāi)導(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#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
本文主要介紹了c#基礎(chǔ)知識(shí)中的數(shù)組與接口使用方法,結(jié)合示例,大家一看就明白2014-01-01
淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別
C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過(guò)本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧2017-09-09
C#中DropDownList動(dòng)態(tài)生成的方法
這篇文章主要介紹了C#中DropDownList動(dòng)態(tài)生成的方法,實(shí)例分析了C#中DropDownList的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
C#之WinForm WebBrowser實(shí)用技巧匯總
這篇文章主要介紹了C#之WinForm WebBrowser實(shí)用技巧匯總,包括常見(jiàn)的各種設(shè)置及信息獲取等,需要的朋友可以參考下2014-08-08

