当前位置:   article > 正文

C# 实现某几页PDF 分割合并成新的PDF_c# pdf拆分页

c# pdf拆分页
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. public partial class PdfItxt : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. int[] pagelist = new int[] { 4, 8 };
  15. ExtractPages(Server.MapPath("1.pdf"), Server.MapPath("/dd"), pagelist);
  16. }
  17. public void ExtractPages(string sourcePdfPath, string outputPdfPath, int[] pagelist)
  18. {
  19. PdfReader reader = null;
  20. Document sourceDocument = null;
  21. PdfCopy pdfCopyProvider = null;
  22. PdfImportedPage importedPage = null;
  23. try
  24. {
  25. reader = new PdfReader(sourcePdfPath);
  26. sourceDocument = new Document(reader.GetPageSizeWithRotation(1));
  27. pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath + "\\1007.pdf", System.IO.FileMode.Create));
  28. sourceDocument.Open();
  29. for (int i = 1; i <= reader.NumberOfPages; i++)
  30. {
  31. for (int j = 0; j < pagelist.Length; j++)
  32. if (i == pagelist[j])
  33. {
  34. importedPage = pdfCopyProvider.GetImportedPage(reader, i);
  35. pdfCopyProvider.AddPage(importedPage);
  36. }
  37. }
  38. sourceDocument.Close();
  39. reader.Close();
  40. }
  41. catch (Exception ex) { throw ex; }
  42. }
  43. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1018964
推荐阅读
相关标签
  

闽ICP备14008679号