基于C#實現(xiàn)PDF按頁分割文件和分頁合并
背景
最近遇到一個文件上傳限制大小問題,
因為有哪些pdf文件可能有300多頁,大小已經(jīng)有100MB,
但是有些文件上傳限制大小在10MB以內(nèi),
因為本篇文章將簡單講講如何將大文件通過分頁分割和合并。
效果
下面就是通過pdf插件進行按頁進行文件分割輸出
單頁分割
插件命名空間
using iTextSharp.text; using iTextSharp.text.pdf;
目標(biāo)分割pdf文件、創(chuàng)建輸出文件所在的文件夾、iTextSharp插件操作pdf分割
// 目標(biāo)分割pdf文件 string inputFilePath = @"你自己的pdf文件物理路徑.pdf"; // 創(chuàng)建輸出文件所在文件夾 string outputFolder = "NewFile"; string rootPath = System.IO.Directory.GetCurrentDirectory(); string folderAll = Path.Combine(rootPath, outputFolder); if (!Directory.Exists(folderAll)) { Directory.CreateDirectory(folderAll); } // 操作pdf分割 using (PdfReader reader = new PdfReader(inputFilePath)) { for (int i = 1; i <= reader.NumberOfPages; i++) { string newFilePath = Path.Combine(outputFolder, $"page_{i}.pdf"); using (Document document = new Document()) using (PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create))) { document.Open(); copy.AddPage(copy.GetImportedPage(reader, i)); document.Close(); } } } Console.WriteLine("PDF 分割完成!");
文件合并
// 目標(biāo)合并pdf文件 string[] sourceFiles = new string[] { @"你的pdf文件1.pdf", @"你的pdf文件2.pdf" }; // 創(chuàng)建輸出文件所在文件夾 string outputFolder = "NewFile"; string rootPath = System.IO.Directory.GetCurrentDirectory(); string folderAll = Path.Combine(rootPath, outputFolder); if (!Directory.Exists(folderAll)) { Directory.CreateDirectory(folderAll); } using (Document document = new Document()) { PdfCopy copy = new PdfCopy(document, new FileStream($"{outputFolder}\\page_1_20_Add_21_40.pdf", FileMode.Create)); document.Open(); foreach (string file in sourceFiles) { using (PdfReader reader = new PdfReader(file)) { for (int i = 1; i <= reader.NumberOfPages; i++) { copy.AddPage(copy.GetImportedPage(reader, i)); } } } document.Close(); copy.Close(); }
多頁分割
根據(jù)分頁范圍進行分割文件,比如:1-10頁分割一個文件,即10頁分割一個文件
// 目標(biāo)分割pdf文件 string inputFilePath = @"你自己的pdf文件物理路徑.pdf"; // 創(chuàng)建輸出文件所在文件夾 string outputFolder = "NewFile"; string rootPath = System.IO.Directory.GetCurrentDirectory(); string folderAll = Path.Combine(rootPath, outputFolder); if (!Directory.Exists(folderAll)) { Directory.CreateDirectory(folderAll); } // 操作pdf分割 using (PdfReader reader = new PdfReader(inputFilePath)) { int startPage = 1; int pageSize = 0; int totalPage = 0; int unitSize = 20; int remainder = 0; totalPage = reader.NumberOfPages; pageSize = totalPage / unitSize; remainder = totalPage % unitSize; // 足夠20的分割文件 int currentIndex = 0; for (int index = 0; index < pageSize; index++) { currentIndex = (index + 1); using (Document document = new Document()) { int sv = (startPage + index * unitSize); int ev = ((index + 1) * unitSize); string newFilePath = Path.Combine(outputFolder, $"page_{sv}_{ev}.pdf"); PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create)); document.Open(); for (int i = sv; i <= ev; i++) { copy.AddPage(copy.GetImportedPage(reader, i)); } document.Close(); copy.Close(); } } // 不足20頁的文件 using (Document document = new Document()) { int sv = (startPage + pageSize * unitSize); int ev = (pageSize * unitSize + remainder); string newFilePath = Path.Combine(outputFolder, $"page_size_{sv}_{ev}.pdf"); PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create)); document.Open(); for (int i = sv; i <= ev; i++) { copy.AddPage(copy.GetImportedPage(reader, i)); } document.Close(); copy.Close(); } } }
插件說明
iTextSharp 是一個開源的 PDF 處理庫,用于在 C# 程序中創(chuàng)建、編輯和處理 PDF 文件。它提供了豐富的功能和 API,使開發(fā)者能夠進行各種 PDF 文件操作,包括創(chuàng)建 PDF、添加文本、插入圖片、設(shè)置頁面布局等功能。iTextSharp 庫基于 iText 庫的 C# 版本,是在 C# 平臺上操作 PDF 文件的常用工具之一。
以下是 iTextSharp 的一些基本功能:
1、創(chuàng)建 PDF 文件
使用 iTextSharp 可以在 C# 中輕松地創(chuàng)建新的 PDF 文件,可以通過代碼指定文檔結(jié)構(gòu)、頁面布局、文本樣式等。
2、編輯 PDF 文件內(nèi)容
可以向已有的 PDF 文件中添加文本、圖片、表格等內(nèi)容,也可以修改現(xiàn)有內(nèi)容,實現(xiàn)文檔內(nèi)容的動態(tài)更新。
3、處理 PDF 文件
iTextSharp 提供了豐富的 API,可以處理 PDF 文件中的文本、表格、圖形等元素,實現(xiàn)對 PDF 內(nèi)容的精確控制和調(diào)整。
4、設(shè)置頁面屬性
可以通過 iTextSharp 設(shè)置頁面尺寸、方向、邊距等屬性,定制化生成的 PDF 文檔格式。
4、添加水印和加密
可以在 PDF 文件中添加水印、數(shù)字簽名,也可以通過 iTextSharp 對 PDF 文件進行加密保護,確保 PDF 文件的安全性。
5、PDF 文件合并和拆分
iTextSharp 提供了合并多個 PDF 文件和拆分單個 PDF 文件的功能,方便進行文檔的整合和拆分操作。
總的來說,iTextSharp 是一個功能強大且靈活的 PDF 處理庫,可用于各種 PDF 文件的生成和處理需求。
通過使用 iTextSharp,開發(fā)者可以在 C# 程序中快速、高效地操作和處理 PDF 文件。
到此這篇關(guān)于基于C#實現(xiàn)PDF按頁分割文件和分頁合并的文章就介紹到這了,更多相關(guān)C# PDF分割與合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C#中delegate/event/EventHandler/Action/Func的使用和區(qū)別
這篇文章主要為大家詳細介紹了C#中delegate、event、EventHandler、Action和Func的使用與區(qū)別,文中的示例代碼講解詳細,感興趣的可以了解一下2023-04-04C#靜態(tài)代碼織入AOP組件之Rougamo的使用詳解
Rougamo是一個靜態(tài)代碼織入的AOP組件,同為AOP組件較為常用的有Castle、Autofac、AspectCore等,下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧2024-01-01C#調(diào)用RabbitMQ實現(xiàn)消息隊列的示例代碼
這篇文章主要介紹了C#調(diào)用RabbitMQ實現(xiàn)消息隊列的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12C# MeasureString測量字符串函數(shù)的使用方法
這篇文章主要介紹了C# MeasureString測量字符串函數(shù)的使用方法,需要的朋友可以參考下2014-10-10