IronPDFを使ってC#でPDFを圧縮する方法
IronPDFはC#で強力なPDF圧縮機能を提供し、埋め込み画像の圧縮やPDFのツリー構造の最適化によってファイルサイズを縮小し、品質を維持しながら保存や共有のためにドキュメントをより管理しやすくします。
PDF圧縮は、PDFドキュメントのファイルサイズを縮小し、保存、共有、送信をより管理しやすくします。 HTMLから生成されたPDFでも、既存のPDFファイルでも、圧縮はドキュメントの処理を最適化するために不可欠です。
通常、PDFファイルサイズの大半は画像で占められています。 IronPDFは埋め込み画像のサイズを縮小し、テーブルの多いPDFによく見られるツリー構造を最適化する圧縮機能を提供します。 これらのテクニックは、URLからPDFへの変換やHTMLファイルへの変換など、さまざまな方法で作成されたPDFでシームレスに動作します。
クイックスタート: IronPDFで PDF ファイルを圧縮するIronPDFの圧縮ツールを使ってPDFファイルサイズを縮小します。 PdfDocument.FromFile を使用して PDF を読み込み、CompressAndSaveAs を呼び出すことで、最適化された PDF を 1 ステップで圧縮・保存できます。このプロセスにより、品質を維持しながらファイルサイズを大幅に削減できます。
-
1Install IronPDF with NuGet Package Manager
-
2このコード スニペットをコピーして実行します。
PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40);PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40)PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40) -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronPDF を使い始めましょう無料トライアル
最小限のワークフロー(3ステップ)
- PDF圧縮用のC#ライブラリをNuGetからダウンロードする。
- 既存のPDFをインポートするか、新しいPDFをレンダリングする
CompressAndSaveAs(outputPath, quality, removeStructureTree)を呼び出し、画像を圧縮し、必要に応じて構造ツリーを除去して、すべてを一度の手順で保存します。
CompressStructTree、および Compress(CompressionOptions) などの従来の圧縮方式は非推奨となっています。PDF内の画像を圧縮するにはどうすればよいですか?
JPEG圧縮品質は、0(最大圧縮)から100(最小損失)までの値を受け付け、その値の適用方法は基となるエンジンによって決定されます。 これらのレベルを理解することは、埋め込み画像を含むPDFを扱うときや、フォントやグラフィックを管理する必要があるときに役立ちます。
- 90%以上:高品質
- 80%-90%: 中程度の品質
- 70%~80%: 低品質
品質とファイルサイズのバランスをとるために、さまざまな値を試してください。画質の低下は、入力画像の種類によって異なります。ある画像は、他の画像よりも明瞭度の低下が目立ちます。 これは、Azure Blob Storage からの画像を扱う場合や、カスタム透かしを入れた PDF を作成する場合に特に重要です。
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Quality 40 re-encodes every embedded image as a low-quality JPEG, which is
// where most of the file-size saving comes from in image-heavy PDFs
pdf.CompressAndSaveAs("compressed.pdf", 40);using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Quality 40 re-encodes every embedded image as a low-quality JPEG, which is
// where most of the file-size saving comes from in image-heavy PDFs
pdf.CompressAndSaveAs("compressed.pdf", 40);Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")
' Quality 40 re-encodes every embedded image as a low-quality JPEG, which is
' where most of the file-size saving comes from in image-heavy PDFs
pdf.CompressAndSaveAs("compressed.pdf", 40)Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")
' Quality 40 re-encodes every embedded image as a low-quality JPEG, which is
' where most of the file-size saving comes from in image-heavy PDFs
pdf.CompressAndSaveAs("compressed.pdf", 40)画像圧縮にはどのような効果が期待できますか?
39.24%削減されました!

