翻譯|使用教程|編輯:李顯亮|2020-11-09 10:44:45.280|閱讀 807 次
概述:在某些情況下,需要將單個PDF文件拆分為多個文件。在本文中,將學習如何應對此類情況,以及如何使用C#將PDF文件拆分為多個PDF。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在之前一篇文章中,已經介紹了如何將多個PDF文件合并為一個PDF。但是,在某些情況下,需要將單個PDF文件拆分為多個文件。可以將PDF的每一頁或頁面集合拆分為多個PDF。在本文中,將學習如何應對此類情況,以及如何使用C#將PDF文件拆分為多個PDF。
	(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
 
使用C#分割PDF文件
PDF拆分標準可以根據您的要求而變化。可以按每頁或頁面集合拆分文檔。首先,讓我們看一下如何分割PDF文件的每一頁。
以下代碼示例顯示了如何使用C#拆分PDF文檔。
// Open document
Document pdfDocument = new Document("merged.pdf");
// For page counter
int pageCount = 1;
// Loop through all the pages
foreach (Aspose.Pdf.Page pdfPage in pdfDocument.Pages)
{
  	// Create a new document
	Document newDocument = new Document();
  
  	// Add page to the document
	newDocument.Pages.Add(pdfPage);
  
  	// Save as PDF 
	newDocument.Save("page_" + pageCount + "_out" + ".pdf");
	pageCount++;
}
使用C#分割PDF的選定頁面
還可以通過指定頁面范圍來拆分PDF。例如,您可以分割第N個或最后N個數字頁,偶數或奇數頁等。為進行演示,以下是從PDF分割偶數和奇數頁的步驟。
以下代碼示例顯示了如何從PDF拆分頁面集合。
// Open document
Document pdfDocument = new Document("merged.pdf"); 
// Select even pages only
Aspose.Pdf.Page[] evenPages = pdfDocument.Pages.Where(x => x.Number % 2 == 0).ToArray();
// Select odd pages only
Aspose.Pdf.Page[] oddPages = pdfDocument.Pages.Where(x => x.Number % 2 != 0).ToArray();
// Save even pages as PDF
Document newDocument = new Document();
newDocument.Pages.Add(evenPages);
newDocument.Save("split_even_Pages.pdf");
// Save odd pages as PDF
newDocument = new Document();
newDocument.Pages.Add(oddPages);
newDocument.Save("split_odd_Pages.pdf");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn