如何在 C# 壓縮 PDF

How to Compress PDFs

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

PDF 壓縮是指減少 PDF(可攜式文檔格式)文檔的文件大小的過程。 這種壓縮是為了使 PDF 文件在存儲、共享和傳輸中更易於管理,尤其是在處理大型或圖像豐富的文檔時。

圖像通常佔據 PDF 文件大小的重要部分,因為它們通常比文本和其他內容大。 IronPdf 提供 PDF 壓縮功能,能壓縮嵌入的圖像並減少常伴隨 PDF 表格數據的樹形結構。

標題:2(快速入門:使用 IronPDF 壓縮 PDF 文件)

使用 IronPDF 的強大壓縮工具輕鬆減少 PDF 文件大小。 首先使用 PdfDocument.FromFile 加載 PDF,使用默認選項以 Compress 方法應用壓縮,然後保存優化後的 PDF。 這種快速簡便的過程確保了顯著的文件大小減少,同時保持質量,非常適合尋求在 .NET C# 中高效文檔管理的開發者。

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.

    PdfDocument.FromFile("input.pdf").CompressImages(40).SaveAs("compressed.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 下載 PDF 壓縮的 C# 庫
  2. 導入現有 PDF 或生成新的 PDF
  3. 使用 CompressImages 方法減小 PDF 中圖像的大小
  4. 利用 CompressStructTree 方法最大限度地減小 PDF 的樹形結構
  5. 導出壓縮後的 PDF 文檔


壓縮圖像示例

調整 JPEG 大小的方式是,100% 質量幾乎沒有損失,而 1% 則是非常低質量的輸出圖像。

  • 90% 及以上:視為高質量
  • 80%-90%:視為中等質量
  • 70%-80%:視為低質量

可以自由探索各種值以了解質量和文件大小之間的權衡。需要注意的是,質量的下降會根據輸入圖像類型而有所不同,某些圖像可能比其他圖像經歷更明顯的清晰度減少。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-image.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");

// Compress images in the PDF
pdf.CompressImages(40);

pdf.SaveAs("compressed.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")

' Compress images in the PDF
pdf.CompressImages(40)

pdf.SaveAs("compressed.pdf")
$vbLabelText   $csharpLabel

壓縮圖像 - 大小比較

減少了 39.24%!!

class="content-img-align-center">
class="center-image-wrapper">壓縮圖像 - 大小比較

理解圖像壓縮選項

讓我們深入了解我們的圖像壓縮選項:

ShrinkImage:此功能根據其在 PDF 文檔中的可見大小縮小圖像分辨率。 通過這樣做,它顯著減少了圖像的大小和質量,優化了它們的高效存儲和傳輸。

HighQualitySubsampling:此設置確定用於圖像壓縮的色度子採樣方法。 選擇“True”使用 4:4:4 色度子採樣,確保高品質的全彩圖像細節。 相反,選擇“False”使用 4:1:1 色度子採樣,犧牲一些色彩細節以進一步減少圖像大小。

色度子採樣是數字圖像壓縮中的一項關鍵技術,旨在減少表示圖像所需的數據,同時保持其視覺質量。 它通過選擇性地降低顏色信息(色度)的分辨率,而同時保持亮度信息(亮度)的完整分辨率來實現這一點。

在“4:4:4”色度子採樣中,每個像素保留自己的顏色信息,沒有色彩細節損失。 相反,在“4:1:1”色度子採樣中,顏色信息以較低的分辨率進行子採樣,減少色彩細節但也減小了文件大小。


壓縮樹形結構示例

這一功能用於通過最小化由 Chrome 引擎創建的樹形結構來減少 PDF 大小。它能很好地處理由 Chrome 引擎從包含大量表格數據的 HTML 生成的 PDF。 一些 PDF 渲染引擎可能會輸出沒有這種樹形結構的 PDF,使得此功能無效。

刪除所有這些樹形結構的缺點是對於某些 PDF,突出顯示文本或提取可能不那麼有效。

Let's use the PDF with table data to test the CompressStructTree method.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-tree-structure.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("table.pdf");

// Compress tree structure in PDF
pdf.CompressStructTree();

pdf.SaveAs("compressedTable.pdf");
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("table.pdf")

' Compress tree structure in PDF
pdf.CompressStructTree()

pdf.SaveAs("compressedTable.pdf")
$vbLabelText   $csharpLabel

壓縮樹形結構 - 大小比較

減少了 67.90%!! 這個比例在更大的表格 PDF 中有所增加。

class="content-img-align-center">
class="center-image-wrapper">壓縮樹形結構 - 大小比較

高級壓縮方法

IronPdf 還有一種 Compress 方法,可以配置圖像壓縮和樹形結構壓縮,使文檔壓縮比以往更容易。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-compress.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

CompressionOptions compressionOptions = new CompressionOptions();

// Configure image compression
compressionOptions.CompressImages = true;
compressionOptions.JpegQuality = 80;
compressionOptions.HighQualityImageSubsampling = true;
compressionOptions.ShrinkImages = true;

// Configure tree structure compression
compressionOptions.RemoveStructureTree = true;

pdf.Compress(compressionOptions);

pdf.SaveAs("compressed.pdf");
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

Private compressionOptions As New CompressionOptions()

' Configure image compression
compressionOptions.CompressImages = True
compressionOptions.JpegQuality = 80
compressionOptions.HighQualityImageSubsampling = True
compressionOptions.ShrinkImages = True

' Configure tree structure compression
compressionOptions.RemoveStructureTree = True

pdf.Compress(compressionOptions)

pdf.SaveAs("compressed.pdf")
$vbLabelText   $csharpLabel

探索可用選項

  • CompressImages:控制文檔中現有圖像是否使用 JPG 編碼壓縮。 默認為假。
  • RemoveStructureTree:刪除結構樹可以顯著減少文檔使用的磁盤空間。 然而,在復雜的文檔中,這可能會對文本選擇產生不利影響。
  • JpegQuality:指定在圖像壓縮期間使用的 JPEG 質量(範圍從 1 到 100)。 默認為 42。
  • HighQualityImageSubsampling:此屬性確定是否使用 4:4:4 色度子採樣以獲得更高的圖像質量(真)或使用 4:1:1 色度子採樣以進一步減少圖像大小(假)。
  • ShrinkImages:縮小圖像分辨率可以大幅度減少文檔中圖像的大小和質量。

準備看看您還能做哪些其他事情嗎? 查看我們的教程頁面:附加功能

常見問題解答

如何在.NET C#中壓縮PDF影像?

您可以使用 IronPDF 在 .NET C# 中壓縮 PDF 影像。該庫提供了一個名為CompressImages的方法,可讓您調整 JPEG 品質並在不同的色度子取樣選項之間進行選擇,從而有效地減少影像大小。

使用 .NET 函式庫進行 PDF 壓縮有什麼好處?

使用 IronPDF 等 .NET 程式庫進行 PDF 壓縮,可以將 PDF 大小縮減功能直接整合到 C# 應用程式中,透過減小檔案大小來增強文件管理,從而更輕鬆地儲存和共用文件。

如何減少包含大量表格資料的PDF檔案的大小?

要減少包含大量表格資料的 PDF 檔案的大小,可以使用 IronPDF 的CompressStructTree方法。此方法可以最小化 PDF 的樹狀結構,從而在保持文件完整性的同時顯著減少文件大小。

PDF壓縮中影像品質調整有哪些選項?

IronPDF 提供了一些選項,可以在壓縮過程中調整影像質量,例如JpegQuality參數,其範圍從 1 到 100。較高的值可以保留更多的影像細節,而較低的值可以提高壓縮率。

我可以在PDF影像壓縮過程中控制色度二次取樣嗎?

是的,IronPDF 允許您使用HighQualitySubsampling設定來控制 PDF 影像壓縮過程中的色度二次採樣。這樣,您可以選擇更高的圖像品質 (4:4:4) 或更大的檔案大小縮減 (4:1:1)。

移除 PDF 檔案中的結構樹可能有哪些缺點?

雖然使用 IronPDF 的壓縮功能刪除 PDF 中的結構樹可以顯著減小文件大小,但可能會影響複雜文件中的文字選擇和提取,因此考慮這種權衡很重要。

如何在PDF中同時配置影像和樹狀結構壓縮?

IronPDF 提供了一種Compress方法,可讓您配置 PDF 中的影像和樹狀結構壓縮。此方法可靈活調整設置,以在品質和檔案大小之間取得理想的平衡。

如何在.NET C#中開始壓縮PDF?

要開始在 .NET C# 中壓縮 PDF,請從 NuGet 下載 IronPDF 庫,匯入現有的 PDF 或渲染新的 PDF,並使用CompressImagesCompressStructTree等方法來有效地縮小檔案大小。

IronPDF 在壓縮 PDF 時是否與 .NET 10 完全相容?

是的-IronPDF 完全相容 .NET 10。您可以在 .NET 10 專案中使用它來壓縮 PDF 文件,可以使用與 .NET 10 相同的壓縮方法,例如CompressImagesCompressStructTree或具有CompressionOptionsCompress方法,以獲得一致的壓縮結果和效能。它支援的運行時版本包括 .NET 10。 ⁠([ironpdf.com](https://ironpdf.com/?utm_source=openai))

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

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
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: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
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: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
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,154,058 | 版本: 2025.11 剛剛發布