使用C#與IronPDF對PDF進行線性化的方法
線性化 PDF 可在下載時立即顯示第一頁,大幅改善大型文件的使用者體驗。 IronPDF 提供簡單的方法在 C# 中創建並驗證它們,優化您的文件以便快速在網頁中查看。
線性化 PDF,也稱為“快速 Web 檢視”或“Web 優化 PDF”,其結構經過重新組織,以適應互聯網串流播放。 這樣一來,相容的檢視器幾乎可以立即顯示文件的第一頁,遠早於整個文件下載完成的時間。
在任務關鍵型或時間敏感型應用中,此功能尤其有用。 它消除了大型文件令人沮喪的載入時間,尤其是在網路速度慢或行動網路的情況下,使用戶能夠立即與內容互動。 這有助於加快決策速度,並提高專業環境中的生產力。 當與IronPDF的性能優化功能結合時,它們提供了卓越的檢視體驗。
在本操作指南文章中,我們將探討 IronPDF 為開發人員提供的將文件匯出為線性化 PDF 的選項。
快速入門:將PDF線性化以加速網頁查看立即開始使用 IronPDF,輕鬆實現 PDF 線性化。 此簡單代碼示例顯示如何通過使用IronPDF的LinearizePdf方法來優化PDF以便在網絡瀏覽器中更快載入。 透過允許頁面在載入時顯示,而不是等待整個文件下載完畢,來提升使用者體驗。 請按照以下步驟來簡化您的PDF,並使其更有效率地進行線上分享。
-
1使用 IronPDF 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var pdf = IronPdf.PdfDocument.FromFile("input.pdf"); pdf.SaveAsLinearized("linearized.pdf");C# -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPDF,並使用 免費試用
最簡工作流程(4 個步驟)
- 從NuGet下載IronPDF C#程式庫
- 使用
PdfDocument.FromFile載入PDF - 使用
SaveAsLinearized將PDF儲存為線性化的PDF - 使用
IsLinearized驗證 PDF 是否線性化。
如何將PDF儲存為線性化?
使用 IronPDF 將文件儲存為線性化 PDF 是一個快速簡單的過程。 在開始之前,請確保您已經通過NuGet安裝了IronPDF或透過其他可用的安裝方法。
在此範例中,我們將使用RenderHtmlAsPdf將HTML字串渲染為PDF。 對於更複雜的HTML文件,您可能會想探索IronPDF的HTML到PDF轉換功能。 隨後,我們將使用SaveAsLinearized實例方法將PdfDocument物件保存為線性化的PDF,並將輸出檔案路徑作為字串參數傳入。
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Pdf Bytes</h1>");
// Get the PDF binary data
var pdfBytes = pdf.BinaryData;
// Save the PDF binary data as a linearized PDF file
PdfDocument.SaveAsLinearized(pdfBytes, "linearize-from-bytes.pdf");Imports IronPdf
' Instantiate Renderer
Dim renderer As New ChromePdfRenderer()
' Create a PDF from an HTML string using VB.NET
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Pdf Bytes</h1>")
' Get the PDF binary data
Dim pdfBytes = pdf.BinaryData
' Save the PDF binary data as a linearized PDF file
PdfDocument.SaveAsLinearized(pdfBytes, "linearize-from-bytes.pdf")Imports IronPdf
' Instantiate Renderer
Dim renderer As New ChromePdfRenderer()
' Create a PDF from an HTML string using VB.NET
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Pdf Bytes</h1>")
' Get the PDF binary data
Dim pdfBytes = pdf.BinaryData
' Save the PDF binary data as a linearized PDF file
PdfDocument.SaveAsLinearized(pdfBytes, "linearize-from-bytes.pdf")此過程會重構PDF的內部格式,將關鍵資訊置於檔案的開頭,從而啟用漸進式下載和渲染。 它特別有益於通過網頁應用程式提供 PDF 時。
輸出看起來是怎樣的?