quality 内の CompressAndSaveAs パラメータは、埋め込まれたすべての画像に適用される JPEG エンコード品質を制御します。 0(最大圧縮、最低品質)から100(最小圧縮、最高品質)までの整数を指定するか、nullを指定して画像圧縮を完全にスキップしてください。
IronPDFは圧縮中に自動的に画像を再エンコードしスケールダウンするため、追加のオプションは必要ありません。 低い値はファイルを小さくしますが、目に見えるアーティファクトが発生する可能性があります。コンテンツに最適なバランスを見つけるために実験してください。
PDFのツリー構造を圧縮するには?
この機能は、Chromeエンジンによって生成されたツリー構造を削除することで、PDFのサイズを縮小します。これを有効にするには、CompressAndSaveAsの3番目のパラメータとして渡してください。 これは、Chromeエンジンを使用して生成された広範なテーブルデータを含むPDFで最も効果的です。 レンダリングエンジンによっては、この構造を持たずにPDFを出力するものもあるため、そのような場合にはこの機能は効果的ではありません。 このテクニックは、複雑なレイアウトを扱うときや、冗長な構造要素を持つ複数のPDFをマージするときに役立ちます。
ツリー構造を削除すると、テキストの強調表示や抽出の効果に影響する可能性があります。 圧縮を適用するとき、特にユーザーが圧縮されたPDFからテキストや画像を取り出す必要がある場合は、このトレードオフを考慮してください。
Test CompressAndSaveAs with tree structure removal using this PDF with table data.
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("table.pdf");
// Pass null quality to skip image compression and true to drop the structure
// tree, which is the bulk of the size in table-heavy Chrome-rendered PDFs
pdf.CompressAndSaveAs("compressedTable.pdf", null, true);using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("table.pdf");
// Pass null quality to skip image compression and true to drop the structure
// tree, which is the bulk of the size in table-heavy Chrome-rendered PDFs
pdf.CompressAndSaveAs("compressedTable.pdf", null, true);Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("table.pdf")
' Pass nothing as quality to skip image compression and True to drop the structure
' tree, which is the bulk of the size in table-heavy Chrome-rendered PDFs
pdf.CompressAndSaveAs("compressedTable.pdf", Nothing, True)Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("table.pdf")
' Pass nothing as quality to skip image compression and True to drop the structure
' tree, which is the bulk of the size in table-heavy Chrome-rendered PDFs
pdf.CompressAndSaveAs("compressedTable.pdf", Nothing, True)ツリー構造圧縮はどのようなファイル サイズ削減を実現できますか?
67.90%削減しました! より大きなテーブルPDFでこの割合は増加します。

