当前位置:   article > 正文

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

pdfsharp

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

一、PDF拆分界面

7ba266cb9effc5e0eae1f14f676c3be6.png

二、PDF拆分代码

//PDF拆分--添加文件

//添加文件表Listbox中,

  1. //PDF拆分--添加文件
  2. private void button5_Click(object sender, EventArgs e)
  3. {
  4. OpenFileDialog openFileDialog = new OpenFileDialog();
  5. openFileDialog.Multiselect = true; // 允许选择多个文件
  6. openFileDialog.Filter = "pdf Files (*.pdf)|*.pdf"; // 设置文件过滤器
  7. if (openFileDialog.ShowDialog() == DialogResult.OK) // 如果用户选择取消或者关闭,ShowDialog会返回Cancel
  8. {
  9. listBox2.Items.Clear();
  10. foreach (string file in openFileDialog.FileNames)
  11. {
  12. listBox2.Items.Add(file); // 将文件路径添加到Listbox中
  13. }
  14. }
  15. }

//PDF拆分---删除button

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

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

//PDF拆分-输出目录Button

拆分后要输出的文件目录

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

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

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

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

  1. //PDF拆分---执行拆分
  2. private void button8_Click(object sender, EventArgs e)
  3. {
  4. if (string.IsNullOrEmpty(textBox2.Text))
  5. {
  6. return;
  7. }
  8. string outputDirectory = textBox2.Text;
  9. // 确保输出目录存在
  10. if (!Directory.Exists(outputDirectory))
  11. {
  12. Directory.CreateDirectory(outputDirectory);
  13. }
  14. // 指定每个PDF文件需要拆分的页数
  15. int pagesPerDocument =(int)numericUpDown1.Value; // 例如,每个文档拆分为5页
  16. //int pagesPerDocument = 5; // 例如,每个文档拆分为5页
  17. // 遍历ListBox中的所有PDF文件
  18. foreach (string pdfFile in listBox2.Items)
  19. {
  20. // if (!(pdfFile is string filePath)) continue; // 确保ListBox中的所有项都是字符串类型的文件路径
  21. // 读取PDF文件
  22. using (PdfDocument document = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import))
  23. {
  24. int pageCount = document.PageCount;
  25. int pagesCopied = 0;
  26. // 计算需要拆分的次数
  27. int splitsNeeded = (pageCount + pagesPerDocument - 1) / pagesPerDocument;
  28. for (int i = 0; i < splitsNeeded; i++)
  29. {
  30. //MessageBox.Show(pdfFile + "" + i.ToString());
  31. int startPage = i * pagesPerDocument + 1;
  32. int endPage = Math.Min(startPage + pagesPerDocument - 1, pageCount);
  33. // 创建一个新的PDF文档用于保存这些页面
  34. using (PdfDocument singlePageDocument = new PdfDocument())
  35. {
  36. for (int j = startPage; j <= endPage; j++)
  37. {
  38. PdfPage page = document.Pages[j-1];
  39. singlePageDocument.AddPage(page);
  40. pagesCopied++;
  41. }
  42. string outputFilePath = Path.Combine(outputDirectory, $"{Path.GetFileNameWithoutExtension(pdfFile)}_{startPage}-{endPage}.pdf");
  43. singlePageDocument.Save(outputFilePath);
  44. }
  45. //输出进度或状态信息MessageBox.Show($"从 {filePath} 拆分了 {pagesCopied} 页并保存为 {outputFilePath}");
  46. }
  47. }
  48. }
  49. MessageBox.Show("所有PDF文件的拆分已完成。");
  50. }

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号