背景
最近遇到一个文件上传限制大小问题,
因为有哪些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 文件的功能,方便进行文档的整合和拆分操作。
相关文章
【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#】使用代码实现龙年春晚扑克牌魔术(守岁共此时),流程描述篇
发表评论