如何以程式方式重新排列PDF頁面在C#中
!How to Rearrange Pages in PDF C# With IronPDF:圖片 1 - 在 PDF C# 中重新排列頁面</a
使用 C# 重新排列 PDF 文件中的頁面,可以省去您在交付前重新組織報告、重新排序合約附件或重建文件包時數小時的手動工作。 IronPDF提供了一個簡單的 API,只需幾行 .NET 程式碼即可載入 PDF、指定新的頁面順序並儲存結果。 本文介紹了五種實用技巧:基本頁面重新排序、批量反轉、將單一頁面移至新索引、刪除不需要的頁面以及完全在記憶體中工作而不觸及檔案系統。
IronPdf.PdfDocument.FromFile("input.pdf")
.CopyPages(new[] { 2, 0, 1, 3 })
.SaveAs("reordered.pdf");
IronPdf.PdfDocument.FromFile("input.pdf")
.CopyPages(new[] { 2, 0, 1, 3 })
.SaveAs("reordered.pdf");
Imports IronPdf
PdfDocument.FromFile("input.pdf") _
.CopyPages({2, 0, 1, 3}) _
.SaveAs("reordered.pdf")
!{--010011000100100101000010010100100100000101010010010110010101111101001110010101010101010101010101010101010101010 0100010111110100100101001101010100010000010100110001001100010111110100001001001100010011110010101010
如何開始使用 IronPDF?
使用 NuGet 套件管理器或 .NET CLI,即可在幾秒鐘內將 IronPDF 新增至任何 .NET 8 或 .NET 10 專案。 Windows、Linux 或 macOS 系統上無需額外的執行時間依賴項或本機二進位。
dotnet add package IronPdf
dotnet add package IronPdf
安裝完成後,在 C# 檔案頂部新增 using IronPdf;。有效的許可證密鑰可解鎖完整的商業用途; 提供免費試用許可證供評估。 在呼叫任何 API 之前請先設定金鑰:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
引用了相關軟體包並配置了許可證後,本文中的每個範例都無需修改即可運行。 IronPDF NuGet 套件適用於 .NET Standard 2.0 及更高版本,因此可在 .NET Framework 4.6.2+、.NET Core 和所有現代 .NET 版本中使用。
C# 中頁面重新排序是如何運作的?
使用 C# 重新排列 PDF 中的頁面,需要載入來源文檔,透過頁面索引陣列指定所需的頁面順序,然後儲存輸出文件。 IronPDF 提供了一個方法,從 PDF 中提取頁面並將其重新排序到新的物件中。
以下程式碼示範如何透過建立一個新的 int 陣列來重新排列頁面,該陣列定義了目標序列。 陣列中的每個值代表原始文件中的頁面索引,其中頁面使用基於零的索引(第 0 頁為第一頁)。
using IronPdf;
// Load the source document from file path
var pdf = PdfDocument.FromFile("quarterly-report.pdf");
// Define new page order: move page 3 to front, then pages 1, 2, 0
int[] pageOrder = new int[] { 3, 1, 2, 0 };
// Copy each requested page into its own PdfDocument
var pageDocs = new List<PdfDocument>();
foreach (var idx in pageOrder)
{
// CopyPage returns a PdfDocument containing only that page
var single = pdf.CopyPage(idx);
pageDocs.Add(single);
}
// Merge the single-page docs into one ordered document
using var merged = PdfDocument.Merge(pageDocs.ToArray());
// Save the new ordered PDF
merged.SaveAs("report-reorganized.pdf");
using IronPdf;
// Load the source document from file path
var pdf = PdfDocument.FromFile("quarterly-report.pdf");
// Define new page order: move page 3 to front, then pages 1, 2, 0
int[] pageOrder = new int[] { 3, 1, 2, 0 };
// Copy each requested page into its own PdfDocument
var pageDocs = new List<PdfDocument>();
foreach (var idx in pageOrder)
{
// CopyPage returns a PdfDocument containing only that page
var single = pdf.CopyPage(idx);
pageDocs.Add(single);
}
// Merge the single-page docs into one ordered document
using var merged = PdfDocument.Merge(pageDocs.ToArray());
// Save the new ordered PDF
merged.SaveAs("report-reorganized.pdf");
Imports IronPdf
' Load the source document from file path
Dim pdf As PdfDocument = PdfDocument.FromFile("quarterly-report.pdf")
' Define new page order: move page 3 to front, then pages 1, 2, 0
Dim pageOrder As Integer() = {3, 1, 2, 0}
' Copy each requested page into its own PdfDocument
Dim pageDocs As New List(Of PdfDocument)()
For Each idx In pageOrder
' CopyPage returns a PdfDocument containing only that page
Dim single As PdfDocument = pdf.CopyPage(idx)
pageDocs.Add(single)
Next
' Merge the single-page docs into one ordered document
Using merged As PdfDocument = PdfDocument.Merge(pageDocs.ToArray())
' Save the new ordered PDF
merged.SaveAs("report-reorganized.pdf")
End Using
輸出 PDF 文件

CopyPages 方法接受一個 IEnumerable<int> 的頁面索引值,這些值與您期望的排列方式相符。 這種方法可以讓你重新排列 PDF 頁面、複製特定頁面,或是將部分頁面提取到單獨的文件中。 此方法傳回一個新的 PdfDocument 對象,而原始來源文件保持不變。 因為原始檔案永遠不會被修改,所以您可以安全地多次呼叫 CopyPages,從同一個來源檔案產生不同的排序。
對於在 Java 環境中工作的團隊, IronPDF 適用於 Java提供了類似的頁面操作方法和相容的 API 接口,因此技能可以跨語言目標進行轉移。
你如何理解基於零的頁面索引?
IronPDF 在其 API 中全程使用從零開始的頁面索引。 第 0 頁是第一頁,第 1 頁是第二頁,依此類推。 建立索引數組時,要從零開始計數,而不是從一開始計數。 超出範圍的索引會拋出ArgumentOutOfRangeException錯誤,因此在生產代碼中調用 CopyPages 之前,務必先根據 PdfDocument.PageCount 驗證數組值。
安全的驗證模式是在將陣列傳遞給任何頁面複製方法之前,檢查陣列中的每個索引是否都在 [0, PageCount - 1] 範圍內。 這樣可以防止在處理步驟之間輸入文件形狀變化的情況下出現運行時異常。
如何一次重新排列多個頁面?
當 PDF 文件包含很多頁時,您可以一次重新排列整個結構。 下面的程式碼展示如何透過以程式設計方式計算索引數組來反轉所有頁面或建立任何自訂序列。
using IronPdf;
// Load PDF document with several pages
var doc = PdfDocument.FromFile("quarterly-report.pdf");
int count = doc.PageCount;
// Build reversed single-page PDFs
var pages = new List<PdfDocument>();
for (int i = count - 1; i >= 0; i--)
{
// Copy a single page as a standalone PdfDocument
pages.Add(doc.CopyPage(i));
}
// Merge all the reversed single-page PDFs
using var reversed = PdfDocument.Merge(pages.ToArray());
// Save to a new filename
reversed.SaveAs("report-reversed.pdf");
using IronPdf;
// Load PDF document with several pages
var doc = PdfDocument.FromFile("quarterly-report.pdf");
int count = doc.PageCount;
// Build reversed single-page PDFs
var pages = new List<PdfDocument>();
for (int i = count - 1; i >= 0; i--)
{
// Copy a single page as a standalone PdfDocument
pages.Add(doc.CopyPage(i));
}
// Merge all the reversed single-page PDFs
using var reversed = PdfDocument.Merge(pages.ToArray());
// Save to a new filename
reversed.SaveAs("report-reversed.pdf");
Imports IronPdf
' Load PDF document with several pages
Dim doc = PdfDocument.FromFile("quarterly-report.pdf")
Dim count As Integer = doc.PageCount
' Build reversed single-page PDFs
Dim pages As New List(Of PdfDocument)()
For i As Integer = count - 1 To 0 Step -1
' Copy a single page as a standalone PdfDocument
pages.Add(doc.CopyPage(i))
Next
' Merge all the reversed single-page PDFs
Using reversed = PdfDocument.Merge(pages.ToArray())
' Save to a new filename
reversed.SaveAs("report-reversed.pdf")
End Using
反向 PDF 頁面輸出
。
這段程式碼載入一個 PDF 文件,查詢 PageCount,並建構一個反轉頁面順序的清單。 for 循環動態地建立新順序,使該方法能夠擴展到任何長度的文檔。 您可以將此模式應用於任何非平凡的排序:按元資料字母順序排序、從多個來源提取單一頁面時按檔案大小排序,或對匿名測試資料進行隨機排序。
您也可以只交換兩頁,而無需重建整個清單。例如,要在三頁文件中交換第 0 頁和第 2 頁,同時保持第 1 頁不變,請將 new int[] { 2, 1, 0 } 作為索引數組傳遞。 IronPDF 頁面操作文件包含複製、插入和刪除頁面的更多範例。
如何高效處理大型文件?
對於有數百頁的文檔,在緊密循環中調用 CopyPage 會分配許多中間對象。 更有效的替代方法是建立一次完整的索引數組,然後直接將其傳遞給 CopyPages。 CopyPages(IEnumerable<int>) 重載在一次內部遍歷中執行整個重新排序,這比合併單獨複製的頁面更快,並且使用的記憶體更少。
處理大量 PDF 時,請考慮使用 PdfDocument 語句或明確 using 呼叫及時處置中間 Dispose() 物件。 .NET 垃圾回收器會自動管理內存,但及時釋放非託管資源可以降低高吞吐量服務的峰值內存使用量。
如何將單一頁面移動到新位置?
將一頁紙移動到不同的位置需要結合複製、刪除和插入操作。 InsertPdf 方法將 PdfDocument 放置在目標文件中的任何索引處。
using IronPdf;
// Load the input PDF file
var pdf = PdfDocument.FromFile("presentation.pdf");
int sourceIndex = 1; // page to move
int targetIndex = 3; // destination position
// Track direction to handle index shift after removal
bool movingForward = targetIndex > sourceIndex;
// 1. Copy the page to move (produces a one-page PdfDocument)
var pageDoc = pdf.CopyPage(sourceIndex);
// 2. Remove the original page from its current position
pdf.RemovePage(sourceIndex);
// 3. Adjust target index if moving forward (removal shifts remaining pages left)
if (movingForward)
targetIndex--;
// 4. Insert the copied page at the target position
pdf.InsertPdf(pageDoc, targetIndex);
// Save the result
pdf.SaveAs("presentation-reordered.pdf");
using IronPdf;
// Load the input PDF file
var pdf = PdfDocument.FromFile("presentation.pdf");
int sourceIndex = 1; // page to move
int targetIndex = 3; // destination position
// Track direction to handle index shift after removal
bool movingForward = targetIndex > sourceIndex;
// 1. Copy the page to move (produces a one-page PdfDocument)
var pageDoc = pdf.CopyPage(sourceIndex);
// 2. Remove the original page from its current position
pdf.RemovePage(sourceIndex);
// 3. Adjust target index if moving forward (removal shifts remaining pages left)
if (movingForward)
targetIndex--;
// 4. Insert the copied page at the target position
pdf.InsertPdf(pageDoc, targetIndex);
// Save the result
pdf.SaveAs("presentation-reordered.pdf");
Imports IronPdf
' Load the input PDF file
Dim pdf = PdfDocument.FromFile("presentation.pdf")
Dim sourceIndex As Integer = 1 ' page to move
Dim targetIndex As Integer = 3 ' destination position
' Track direction to handle index shift after removal
Dim movingForward As Boolean = targetIndex > sourceIndex
' 1. Copy the page to move (produces a one-page PdfDocument)
Dim pageDoc = pdf.CopyPage(sourceIndex)
' 2. Remove the original page from its current position
pdf.RemovePage(sourceIndex)
' 3. Adjust target index if moving forward (removal shifts remaining pages left)
If movingForward Then
targetIndex -= 1
End If
' 4. Insert the copied page at the target position
pdf.InsertPdf(pageDoc, targetIndex)
' Save the result
pdf.SaveAs("presentation-reordered.pdf")
原始 PDF 對比輸出
。
該演算法複製來源頁面,將其從文件中刪除(這將使所有後續頁面索引向下移動一位),調整目標索引以適應該偏移,然後將頁面插入修正後的位置。 這種模式可以正確處理向前和向後的移動。 當您需要對一兩頁進行精確控制,而無需重建整個文件時,可以使用此功能。
對於需要從第二個 PDF 插入內容而不是移動現有頁面的情況,InsertPdf 接受任何 PdfDocument 作為其第一個參數,包括使用IronPDF HTML-to-PDF API從 HTML 生成的文件。
如何使用 MemoryStream 刪除頁面並重新排序?
實現 PDF 工作流程自動化的應用程式有時需要在不將中間文件寫入磁碟的情況下處理文件。 從位元組數組載入並匯出到 MemoryStream 可以將所有處理保持在記憶體中,這對於瞬態操作來說速度更快,並且可以避免容器化或無伺服器環境中的檔案系統權限問題。
using IronPdf;
using System.IO;
// Load PDF from byte array (simulating input from a database or API response)
byte[] pdfBytes = File.ReadAllBytes("report-with-blank.pdf");
var pdf = new PdfDocument(pdfBytes);
// Delete the blank page at index 2 (zero-based)
pdf.RemovePage(2);
// Reorder remaining pages: new sequence from a four-page document
var reorderedPdf = pdf.CopyPages(new int[] { 1, 0, 2, 3 });
// Export to MemoryStream for further processing (e.g., HTTP response body)
MemoryStream outputStream = reorderedPdf.Stream;
// Or save directly using the BinaryData property
File.WriteAllBytes("cleaned-report.pdf", reorderedPdf.BinaryData);
using IronPdf;
using System.IO;
// Load PDF from byte array (simulating input from a database or API response)
byte[] pdfBytes = File.ReadAllBytes("report-with-blank.pdf");
var pdf = new PdfDocument(pdfBytes);
// Delete the blank page at index 2 (zero-based)
pdf.RemovePage(2);
// Reorder remaining pages: new sequence from a four-page document
var reorderedPdf = pdf.CopyPages(new int[] { 1, 0, 2, 3 });
// Export to MemoryStream for further processing (e.g., HTTP response body)
MemoryStream outputStream = reorderedPdf.Stream;
// Or save directly using the BinaryData property
File.WriteAllBytes("cleaned-report.pdf", reorderedPdf.BinaryData);
Imports IronPdf
Imports System.IO
' Load PDF from byte array (simulating input from a database or API response)
Dim pdfBytes As Byte() = File.ReadAllBytes("report-with-blank.pdf")
Dim pdf As New PdfDocument(pdfBytes)
' Delete the blank page at index 2 (zero-based)
pdf.RemovePage(2)
' Reorder remaining pages: new sequence from a four-page document
Dim reorderedPdf = pdf.CopyPages(New Integer() {1, 0, 2, 3})
' Export to MemoryStream for further processing (e.g., HTTP response body)
Dim outputStream As MemoryStream = reorderedPdf.Stream
' Or save directly using the BinaryData property
File.WriteAllBytes("cleaned-report.pdf", reorderedPdf.BinaryData)
PdfDocument 建構子直接接受一個 byte[],而 Stream 屬性傳回產生的 PDF 作為 MemoryStream。 此模式適用於傳回檔案回應的ASP.NET Core控制器、讀取和寫入 Blob 儲存體的 Azure 函數以及從訊息佇列處理批次 PDF 的背景服務。 即使對於包含嵌入式影像的大型文檔,該函式庫也能有效率地處理記憶體管理。
請參閱PdfDocument API 參考文檔,以了解完整的頁面操作方法集,包括旋轉、擷取和蓋章。
如何在 ASP.NET Core 控制器中處理 PDF 頁面?
從控制器直接返回重新排序的 PDF 檔案非常簡單。 對已載入的文件呼叫 RemovePage 或 CopyPages,然後將 BinaryData 寫入回應:
using IronPdf;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/pdf")]
public class PdfController : ControllerBase
{
[HttpPost("reorder")]
public IActionResult Reorder(IFormFile file, [FromQuery] string order)
{
// Parse comma-separated page indexes from query string
var indexes = order.Split(',').Select(int.Parse).ToArray();
using var stream = file.OpenReadStream();
using var ms = new System.IO.MemoryStream();
stream.CopyTo(ms);
var pdf = new PdfDocument(ms.ToArray());
var reordered = pdf.CopyPages(indexes);
// Return the reordered PDF as a downloadable file
return File(reordered.BinaryData, "application/pdf", "reordered.pdf");
}
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/pdf")]
public class PdfController : ControllerBase
{
[HttpPost("reorder")]
public IActionResult Reorder(IFormFile file, [FromQuery] string order)
{
// Parse comma-separated page indexes from query string
var indexes = order.Split(',').Select(int.Parse).ToArray();
using var stream = file.OpenReadStream();
using var ms = new System.IO.MemoryStream();
stream.CopyTo(ms);
var pdf = new PdfDocument(ms.ToArray());
var reordered = pdf.CopyPages(indexes);
// Return the reordered PDF as a downloadable file
return File(reordered.BinaryData, "application/pdf", "reordered.pdf");
}
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
<ApiController>
<Route("api/pdf")>
Public Class PdfController
Inherits ControllerBase
<HttpPost("reorder")>
Public Function Reorder(file As IFormFile, <FromQuery> order As String) As IActionResult
' Parse comma-separated page indexes from query string
Dim indexes = order.Split(","c).Select(Function(s) Integer.Parse(s)).ToArray()
Using stream = file.OpenReadStream()
Using ms = New MemoryStream()
stream.CopyTo(ms)
Dim pdf = New PdfDocument(ms.ToArray())
Dim reordered = pdf.CopyPages(indexes)
' Return the reordered PDF as a downloadable file
Return File(reordered.BinaryData, "application/pdf", "reordered.pdf")
End Using
End Using
End Function
End Class
此控制器讀取上傳的文件,接受以逗號分隔的查詢字串指定的頁面順序,並將重新排序的 PDF 作為回應串流傳回。 整個過程中不會寫入任何臨時檔案。 同樣的方法也適用於在 ASP.NET Core 中從 HTML 或模板產生 PDF 。
重新排序後如何合併PDF?
頁面重新排序通常與合併來自多個來源檔案的內容密切相關。 IronPDF 的 PdfDocument.Merge 方法接受一個 PdfDocument 物件數組,並按提供的順序將它們連接起來,這與上面顯示的頁面複製技術自然地結合在一起。
using IronPdf;
// Load two separate PDF files
var docA = PdfDocument.FromFile("section-a.pdf");
var docB = PdfDocument.FromFile("section-b.pdf");
// Reorder pages within each source document
var reorderedA = docA.CopyPages(new int[] { 1, 0, 2 });
var reorderedB = docB.CopyPages(new int[] { 0, 2, 1 });
// Merge into a single output document
using var combined = PdfDocument.Merge(reorderedA, reorderedB);
combined.SaveAs("combined-report.pdf");
using IronPdf;
// Load two separate PDF files
var docA = PdfDocument.FromFile("section-a.pdf");
var docB = PdfDocument.FromFile("section-b.pdf");
// Reorder pages within each source document
var reorderedA = docA.CopyPages(new int[] { 1, 0, 2 });
var reorderedB = docB.CopyPages(new int[] { 0, 2, 1 });
// Merge into a single output document
using var combined = PdfDocument.Merge(reorderedA, reorderedB);
combined.SaveAs("combined-report.pdf");
Imports IronPdf
' Load two separate PDF files
Dim docA = PdfDocument.FromFile("section-a.pdf")
Dim docB = PdfDocument.FromFile("section-b.pdf")
' Reorder pages within each source document
Dim reorderedA = docA.CopyPages(New Integer() {1, 0, 2})
Dim reorderedB = docB.CopyPages(New Integer() {0, 2, 1})
' Merge into a single output document
Using combined = PdfDocument.Merge(reorderedA, reorderedB)
combined.SaveAs("combined-report.pdf")
End Using
合併後,產生的文件包含來自 reorderedA 的所有頁面,以及來自 reorderedB 的所有頁面。 您可以連結其他 Merge 呼叫或傳遞更多文件來組合任意數量的來源。 IronPDF 的文件合併和分割功能涵蓋將文件分割為各個部分,這是相反的操作。
要深入了解如何合併位元組數組和記憶體中的文檔,請參閱C# 中關於從字節數組合併 PDF 的指南,其中文檔以二進制 blob 而不是文件路徑的形式存儲,該指南涵蓋了資料庫支援的工作流程。
PDF頁面管理的最佳實務是什麼?
防禦性編碼習慣可以防止在大規模操作 PDF 頁面時出現不易察覺的錯誤和生產故障。 持續應用這些做法,可以使頁面重排序程式碼更容易測試和維護。
在將頁面索引傳遞給 CopyPages 或 RemovePage 之前,請務必先根據 PdfDocument.PageCount 驗證頁面索引。 索引超出 [0, PageCount - 1] 範圍會在執行時間引發 ArgumentOutOfRangeException 錯誤。一行簡單的保護檢查即可完全消除此類錯誤。
處理多頁重新排序時,優先使用 CopyPages(IEnumerable<int>) 重載,而不是手動循環遍歷 CopyPage。 批次處理模式一次處理整個序列,從而減少記憶體分配和執行時間。逐頁循環模式僅適用於需要進行逐頁轉換的情況,例如在合併前輪換單一頁面。
將中間 PdfDocument 物件包裝在 using 語句中,以確保其未託管資源在使用後立即釋放。 這在 Web 請求處理程序和後台作業中尤其重要,因為如果中間物件沒有及時釋放,許多文件可能會在記憶體中累積。 IronPDF故障排除指南詳細介紹了常見的記憶體和效能模式。
建立文件自動化流程時,請考慮將重新排序邏輯與文件輸入/輸出分開。 在服務層中接受並返回 byte[] 或 MemoryStream,以保持單元測試快速並避免檔案系統依賴。 IronPDF 提供的 PDF 頁面操作範例並排展示了文件路徑和記憶體工作流程的模式。
下一步計劃是什麼?
使用 IronPDF 在 C# 中重新排列 PDF 頁面,可以將複雜的文件操作任務簡化為少量方法呼叫。 本文涵蓋的核心技術包括:使用索引數組對頁面進行重新排序(使用 CopyPages),使用循環以編程方式反轉所有頁面,通過組合 @@--CODE-26972--@@、@@--CODE-26973--@@ 和 @@--CODE-26974--@@ 移動單頁,以及使用位元組數組和 @@--CODE-26975--@@ 在記憶體中完全處理文件。
這些模式均與 IronPDF 的其他功能集整合。 重新排列頁面後,您可以在交付最終文件之前添加浮水印或圖章、裁剪單一頁面或添加頁碼。 IronPDF 的操作指南為每個後續操作提供了程式碼範例。
首先申請免費試用許可證,在您自己的環境中測試頁面重新排序和 IronPDF 的所有其他功能。 準備部署時,請查看IronPDF 授權選項,以找到符合您專案要求的等級。 當您需要探索數位簽章、PDF/A 合規性和輔助功能標記等進階場景時, IronPDF 文件和物件參考將隨時可用。
常見問題解答
如何使用IronPDF在C#中重新排列PDF頁面?
使用PdfDocument.FromFile載入PDF,創建int[]指定所需的零基頁面順序,然後呼叫pdf.CopyPages(indexArray)並使用SaveAs保存結果。
IronPDF中的零基頁面索引是什麼?
IronPDF從0開始為頁面編號。第一頁是索引0,第二頁是索引1,以此類推。在呼叫CopyPages之前,始終驗證您的陣列中的每個索引均在[0, PageCount - 1]範圍內。
我可以在不保存臨時檔案的情況下重新排列PDF頁面嗎?
可以。使用new PdfDocument(bytes)從byte[]載入PDF,呼叫CopyPages,然後通過reorderedPdf.BinaryData或reorderedPdf.Stream訪問結果,而不進行任何文件系統寫入。
如何在PDF中將單頁移動到不同位置?
使用三步模式:呼叫CopyPage(sourceIndex)提取頁面,呼叫RemovePage(sourceIndex)從文檔中刪除,然後呼叫InsertPdf(pageDoc, targetIndex)將其放置於新位置。向前移動時調整目標索引減1以補償移除所造成的變位。
如何在C#中從PDF中刪除頁面?
在要刪除的頁面號的零基索引處呼叫pdf.RemovePage(index)。刪除後,所有後續頁面索引下移一位。
我可以從ASP.NET Core控制器返回重新排列的PDF嗎?
可以。將上傳的文件載入byte[],以所需的索引陣列調用CopyPages,然後從您的控制器操作返回File(reordered.BinaryData, "application/pdf", "reordered.pdf")。
如何將多個重新排序的PDF合併為一個文檔?
在每個來源文件上調用CopyPages以生成獨立重新排序的PdfDocument對象,然後將它們全部傳遞給PdfDocument.Merge(docA, docB)以輸出單一合併的結果。
重新排序大型PDF中的頁面的最有效方法是什麼?
使用CopyPages(IEnumerable重載與完整索引陣列,而不是在迴圈中調用CopyPage。批次重載在單一內部通過中處理整個序列,減少分配和執行時間。