如何將PDF位元組儲存為線性化?
除了直接保存PdfDocument物件外,IronPDF還允許用戶將PDF位元組陣列轉換為線性化的PDF。 這種靈活性在處理儲存在資料庫中的 PDF 或在記憶體密集型應用程式中處理 PDF 時特別有用。 有關在記憶中處理PDF的更多資訊,請參閱我們的從記憶流載入PDF指南。
在此範例中,我們將展示如何將HTML字串渲染為PdfDocument物件,取得其位元組陣列,然後使用接受byte[]輸入、輸出路徑和可選密碼的SaveAsLinearized重載將該數據保存為線性化的PDF。
輸出看起來是怎樣的?
這是程式碼產生的檔案:
如何將MemoryStream儲存為線性化?
接受Stream輸入的SaveAsLinearized重載仍然會將線性化的輸出寫入到指定的輸出路徑的檔案中。 當您的源 PDF 已處於流狀態(來自資料庫、網路或內存緩衝區)時,這對於想將線性化結果持久化到磁碟非常有用。
在此範例中,我們將把PdfDocument物件轉換為位元組陣列,寫入MemoryStream,然後保存該流為線性化的PDF檔來展示此功能。
如果您需要完全避免寫入磁碟(例如 HIPAA、PCI-DSS及具有只讀文件系統的雲端函數或沙箱應用程式),請查看下一節中的內存方法。
這是程式碼產生的檔案:
如何在內存中完全線性化?
對於沙盒環境、雲端功能或合規敏感的工作流程(HIPAA,PCI-DSS),其中寫入磁盤受到限制或不可取,IronPDF提供LinearizePdfToBytes和LinearizePdfToStream方法可直接返回線性化的PDF,無需臨時檔案或磁盤I/O。
這些方法可作為PdfDocument上的實例方法使用,也可以作為接受byte[]或Stream輸入的靜態方法使用。
以字元獲取線性化輸出
當您需要byte[]用於HTTP回應、資料庫varbinary欄位或任何固定大小的緩衝區時,使用LinearizePdfToBytes。
using IronPdf;
// Instance method: linearize the current document, get bytes back
var pdf = PdfDocument.FromFile("input.pdf");
byte[] linearizedBytes = pdf.LinearizePdfToBytes();
// Static method: linearize from a byte array without instantiating PdfDocument
byte[] inputBytes = File.ReadAllBytes("input.pdf");
byte[] result = PdfDocument.LinearizePdfToBytes(inputBytes);
// Password-protected PDFs
byte[] decrypted = PdfDocument.LinearizePdfToBytes(encryptedBytes, password: "secret");using IronPdf;
// Instance method: linearize the current document, get bytes back
var pdf = PdfDocument.FromFile("input.pdf");
byte[] linearizedBytes = pdf.LinearizePdfToBytes();
// Static method: linearize from a byte array without instantiating PdfDocument
byte[] inputBytes = File.ReadAllBytes("input.pdf");
byte[] result = PdfDocument.LinearizePdfToBytes(inputBytes);
// Password-protected PDFs
byte[] decrypted = PdfDocument.LinearizePdfToBytes(encryptedBytes, password: "secret");Imports IronPdf
' Instance method: linearize the current document, get bytes back
Dim pdf = PdfDocument.FromFile("input.pdf")
Dim linearizedBytes As Byte() = pdf.LinearizePdfToBytes()
' Static method: linearize from a byte array without instantiating PdfDocument
Dim inputBytes As Byte() = File.ReadAllBytes("input.pdf")
Dim result As Byte() = PdfDocument.LinearizePdfToBytes(inputBytes)
' Password-protected PDFs
Dim decrypted As Byte() = PdfDocument.LinearizePdfToBytes(encryptedBytes, password:="secret")Imports IronPdf
' Instance method: linearize the current document, get bytes back
Dim pdf = PdfDocument.FromFile("input.pdf")
Dim linearizedBytes As Byte() = pdf.LinearizePdfToBytes()
' Static method: linearize from a byte array without instantiating PdfDocument
Dim inputBytes As Byte() = File.ReadAllBytes("input.pdf")
Dim result As Byte() = PdfDocument.LinearizePdfToBytes(inputBytes)
' Password-protected PDFs
Dim decrypted As Byte() = PdfDocument.LinearizePdfToBytes(encryptedBytes, password:="secret")將線性化輸出作為串流取得
當要將輸出管道化到另一個流(例如雲端Blob上傳、HTTP回應主體、網絡套接字)而不在記憶體中緩衝整個檔案時,使用LinearizePdfToStream。
// Instance method: return a Stream
Stream linearizedStream = pdf.LinearizePdfToStream();
// Static method: linearize from a stream
using var inputStream = File.OpenRead("input.pdf");
Stream resultStream = PdfDocument.LinearizePdfToStream(inputStream);// Instance method: return a Stream
Stream linearizedStream = pdf.LinearizePdfToStream();
// Static method: linearize from a stream
using var inputStream = File.OpenRead("input.pdf");
Stream resultStream = PdfDocument.LinearizePdfToStream(inputStream);Imports System.IO
' Instance method: return a Stream
Dim linearizedStream As Stream = pdf.LinearizePdfToStream()
' Static method: linearize from a stream
Using inputStream As FileStream = File.OpenRead("input.pdf")
Dim resultStream As Stream = PdfDocument.LinearizePdfToStream(inputStream)
End UsingImports System.IO
' Instance method: return a Stream
Dim linearizedStream As Stream = pdf.LinearizePdfToStream()
' Static method: linearize from a stream
Using inputStream As FileStream = File.OpenRead("input.pdf")
Dim resultStream As Stream = PdfDocument.LinearizePdfToStream(inputStream)
End Using控制線性化策略
所有六種方法均接受可選的LinearizationMode參數:
| 模式 | 行為 | 何時使用 |
|---|---|---|
Automatic(預設) | 首先嘗試基於檔案的方法;若磁碟存取受限,則回退至記憶體內處理 | 建議預設; 適用於大多數環境 |
InMemory | 完全在記憶體中執行線性化處理,無需任何磁碟 I/O | HIPAA / PCI-DSS 工作流程、沙箱應用程式、唯讀檔案系統、雲端函式 |
FileBased | 使用系統暫存目錄中的臨時檔案 | 當您明確需要臨時檔案行為且具備寫入權限時 |
using IronPdf;
// Force in-memory linearization (no disk I/O at all)
byte[] linearized = pdf.LinearizePdfToBytes(LinearizationMode.InMemory);using IronPdf;
// Force in-memory linearization (no disk I/O at all)
byte[] linearized = pdf.LinearizePdfToBytes(LinearizationMode.InMemory);Imports IronPdf
' Force in-memory linearization (no disk I/O at all)
Dim linearized As Byte() = pdf.LinearizePdfToBytes(LinearizationMode.InMemory)Imports IronPdf
' Force in-memory linearization (no disk I/O at all)
Dim linearized As Byte() = pdf.LinearizePdfToBytes(LinearizationMode.InMemory)ArgumentException在輸入字節為空/無內容或輸入流無法讀取時將引發。 當輸入流為null時,引發ArgumentNullException。如何將線性化 PDF 串流至 HTTP 回應?
結合LinearizePdfToStream和ASP.NET的FileStreamResult,直接向瀏覽器提供網絡優化的PDF,而不需寫入臨時檔案:
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class ReportController : Controller
{
public IActionResult DownloadReport()
{
var pdf = PdfDocument.FromFile("quarterly-report.pdf");
Stream linearized = pdf.LinearizePdfToStream(LinearizationMode.InMemory);
return new FileStreamResult(linearized, "application/pdf")
{
FileDownloadName = "quarterly-report.pdf"
};
}
}using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class ReportController : Controller
{
public IActionResult DownloadReport()
{
var pdf = PdfDocument.FromFile("quarterly-report.pdf");
Stream linearized = pdf.LinearizePdfToStream(LinearizationMode.InMemory);
return new FileStreamResult(linearized, "application/pdf")
{
FileDownloadName = "quarterly-report.pdf"
};
}
}Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
Public Class ReportController
Inherits Controller
Public Function DownloadReport() As IActionResult
Dim pdf = PdfDocument.FromFile("quarterly-report.pdf")
Dim linearized As Stream = pdf.LinearizePdfToStream(LinearizationMode.InMemory)
Return New FileStreamResult(linearized, "application/pdf") With {
.FileDownloadName = "quarterly-report.pdf"
}
End Function
End ClassImports IronPdf
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
Public Class ReportController
Inherits Controller
Public Function DownloadReport() As IActionResult
Dim pdf = PdfDocument.FromFile("quarterly-report.pdf")
Dim linearized As Stream = pdf.LinearizePdfToStream(LinearizationMode.InMemory)
Return New FileStreamResult(linearized, "application/pdf") With {
.FileDownloadName = "quarterly-report.pdf"
}
End Function
End Class瀏覽器接收到的線性化PDF從第一頁開始顯示,同時其餘內容流動進來——無中間文件曾在伺服器的磁碟上。
如何驗證PDF是否為線性化?
除了在PDF檢視器(如Adobe Acrobat)中檢查文件屬性以查看PDF是否已線性化外,IronPDF還提供了一種通過IsLinearized方法以程式化方式檢查的方法。 它接受一個字串參數作為檔案路徑,若 PDF 檔案經過加密,則可選地接受第二個字串參數作為密碼。 此驗證能力對於品質保證非常重要,並可整合至自動化測試工作流程中。
在這個例子中,我們將使用上面三個例子的輸出檔案來測試它們是否線性化,並包含第四個非線性化的 PDF 來展示方法的行為。 欲了解更先進的PDF操作和驗證技術,請探索IronPDF的全面功能集。
結果如何?

