目录
- 前言
- C# 裁剪PDF页面
- 拓展:C#实现pdf按页分割文件,以及分页合并
- 效果
- 单页分割
- 文件合并
- 多页分割
- 插件说明
前言
在处理PDF文档时,有时需要精确地裁剪页面以适应特定需求,比如去除广告、背景信息或者仅仅是为了简化文档内容。
本文将指导如何使用免费.NET控件通过C#实现裁剪PDF页面。
C# 裁剪PDF页面
Free Spire.PDF for .NET这个库提供了一个非常简单的接口来实现裁剪PDF页面指定区域,具体操作如下:
- 通过
LoadFromFile()方法加载PDF文档; - 获取指定PDF页面;
- 指定一个区域,然android后通过
PdfPageBase.CropBox属性裁剪指定区域; - 通过
SaveToFile()方法保存裁剪后的PDF文档。
示例代码如下:
using System.Drawing;
using Spire.Pdf;
namespace CropPDFPage
{
class Program
{
static void Main(string[] args)
{
//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("示例.pdf");
//获取第二页
PdfPageBase page = pdf.Pages[1];
//按指定区域裁剪PDF页面
page.CropBox = new RectangleF(270, 130, 400, 480);
//保存裁剪后的文档
pdf.SaveToFile("裁剪PDF.pdf");
pdf.Close();
}
}
}
裁剪前后对比:

拓展:C#实现pdf按页分割文件,以及分页合并
效果
下面就是通过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 (!Dire编程ctory.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 分割完成!");
文件合并
// 目标合并pphpdf文件
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}.http://www.devze.compdf");
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.Clhttp://www.devze.comose();
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 文档格式。
5、添加水印和加密
可以在 PDF 文件中添加水印、数字签名,也可以通过 iTextSharp 对 PDF 文件进行加密保护,确保 PDF 文件的安全性。
6、PDF 文件合并和拆分
iTextSharp 提供了合并多个 PDF 文件和拆分单个 PDF 文件的功能,方便进行文档的整合和拆分操作。
到此这篇关于通过C#实现裁剪PDF页面功能的文章就介绍到这了,更多相关C#裁剪PDF页面内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论