基于C#实现PDF按页分割文件和分页合并
目录
- 背景
- 效果
- 单页分割
- 文件合并
- 多页分割
- 插件说明
背景
最近遇到一个文件上传限制大小问题,
因为有哪些pdf文件可能有300多页,大小已经有100MB,
但是有些文件上传限制大小在10MB以内,
因为本篇文章将简单讲讲如何将大文件通过分页分割和合并。
效果
下面就是通过pdf插件进行按页进行文件分割输出
单页分割
插件命名空间
using iTextSharp.text; using iTextSharp.text.pdf;
目标分割pdf文件、创建输出文件所在的文件夹、iTextSharp插件操作pdf分割
// 目标分割pdf文件 string inputFilePath = @"你自己的pdf文件物理路径.pdf"; // 创建输出文件所在文件夹 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 分割完成!");
文件合并
// 目标合并pdf文件 string[] sourceFiles = new string[] { @"你的pdf文件1.pdf", @"你的pdf文件2.pdf" }; // 创建输出http://www.devze.com文件所在文件夹 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(); }
多页分割
根据分页范围进行分割文件,比如:1-10页分割一个文件,即10页分割一个文件
// 目标分割pdf文件 string inputFilePath = @"你自己的pdf文件物理路径.pdf"; // 创建输出文件所在文件夹 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 * unitpythonSize + 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# 程序中创建、编辑和处理 PDF 文件。它提供了丰富的功能和 API,使开发者能够进行各种 PDF 文件操作,包括创建 PDF、添加文本、插入图片、设置页面布局等功能。iTextSharp 库基于 iText 库的 C# 版本,是在 C# 平台上操作 PDF 文件的常用工具之一。
以下是 iThttp://www.devze.comextSharp 的一些基本功能:
1、创建 PDF 文件
使用 iTextSharp 可以在 C# 中轻松地创建新的 PDF 文件,可以通过代码指定文档结构、页面布局、文本样式等。
2、编辑 PDF 文件内容
可以向已有的 PDF 文件中添加文本、图片、表格等内容,也可以修改现有内容,实现文档内容的动态更新。
3、处理 PDF 文件
iTextSharp 提供了丰富的 API,可以处理 PDF 文件中的文本、表格、图形等元素,实现对 PDF 内容的精确控制和调整。
4、设置页面属性
可以通过 iTextSharp 设置页面尺寸、方向、边距等属性,定制化生成的 PDF 文档格式。
4、添加水印和加密
可以在 PDF 文件中添加水印、数字签名,也可以通过 iTextSharp 对 PDF 文件进行加密保护,javascript确保 PDF编程客栈 文件的安全性。
5、PDF 文件合并和拆分
iTextSharp 提供了合并多个 PDF 文件和拆分单个 PDF 文件的功能,方便进行文档的整合和拆分操作。
总的来说,iTextSharp 是一个功能强大且灵活的 PDF 处理库,可用于各种 PDF 文件的生成和处理需求。
通过使用 iTextSharp,开发者可以在 C# 程序中快速、高效地操作和处理 PDF 文件。
到此这篇关于基于C#实现PDF按页分割文件和分页合并的文章就介绍到这了,更多相关C# PDF分割与合并内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论