両方の圧縮方法を一緒に適用するにはどうすればよいですか?
CompressAndSaveAs は、1回の呼び出しで画像とツリー構造の両方の圧縮を処理します。quality の値を渡し、removeStructureTree を true に設定してください。 このような複合的なアプローチは、画像や構造化データを含む複雑なPDF(図表を含むレポートや、表やグラフィックが埋め込まれた文書など)に対して特に効果的です。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Shrink both sources of bloat in one pass: re-encode images at quality 80
// and strip the structure tree (true) for the largest size reduction
pdf.CompressAndSaveAs("compressed.pdf", 80, true);using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Shrink both sources of bloat in one pass: re-encode images at quality 80
// and strip the structure tree (true) for the largest size reduction
pdf.CompressAndSaveAs("compressed.pdf", 80, true);Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Shrink both sources of bloat in one pass: re-encode images at quality 80
' and strip the structure tree (true) for the largest size reduction
pdf.CompressAndSaveAs("compressed.pdf", 80, True)Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Shrink both sources of bloat in one pass: re-encode images at quality 80
' and strip the structure tree (true) for the largest size reduction
pdf.CompressAndSaveAs("compressed.pdf", 80, True)CompressAndSaveAs はどのようなパラメータを受け付けますか?
-
outputPath(文字列): 圧縮されたPDFが保存されるファイルパス。 -
quality(int?, 0–100): 埋め込まれたすべての画像に適用されるJPEGの画質。 低い値はより小さいファイルを生成します。nullを渡すと、画像の圧縮を完全にスキップします。 -
removeStructureTree(bool, デフォルトfalse):trueの場合、PDF 構造ツリーを削除してファイルサイズをさらに縮小します。表を含む HTML 生成の PDF で最も効果的です。
圧縮を適用する場合は、使用ケースを考慮してください。 アーカイブの目的は、ファイルサイズよりも品質を優先する場合があります。ウェブ配信の場合は、ファイルサイズを小さくする必要があります。 PDF/A準拠文書と標準PDFのどちらで作業しているかに基づいて、設定を微調整します。
ディスクに保存せずにPDFを圧縮するにはどうすればよいですか?
APIレスポンス、クラウド関数、メール添付ファイルなど、多くの実際のシナリオでは、圧縮されたPDFをディスクに書き込むことは不要または非現実的です。
CompressPdfToBytes は、圧縮された PDF をバイト配列として返すため、出力の保存や送信方法を完全に制御できます。 CompressPdfToBytes および CompressPdfToStream の両方は、圧縮の方法を制御するオプションの CompressionMode パラメータを受け付けます。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Return the compressed PDF as bytes so it can go straight into an HTTP
// response, a database varbinary column, or a queue message with no temp file
byte[] compressedBytes = pdf.CompressPdfToBytes();
File.WriteAllBytes("compressed.pdf", compressedBytes);using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Return the compressed PDF as bytes so it can go straight into an HTTP
// response, a database varbinary column, or a queue message with no temp file
byte[] compressedBytes = pdf.CompressPdfToBytes();
File.WriteAllBytes("compressed.pdf", compressedBytes);Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Return the compressed PDF as bytes so it can go straight into an HTTP
' response, a database varbinary column, or a queue message with no temp file
Dim compressedBytes As Byte() = pdf.CompressPdfToBytes()
File.WriteAllBytes("compressed.pdf", compressedBytes)Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Return the compressed PDF as bytes so it can go straight into an HTTP
' response, a database varbinary column, or a queue message with no temp file
Dim compressedBytes As Byte() = pdf.CompressPdfToBytes()
File.WriteAllBytes("compressed.pdf", compressedBytes)CompressionMode どのようなオプションが利用可能ですか?
展開環境によって、ディスクアクセスやパフォーマンスに関する制約は異なります。 CompressionMode 列挙型を使用することで、適切なトレードオフを選択できます。
-
Automatic(デフォルト): システムの能力に基づいて最適な圧縮方式を選択します。 まずHighQualityを試行し、ディスクアクセスが制限されている場合はFastMemoryにフォールバックします。 -
FastMemory: ディスク I/O を一切行わず、メモリ内でのみ圧縮を実行します。 最速のオプションですが、画像のリサンプリングやダウンサンプリングは適用されないため、出力ファイルサイズが大きくなります。 -
HighQuality: 一時的なディスクアクセスを使用して、最大限の圧縮を行います。 画像の最適化を最大限に行い、最小サイズの出力ファイルを生成します。 システムの一時ディレクトリへの書き込み権限が必要です。
バイト配列の代わりに Stream が必要な場合は、CompressPdfToStream を使用してください。 これは、HTTPレスポンス、クラウドストレージへのアップロード、またはストリームベースのワークフローに直接パイプする場合に便利です。
using IronPdf;
using System.IO;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Stream output so the compressed bytes can be piped straight to an HTTP
// response or cloud upload without first buffering the whole file as byte[]
using Stream compressedStream = pdf.CompressPdfToStream();
// CompressPdfToStream returns a read-only Stream, so copy it to the destination
using FileStream fileStream = File.Create("compressed.pdf");
compressedStream.CopyTo(fileStream);using IronPdf;
using System.IO;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Stream output so the compressed bytes can be piped straight to an HTTP
// response or cloud upload without first buffering the whole file as byte[]
using Stream compressedStream = pdf.CompressPdfToStream();
// CompressPdfToStream returns a read-only Stream, so copy it to the destination
using FileStream fileStream = File.Create("compressed.pdf");
compressedStream.CopyTo(fileStream);Imports IronPdf
Imports System.IO
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Stream output so the compressed bytes can be piped straight to an HTTP
' response or cloud upload without first buffering the whole file as byte[]
Using compressedStream As Stream = pdf.CompressPdfToStream()
' CompressPdfToStream returns a read-only Stream, so copy it to the destination
Using fileStream As FileStream = File.Create("compressed.pdf")
compressedStream.CopyTo(fileStream)
End Using
End UsingImports IronPdf
Imports System.IO
Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Stream output so the compressed bytes can be piped straight to an HTTP
' response or cloud upload without first buffering the whole file as byte[]
Using compressedStream As Stream = pdf.CompressPdfToStream()
' CompressPdfToStream returns a read-only Stream, so copy it to the destination
Using fileStream As FileStream = File.Create("compressed.pdf")
compressedStream.CopyTo(fileStream)
End Using
End UsingCompressPdfToBytes および CompressPdfToStream は、Java、Node.js、Pythonを含むすべてのIronPDFプラットフォームで利用可能になりました。
次に何ができるのかを見てみましょうか? チュートリアルのページはこちらをご覧ください:追加機能。
よくある質問
PDFのファイルサイズは、圧縮によってどの程度削減できますか?
結果はコンテンツによって異なります。示された例では、イメージ圧縮はファイルを39.24%削減し、構造ツリー削除は表が多いPDFを67.90%削減しました。イメージ重視および表重視のドキュメントは最大の節約を見ます。
IronPDFはC#でどのようにPDFを圧縮しますか?
IronPDFは、単一のメソッド「CompressAndSaveAs」を通じて圧縮処理を行います。このメソッドは、設定可能なJPEG画質(0~100)を使用して埋め込み画像のサイズを縮小し、オプションで`removeStructureTree: true`を設定することでPDFの内部構造ツリーを削除できます。これは、表が多用されているドキュメントにおいて特に効果的です。
PDFのJPEG圧縮には、どのような品質レベルを使用すればよいですか?
IronPDFは1%から100%のJPEG圧縮品質をサポートしています。推奨レベルは以下の通りです:高品質は90%以上、中品質は80%~90%、低品質は70%~80%です。最適な設定はファイルサイズとビジュアル品質の要求のバランスによって決まります。
たった1行のコードでPDFを圧縮できますか?
はい、IronPDFでは1行のコードでPDFの圧縮が可能です:PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40); これにより、PDFを読み込み、画像を40%の品質で圧縮し、結果を保存します。
圧縮はHTMLから作成されたPDFでも機能しますか?
はい、IronPDFの圧縮は、HTML文字列のPDF変換、URLのPDF変換、HTMLファイルの変換、既存のPDFファイルなど、さまざまな方法で作成されたPDFと一緒に動作します。
PDF圧縮のメリットを最も享受できるのはどのようなコンテンツですか?
通常、画像はPDFファイルサイズの大部分を占めるため、画像を多用する文書は圧縮の理想的な候補となります。さらに、複雑な表構造のPDFはIronPDFの構造ツリー圧縮機能の恩恵を受けます。
PDFをディスクに保存せずに圧縮することはできますか?
はい、IronPDFには、圧縮されたPDFをそれぞれバイト配列またはStreamとして返すCompressPdfToBytesメソッドとCompressPdfToStreamメソッドが用意されています。どちらも、Automatic、FastMemory、またはHighQualityの圧縮オプションを指定できるオプションのCompressionModeパラメータを受け付けます。
C#以外の言語でもPDF圧縮機能は利用できますか?
はい、CompressPdfToBytes および CompressPdfToStream は、Java、Node.js、Python を含むすべての IronPDF プラットフォームで利用可能になりました。
C#でPDF圧縮を詳細に制御するにはどうすればよいですか?
AdvancedCompressionOptionsオブジェクトをCompressAndSaveAsに渡して、パイプラインを調整します。これはTargetImageDpiを介した画像DPIのダウンサンプリング、JpegQualityを介したJPEG再エンコード品質、およびzlibCompressionLevelを含みます。元のCompressAndSaveAs(outputPath, quality, removeStructureTree)オーバーロードはまだ動作し、以前と同じ出力を生成します。
How can I achieve fine-grained control over PDF compression in IronPDF?
For fine-grained control, IronPDF allows you to pass an AdvancedCompressionOptions object to the CompressAndSaveAs method, letting you adjust JPEG quality, image DPI downsampling, and zlib compression levels.
How do I compress black-and-white scans in IronPDF?
Set BitonalMode to BitonalCompressionMode.Force on an AdvancedCompressionOptions object and pass it to CompressAndSaveAs. IronPDF thresholds each eligible image to 1-bit and encodes it as CCITT Group 4, keeping the result only when it is smaller. It is off by default and is intended for scanned text, faxes and line art rather than colour or photographic content.
Why does my scan look softer after setting BitonalMode to Force?
BitonalResolutionDpi is null by default, which falls back to TargetImageDpi, and TargetImageDpi defaults to 150. Setting only BitonalMode therefore downsamples a 300 DPI scan to 150 DPI before thresholding it. Set BitonalResolutionDpi explicitly, or set TargetImageDpi to null, to threshold at the original resolution.
What does BitonalThreshold do in IronPDF?
BitonalThreshold is the luminance cutoff from 1 to 255 that decides whether a pixel becomes black or white. Leave it null, the default, and IronPDF computes a threshold per image using Otsu's method, which handles scans that vary in exposure. A value of 0, or anything outside 1 to 255, falls back to the automatic threshold.
Can I use bitonal compression with CompressPdfToBytes or CompressPdfToStream?
No. Bitonal compression is only available through the CompressAndSaveAs overloads that accept an AdvancedCompressionOptions object. CompressPdfToBytes and CompressPdfToStream take a JPEG quality value instead, so the in-memory paths cannot produce CCITT Group 4 output.
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。