在
(安装包仅提供部分功能,并设置限制,如需试用完整功能请申请免费授权。)
软件国产化服务季来啦!整合所有格式的Aspose.Total永久授权正在火热促销中,乐享85折起!联系客服立马1分钟了解全部咨询!
使用C#分割PDF文件
PDF拆分标准可以根据您的要求而变化。可以按每页或页面集合拆分文档。首先,让我们看一下如何分割PDF文件的每一页。
- 使用Document类加载PDF文档。
- 循环遍历Document.Pages集合,以使用Page类访问每个页面。
- 在每次迭代中,创建一个新Document,将当前页面添加到该文档中,然后使用Document.Save(String)方法将其另存为PDF文件。
以下代码示例显示了如何使用C#拆分PDF文档。
// Open documentDocument pdfDocument = new Document("merged.pdf");// For page counterint pageCount = 1;// Loop through all the pagesforeach (Aspose.Pdf.Page pdfPage in pdfDocument.Pages){ // Create a new documentDocument newDocument = new Document(); // Add page to the documentnewDocument.Pages.Add(pdfPage); // Save as PDFnewDocument.Save("page_" + pageCount + "_out" + ".pdf");pageCount++;}
使用C#分割PDF的选定页面
还可以通过指定页面范围来拆分PDF。例如,您可以分割第N个或最后N个数字页,偶数或奇数页等。为进行演示,以下是从PDF分割偶数和奇数页的步骤。
- 使用Document类加载PDF文档。
- 获取要拆分为Page []数组的页面。
- 创建一个新文档,并使用Document.Pages.Add(Page [])方法向其中添加页面。
- 使用Document.Save(String)方法保存PDF文件。
以下代码示例显示了如何从PDF拆分页面集合。
// Open documentDocument pdfDocument = new Document("merged.pdf"); // Select even pages onlyAspose.Pdf.Page[] evenPages = pdfDocument.Pages.Where(x => x.Number % 2 == 0).ToArray();// Select odd pages onlyAspose.Pdf.Page[] oddPages = pdfDocument.Pages.Where(x => x.Number % 2 != 0).ToArray();// Save even pages as PDFDocument newDocument = new Document();newDocument.Pages.Add(evenPages);newDocument.Save("split_even_Pages.pdf");// Save odd pages as PDFnewDocument = new Document();newDocument.Pages.Add(oddPages);newDocument.Save("split_odd_Pages.pdf");
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!