当前位置: 首页> 游戏> 网游 > 【C#】pdf按页分割文件,以及分页合并,效果还不错,你值得拥有

【C#】pdf按页分割文件,以及分页合并,效果还不错,你值得拥有

时间:2025/7/9 12:22:25来源:https://blog.csdn.net/lmy_520/article/details/139598420 浏览次数:0次

欢迎来到《小5讲堂》
这是《C#》系列文章,每篇文章将以博主理解的角度展开讲解。
温馨提示:博主能力有限,理解水平有限,若有不对之处望指正!

在这里插入图片描述

目录

  • 背景
  • 效果
  • 单页分割
  • 文件合并
  • 多页分割
  • 插件说明
  • 相关文章

背景

最近遇到一个文件上传限制大小问题,
因为有哪些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"
};// 创建输出文件所在文件夹
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 * 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# 程序中创建、编辑和处理 PDF 文件。它提供了丰富的功能和 API,使开发者能够进行各种 PDF 文件操作,包括创建 PDF、添加文本、插入图片、设置页面布局等功能。iTextSharp 库基于 iText 库的 C# 版本,是在 C# 平台上操作 PDF 文件的常用工具之一。
以下是 iTextSharp 的一些基本功能:
1、创建 PDF 文件
使用 iTextSharp 可以在 C# 中轻松地创建新的 PDF 文件,可以通过代码指定文档结构、页面布局、文本样式等。

2、编辑 PDF 文件内容
可以向已有的 PDF 文件中添加文本、图片、表格等内容,也可以修改现有内容,实现文档内容的动态更新。

3、处理 PDF 文件
iTextSharp 提供了丰富的 API,可以处理 PDF 文件中的文本、表格、图形等元素,实现对 PDF 内容的精确控制和调整。

4、设置页面属性
可以通过 iTextSharp 设置页面尺寸、方向、边距等属性,定制化生成的 PDF 文档格式。

4、添加水印和加密
可以在 PDF 文件中添加水印、数字签名,也可以通过 iTextSharp 对 PDF 文件进行加密保护,确保 PDF 文件的安全性。

5、PDF 文件合并和拆分
iTextSharp 提供了合并多个 PDF 文件和拆分单个 PDF 文件的功能,方便进行文档的整合和拆分操作。

总的来说,iTextSharp 是一个功能强大且灵活的 PDF 处理库,可用于各种 PDF 文件的生成和处理需求。
通过使用 iTextSharp,开发者可以在 C# 程序中快速、高效地操作和处理 PDF 文件。

相关文章

【C#】pdf按页分割文件,以及分页合并,效果还不错,你值得拥有

【C#】未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。找不到指定的模块。

【C#】.net core 6.0 在program时间格式统一json格式化,并列举program默认写法和简化写法

【C#】.net core 6.0 ApiController,API控制器方法,API接口以实体类作为接收参数应该注意的点

【C#】 SortedDictionary,查找字典中是否存在给定的关键字

【C#】.net core 6.0 MVC返回JsonResult显示API接口返回值不可被JSON反序列化

【C#】.net core 6.0 使用第三方日志插件Log4net,配置文件详细说明

【C#】使用代码实现龙年春晚扑克牌魔术(守岁共此时),代码实现篇

【C#】使用代码实现龙年春晚扑克牌魔术(守岁共此时),流程描述篇

【C#】约瑟夫原理举例2个代码实现

【C#】List泛型数据集如何循环移动,最后一位移动到第一位,以此类推

【C#】获取文本中的链接,通过正则表达式的方法获取以及优化兼容多种格式

温故而知新,不同阶段重温知识点,会有不一样的认识和理解,博主将巩固一遍知识点,并以实践方式和大家分享,若能有所帮助和收获,这将是博主最大的创作动力和荣幸。也期待认识更多优秀新老博主。

关键字:【C#】pdf按页分割文件,以及分页合并,效果还不错,你值得拥有

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: