How to Linearize PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English

線性化 PDF,也被稱為「快速網頁視圖」或「網頁優化 PDF」,是為了網際網路串流而重新組織的結構。 這允許兼容的查看器幾乎立即顯示文檔的第一頁,遠在整個文件下載完成之前。

在關鍵任務或時間敏感的應用中,這個功能特別有用。 它消除了大型文件的載入時間,特別是在慢速或移動網路上,允許用戶立即與內容互動。 這有助於加快決策速度並提高專業環境下的生產力。

在這篇操作指南中,我們將探索 IronPDF 提供給開發者的選項,以將他們的文件匯出為線性化 PDF。

快速入門:線性化您的 PDF 以加快網頁視圖

使用 IronPDF 開始輕鬆線性化您的 PDF。 這個簡單的代碼範例展示了如何使用 IronPDF 的 LinearizePdf 方法優化 PDF,使其在網頁瀏覽器中加速加載。 通過允許頁面在加載時顯示,增強用戶體驗,而不是等到整個文檔下載完成。 按照以下步驟來簡化您的 PDF 並使其更有效率地在線共享。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = IronPdf.PdfDocument.FromFile("input.pdf");
    pdf.SaveAsLinearized(pdf.BinaryData, "linearized.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5步驟)

  1. 從 NuGet 下載 IronPDF C# 庫
  2. 實例化 PDF 渲染器並傳遞 HTML 字串
  3. 使用 RenderHtmlAsPdf 渲染 HTML 字串
  4. 使用 SaveAsLinearized 將 PDF 保存為線性化 PDF
  5. 使用 IsLinearized 驗證 PDF 是否為線性化


儲存為線性化 PDF

使用 IronPDF 將文檔保存為線性化 PDF 是一個快速且簡單的過程。 在這個範例中,我們將使用 RenderHtmlAsPdf 渲染一個 HTML 字串到 PDF。 之後,我們將使用 SaveAsLinearized 實例方法將 PdfDocument 對象保存為線性化 PDF。 此方法接受輸出文件路徑的字串參數。

:path=/static-assets/pdf/content-code-examples/how-to/linearize-pdf.cs
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

class="content-img-align-center">
style="width=50%"> 快速網頁視圖 PDF

儲存為線性化 PDF bytes

除了直接保存 PdfDocument 對象, IronPDF 也允許用戶將 PDF 位元組數組轉換為線性化 PDF。 在這個範例中,我們將演示如何渲染 HTML 字串為 PdfDocument 對象,獲取其位元組數組,然後將該數據保存為線性化 PDF。 此 SaveAsLinearized 方法還接受一個可選的第三個字串參數作為密碼,如果源文檔被加密的話。

:path=/static-assets/pdf/content-code-examples/how-to/linearize-pdf-bytes.cs
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

這是代碼生成的文件:

儲存為線性化 PDF MemoryStream

SaveAsLinearized 方法也可以接受 Stream 對象作為輸入。 在這個範例中,我們將轉換 PdfDocument 對象為位元組數組,將其寫入 MemoryStream,然後將流保存為線性化 PDF,以展示此功能。

:path=/static-assets/pdf/content-code-examples/how-to/linearize-pdf-stream.cs
using IronPdf;
using System.IO;

// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Memory Stream</h1>");

// Get the PDF binary data
var pdfBytes = pdf.BinaryData;

// Transform PDF bytes to a MemoryStream
MemoryStream memoryStream = new MemoryStream(pdfBytes);

// Save the MemoryStream as a linearized PDF
PdfDocument.SaveAsLinearized(memoryStream, "linearize-stream.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

這是代碼生成的文件:


驗證 PDF 是否為線性化

除了在 PDF 查看器中(如 Adobe Acrobat)檢查文件屬性以查看 PDF 是否為線性化之外,IronPDF 還提供了一種通過 IsLinearized 方法以程式化方式進行檢查的方式。 該方法接受文件路徑的字串參數,以及一個可選的第二個字串參數作為密碼,如果該 PDF 被加密的話。

在這個範例中,我們將使用上面三個範例的輸出文件來測試它們是否為線性化,並包括第四個非線性化 PDF 以展示該方法的行為。

:path=/static-assets/pdf/content-code-examples/how-to/linearize-pdf-test.cs
using IronPdf;
using System;

// First example Linearized PDF
Console.WriteLine(PdfDocument.IsLinearized("linearize.pdf"));

// Second example Linearized PDF
Console.WriteLine(PdfDocument.IsLinearized("linearize-from-bytes.pdf"));

// Third example Linearized PDF
Console.WriteLine(PdfDocument.IsLinearized("linearize-stream.pdf"));

// Fourth example Non-Linearized PDF
Console.WriteLine(PdfDocument.IsLinearized("sample.pdf"));
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

輸出

class="content-img-align-center">
style="width=50%"> 是否線性化結果

如您所見,前三個範例返回 true,而最後一個未線性化的 PDF 返回false

請注意沒有方法可以直接檢查 PdfDocument 對象本身是否為線性化。 當 PDF 文件被打開和載入到一個對象中時,其特殊的線性化結構就會丟失。出於同樣的原因,也沒有方法將線性化 PDF 返回為位元組數組。 這種線性化特性僅存在於保存到磁碟上的文件中。
This is because when a PDF file is opened and loaded into an object, its special linearized structure is lost. For the same reason, there is no method to return a linearized PDF as a byte array. This linearized feature only exists as a saved file on disk.)}]

常見問題解答

線性化PDF是什麼意思?

PDF線性化,也稱為快速Web視圖,是指透過優化PDF檔案的結構,使其更適合在網頁上使用,從而加快載入速度。這對於需要線上存取的大型文件尤其有用。

為什麼要對PDF進行線性化處理?

將 PDF 文件線性化可以提升使用者體驗,使用戶能夠更快地存取文件內容,尤其是透過網路查看時。它允許在整個文件下載完成之前顯示第一頁,這對於大型文件來說非常有利。

IronPDF 如何幫助線性化 PDF 檔案?

IronPDF 提供了一種使用 C# 程式碼線性化 PDF 檔案的簡單方法。它可以幫助開發人員優化 PDF 的結構,從而提高 Web 效能,確保使用者能夠更快地存取和檢視文件。

線性化過程是可逆的嗎?

是的,線性化過程可以逆轉,但這需要將檔案結構改回非線性格式。 IronPDF 讓您可以操作 PDF 文件,從而控制它們的最佳化和顯示方式。

如果我不是開發人員,可以使用 IronPDF 將 PDF 文件線性化嗎?

雖然 IronPDF 是一款專為 C# 設計的開發工具,但它提供了清晰的文件和範例,即使是編碼經驗有限的人也能有效地線性化 PDF。

使用 IronPDF 進行 PDF 線性化有哪些效能優勢?

IronPDF 可優化 PDF 文件,加快載入速度,尤其是在網頁上。這種效能提升對於改善使用者體驗和存取便利性至關重要,尤其對於網路速度較慢的使用者而言。

我需要特殊的軟體才能查看線性化的PDF檔案嗎?

查看線性化 PDF 檔案無需任何特殊軟體。任何標準 PDF 閱讀器均可打開它們。線性化的主要優勢在於加快線上存取 PDF 時的載入速度。

.NET 10 專案是否支援 IronPDF 線性化?

是的。 IronPDF 完全相容於 .NET 10,線性化功能(例如 SaveAsLinearized 和 IsLinearized)無需特殊配置即可正常運作。您可以在 .NET 10 應用程式中使用這些方法,就像在早期受支援的版本中一樣。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,133,208 | 版本: 2025.11 剛剛發布