前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在C#中,PDFsharp库使用(二):PDF拆分

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

作者头像
哆哆Excel
发布2024-04-18 19:45:35
2330
发布2024-04-18 19:45:35
举报
文章被收录于专栏:哆哆Excel哆哆Excel

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

一、PDF拆分界面

二、PDF拆分代码

//PDF拆分--添加文件

//添加文件表Listbox中,

代码语言:javascript
复制
//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中的列表进行操作删除

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

//PDF拆分-输出目录Button

拆分后要输出的文件目录

代码语言:javascript
复制
//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...

代码语言:javascript
复制
//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文件的拆分已完成。");
        }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-04-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档