正如您所見,前三個範例返回True,而最後一個未線性化的PDF返回False。
IsLinearized可以對已保存的檔案進行驗證。 若要生成作為位元組或流的線性化輸出而不寫入磁盤,請使用上面描述的LinearizePdfToBytes和LinearizePdfToStream方法。線性化PDF的最佳實踐
在處理線性化 PDF 時,請考慮這些最佳實務:
- 檔案大小考量:由於重新結構化格式,線性化可能會輕微增加檔案大小。 根據需要使用IronPDF的壓縮功能來優化檔案大小。
- 網站部署:線性化PDF適合用於網頁應用。 配置您的 Web 伺服器以支援 byte-range 請求,從而最大化線性化的效益。
- 性能測試:始終在您的目標環境中測試線性化PDF。 在較慢的連線上處理大型檔案時,效能提升最為顯著。
- 相容性:雖然大多數現代PDF檢視器支援線性化PDF,但請確保與用戶的首選檢視器相容。
有關其他優化策略和高級PDF處理技術,請參閱IronPDF的渲染選項文件。
常見問題
什麼是線性化PDF?
線性化PDF,也稱為「快速網頁查看」或「網頁優化PDF」,是一種為網路串流重組的PDF。這樣可以讓查看者在整份文件仍在下載時,幾乎立即顯示第一頁,提高了較大文件的使用者體驗。
如何使用IronPDF進行PDF線性化?
您可以利用 IronPDF 的 SaveAsLinearized 方法來線性化 PDF。這個方法將 PDF 結構重組以供漸進式下載,並允許在兼容的查看器中快速顯示首頁。
為什麼要在網頁應用程式中使用線性化PDF?
在網頁應用程式中使用線性化PDF有益,因為它消除了大型文件的長加載時間,能讓用戶立即與文件互動,提升了在慢速或行動網路上的使用者體驗。
IronPDF如何在線性化過程中處理PDF位元組陣列?
IronPDF 允許您使用 SaveAsLinearized 方法將 PDF 位元組陣列轉換為線性化 PDF。當處理存儲在資料庫中或內存密集型應用中的 PDF 時,這種靈活性很有用。
我可以使用IronPDF在完全記憶體中線性化PDF嗎?
是的,IronPDF 提供了 LinearizePdfToBytes 和 LinearizePdfToStream 這樣的方法,來在內存中完全線性化 PDF,這在磁盤 I/O 受限或不希望時是理想的選擇。
如何使用IronPDF驗證PDF是否已線性化?
IronPDF 提供 IsLinearized 方法來程式化檢查 PDF 是否被線性化。這可以集成到自動化測試工作流程中以保證質量。
使用IronPDF的`LinearizationMode`有什麼好處?
IronPDF 中的 LinearizationMode 可以讓您控制如何執行線性化,提供如 Automatic、InMemory 和 FileBased 這樣的選項。這些模式根據環境和需求提供了靈活性。
使用線性化PDF的最佳實踐是什麼?
線性化PDF的最佳實踐包括考量文件大小、使用支持位元組範圍請求的網頁部署策略、在目標環境中進行性能測試,以及確保檢視器相容性。
IronPDF可以直接從流中線性化PDF嗎?
是的,IronPDF 可以直接從流中使用 LinearizePdfToStream 方法來線性化 PDF。這對於雲端上傳、網絡插槽或 HTTP 回應主體十分有用。
線性化如何影響PDF文件大小?
線性化可能會因重組的格式略微增加PDF文件大小。為了管理文件大小,請考慮使用IronPDF的壓縮功能來優化您的文件。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。