欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐

 更新時(shí)間:2024年01月10日 15:18:30   作者:JosieBook  
有時(shí)我們可能會(huì)遇到需要的資料或教程被分成了幾部分存放在多個(gè)PDF文件中,本文主要介紹了C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下

一、下載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#讀取xml文件到datagridview實(shí)例

    c#讀取xml文件到datagridview實(shí)例

    c#讀取xml文件到datagridview實(shí)例,需要的朋友可以參考一下
    2013-03-03
  • C#使用JArray和JObject封裝JSON對(duì)象

    C#使用JArray和JObject封裝JSON對(duì)象

    這篇文章介紹了C#使用JArray和JObject封裝JSON對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#實(shí)現(xiàn)上傳下載圖片

    C#實(shí)現(xiàn)上傳下載圖片

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)上傳下載圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • C#給Word不同頁(yè)面設(shè)置不同背景

    C#給Word不同頁(yè)面設(shè)置不同背景

    這篇文章主要介紹了C#給Word不同頁(yè)面設(shè)置不同背景,文章圖文講解的很清晰,有對(duì)于這方面不懂得同學(xué)可以學(xué)習(xí)下
    2021-02-02
  • C#中事件的繼承實(shí)例分析

    C#中事件的繼承實(shí)例分析

    這篇文章主要介紹了C#中事件的繼承,通過一個(gè)面向?qū)ο蟪绦驅(qū)嵗齺碚f明子類調(diào)用父類事件的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-08-08
  • c# 開發(fā)文字識(shí)別軟件

    c# 開發(fā)文字識(shí)別軟件

    這篇文章主要介紹了c# 開發(fā)文字識(shí)別軟件的方法,幫助大家更好的利用c#進(jìn)行文字識(shí)別,感興趣的朋友可以了解下
    2020-12-12
  • 不使用qvod播放器獲取qvod播放路徑的方法

    不使用qvod播放器獲取qvod播放路徑的方法

    這篇文章主要介紹了使用c#獲取qvod播放路徑的方法,大家參考使用吧
    2014-01-01
  • c#下將.cs文件編譯成dll

    c#下將.cs文件編譯成dll

    c#下將.cs文件編譯成dll...
    2007-07-07
  • c#實(shí)現(xiàn)圖片的平移和旋轉(zhuǎn)示例代碼

    c#實(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
  • C#讀寫config配置文件的方法

    C#讀寫config配置文件的方法

    下面小編就為大家?guī)硪黄狢#讀寫config配置文件的方法。小編覺的挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12

最新評(píng)論