在C#中,PDFsharp库使用(二):PDF拆分

PDFsharp 是一个流行的 C# 库,用于创建和处理 PDF 文档。它提供了一套丰富的 API,允许你以编程方式生成、编辑和渲染 PDF 文件

一、PDF拆分界面

7ba266cb9effc5e0eae1f14f676c3be6.png

二、PDF拆分代码

//PDF拆分--添加文件

//添加文件表Listbox中,

//PDF拆分--添加文件
private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;  // 允许选择多个文件
            openFileDialog.Filter = "pdf Files (*.pdf)|*.pdf";  // 设置文件过滤器


if (openFileDialog.ShowDialog() == DialogResult.OK)  // 如果用户选择取消或者关闭,ShowDialog会返回Cancel
            {
                listBox2.Items.Clear();
foreach (string file in openFileDialog.FileNames)
                {
                    listBox2.Items.Add(file);  // 将文件路径添加到Listbox中
                }
            }
        }

//PDF拆分---删除button

//对Listbox中的列表进行操作删除

//PDF拆分---删除button
private void button6_Click(object sender, EventArgs e)
        {
// 确保至少有一个项被选中
if (listBox2.SelectedItems.Count > 0)
            {
// 删除选定的项
                listBox2.Items.Remove(listBox2.SelectedItems[0]);
            }
        }

//PDF拆分-输出目录Button

拆分后要输出的文件目录

//PDF拆分-输出目录
private void button7_Click(object sender, EventArgs e)
        {
string folderPath = SelectFolder();
if (folderPath != null)
            {
                textBox2.Text = folderPath;
// 在这里处理文件夹路径,例如读取文件或执行其他操作  
            }


        }

//PDF拆分---执行拆分Button

//读取Listbox的列表,循环列表,按x页/每个文档的方式拆分,

如:按3页/每个文档 ,将输出:原文件名_1_3.pdf、原文件名_4_6.pdf...

//PDF拆分---执行拆分
private void button8_Click(object sender, EventArgs e)
        {
if (string.IsNullOrEmpty(textBox2.Text))
            {
return;
            }
string outputDirectory = textBox2.Text;
// 确保输出目录存在
if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }


// 指定每个PDF文件需要拆分的页数
int pagesPerDocument =(int)numericUpDown1.Value; // 例如,每个文档拆分为5页
//int pagesPerDocument = 5; // 例如,每个文档拆分为5页


// 遍历ListBox中的所有PDF文件
foreach (string pdfFile in listBox2.Items)
            {


// if (!(pdfFile is string filePath)) continue; // 确保ListBox中的所有项都是字符串类型的文件路径


// 读取PDF文件
using (PdfDocument document = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import))
                {
int pageCount = document.PageCount;
int pagesCopied = 0;


// 计算需要拆分的次数
int splitsNeeded = (pageCount + pagesPerDocument - 1) / pagesPerDocument;


for (int i = 0; i < splitsNeeded; i++)
                    {
//MessageBox.Show(pdfFile + "" + i.ToString());
int startPage = i * pagesPerDocument + 1;
int endPage = Math.Min(startPage + pagesPerDocument - 1, pageCount);
// 创建一个新的PDF文档用于保存这些页面
using (PdfDocument singlePageDocument = new PdfDocument())
                        {
for (int j = startPage; j <= endPage; j++)
                            {
                                PdfPage page = document.Pages[j-1];
                                singlePageDocument.AddPage(page);
                                pagesCopied++;
                            }
string outputFilePath = Path.Combine(outputDirectory, $"{Path.GetFileNameWithoutExtension(pdfFile)}_{startPage}-{endPage}.pdf");
                            singlePageDocument.Save(outputFilePath);
                        }
//输出进度或状态信息MessageBox.Show($"从 {filePath} 拆分了 {pagesCopied} 页并保存为 {outputFilePath}");
                    }
                }






            }
            MessageBox.Show("所有PDF文件的拆分已完成。");
        }

若对你有帮助,记得分享给大家,免费学习哦
公众号发信息“PDF”, 可获取此免费PDF工具

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值