# IronPDFを使ってC#でPDFを圧縮する方法
IronPDFはC#で強力なPDF圧縮機能を提供し、埋め込み画像の圧縮やPDFのツリー構造の最適化によってファイルサイズを縮小し、品質を維持しながら保存や共有のためにドキュメントをより管理しやすくします。
PDF圧縮は、PDFドキュメントのファイルサイズを縮小し、保存、共有、送信をより管理しやすくします。 [HTMLから生成されたPDF](https://ironpdf.com/how-to/html-string-to-pdf/)でも、[既存のPDFファイル](https://ironpdf.com/how-to/pdf-memory-stream/)でも、圧縮はドキュメントの処理を最適化するために不可欠です。
通常、PDFファイルサイズの大半は画像で占められています。 IronPDFは埋め込み画像のサイズを縮小し、テーブルの多いPDFによく見られるツリー構造を最適化する圧縮機能を提供します。 これらのテクニックは、[URLからPDFへの変換](https://ironpdf.com/how-to/url-to-pdf/)や[HTMLファイルへの変換](https://ironpdf.com/how-to/html-file-to-pdf/)など、さまざまな方法で作成されたPDFでシームレスに動作します。
*as-heading:2(クイックスタート: IronPDFで PDF ファイルを圧縮する )*
IronPDFの圧縮ツールを使ってPDFファイルサイズを縮小します。 `PdfDocument.FromFile` を使用して PDF を読み込み、`CompressAndSaveAs` を呼び出すことで、最適化された PDF を 1 ステップで圧縮・保存できます。このプロセスにより、品質を維持しながらファイルサイズを大幅に削減できます。
```cs
:title=Compress your PDF in one line!
PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40);
```
<div class="hsg-featured-snippet">
<h3>最小限のワークフロー(3ステップ)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://www.nuget.org/packages/IronPdf/">PDF圧縮用のC#ライブラリをNuGetからダウンロードする</a>。</li>
<li>既存のPDFをインポートするか、新しいPDFをレンダリングする</li>
<li><code>CompressAndSaveAs(outputPath, quality, removeStructureTree)</code>を呼び出し、画像を圧縮し、必要に応じて構造ツリーを除去して、すべてを一度の手順で保存します。</li>
</ol>
</div>
<br class="clear" />
[[i:( `CompressStructTree`、および `Compress(CompressionOptions)` などの従来の圧縮方式は非推奨となっています。)]]
## PDF内の画像を圧縮するにはどうすればよいですか?
JPEG圧縮品質は、0(最大圧縮)から100(最小損失)までの値を受け付け、その値の適用方法は基となるエンジンによって決定されます。 これらのレベルを理解することは、[埋め込み画像](https://ironpdf.com/how-to/add-images-to-pdfs/)を含むPDFを扱うときや、[フォントやグラフィックを管理する](https://ironpdf.com/how-to/manage-fonts/)必要があるときに役立ちます。
- 90%以上:高品質
- 80%-90%: 中程度の品質
- 70%~80%: 低品質
品質とファイルサイズのバランスをとるために、さまざまな値を試してください。画質の低下は、入力画像の種類によって異なります。ある画像は、他の画像よりも明瞭度の低下が目立ちます。 これは、[Azure Blob Storage](https://ironpdf.com/how-to/images-azure-blob-storage/) からの画像を扱う場合や、[カスタム透かし](https://ironpdf.com/how-to/custom-watermark/)を入れた PDF を作成する場合に特に重要です。
```csharp
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);
```
### 画像圧縮にはどのような効果が期待できますか?
**39.24%**削減されました!
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/pdf/how-to/pdf-compression/compress-image-compare-size.png" alt="圧縮.pdf(449 KB)と非圧縮.pdf(739 KB)の画像圧縮結果を示すファイル比較" class="img-responsive add-shadow" />
</div>
</div>
`quality` 内の `CompressAndSaveAs` パラメータは、埋め込まれたすべての画像に適用される JPEG エンコード品質を制御します。 0(最大圧縮、最低品質)から100(最小圧縮、最高品質)までの整数を指定するか、`null`を指定して画像圧縮を完全にスキップしてください。
IronPDFは圧縮中に自動的に画像を再エンコードしスケールダウンするため、追加のオプションは必要ありません。 低い値はファイルを小さくしますが、目に見えるアーティファクトが発生する可能性があります。コンテンツに最適なバランスを見つけるために実験してください。
<hr />
## PDFのツリー構造を圧縮するには?
この機能は、Chromeエンジンによって生成されたツリー構造を削除することで、PDFのサイズを縮小します。これを有効にするには、`CompressAndSaveAs`の3番目のパラメータとして渡してください。 これは、Chromeエンジンを使用して生成された広範なテーブルデータを含むPDFで最も効果的です。 レンダリングエンジンによっては、この構造を持たずにPDFを出力するものもあるため、そのような場合にはこの機能は効果的ではありません。 このテクニックは、複雑なレイアウトを扱うときや、冗長な構造要素を持つ[複数のPDF](https://ironpdf.com/how-to/merge-or-split-pdfs/)をマージするときに役立ちます。
ツリー構造を削除すると、テキストの強調表示や抽出の効果に影響する可能性があります。 圧縮を適用するとき、特にユーザーが[圧縮されたPDFからテキストや画像を取り出す](https://ironpdf.com/how-to/extract-text-and-images/)必要がある場合は、このトレードオフを考慮してください。
Test `CompressAndSaveAs` with tree structure removal using this <a href="/static-assets/pdf/how-to/pdf-compression/table.pdf" download='table.pdf'>PDF with table data</a>.
```csharp
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);
```
### ツリー構造圧縮はどのようなファイル サイズ削減を実現できますか?
**67.90%**削減しました! より大きなテーブルPDFでこの割合は増加します。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<img src="/static-assets/pdf/how-to/pdf-compression/compress-tree-structure-compare-size.png" alt="圧縮結果を示すcompressedTable.pdf(52 KB)とtable.pdf(162 KB)のファイル比較" class="img-responsive add-shadow" />
</div>
</div>
## 両方の圧縮方法を一緒に適用するにはどうすればよいですか?
`CompressAndSaveAs` は、1回の呼び出しで画像とツリー構造の両方の圧縮を処理します。`quality` の値を渡し、`removeStructureTree` を `true` に設定してください。 このような複合的なアプローチは、画像や構造化データを含む複雑なPDF(図表を含むレポートや、表やグラフィックが埋め込まれた文書など)に対して特に効果的です。
```csharp
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);
```
### `CompressAndSaveAs` はどのようなパラメータを受け付けますか?
- `outputPath` (文字列): 圧縮されたPDFが保存されるファイルパス。
- `quality` (int?, 0–100): 埋め込まれたすべての画像に適用されるJPEGの画質。 低い値はより小さいファイルを生成します。 `null` を渡すと、画像の圧縮を完全にスキップします。
- `removeStructureTree` (bool, デフォルト `false`): `true` の場合、PDF 構造ツリーを削除してファイルサイズをさらに縮小します。表を含む HTML 生成の PDF で最も効果的です。
圧縮を適用する場合は、使用ケースを考慮してください。 アーカイブの目的は、ファイルサイズよりも品質を優先する場合があります。ウェブ配信の場合は、ファイルサイズを小さくする必要があります。 [PDF/A準拠文書](https://ironpdf.com/how-to/pdfa/)と標準PDFのどちらで作業しているかに基づいて、設定を微調整します。
## ディスクに保存せずにPDFを圧縮するにはどうすればよいですか?
APIレスポンス、クラウド関数、メール添付ファイルなど、多くの実際のシナリオでは、圧縮されたPDFをディスクに書き込むことは不要または非現実的です。
`CompressPdfToBytes` は、圧縮された PDF をバイト配列として返すため、出力の保存や送信方法を完全に制御できます。 `CompressPdfToBytes` および `CompressPdfToStream` の両方は、圧縮の方法を制御するオプションの `CompressionMode` パラメータを受け付けます。
```csharp
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);
```
### `CompressionMode` どのようなオプションが利用可能ですか?
展開環境によって、ディスクアクセスやパフォーマンスに関する制約は異なります。 `CompressionMode` 列挙型を使用することで、適切なトレードオフを選択できます。
- `Automatic` (デフォルト): システムの能力に基づいて最適な圧縮方式を選択します。 まず `HighQuality` を試行し、ディスクアクセスが制限されている場合は `FastMemory` にフォールバックします。
- `FastMemory`: ディスク I/O を一切行わず、メモリ内でのみ圧縮を実行します。 最速のオプションですが、画像のリサンプリングやダウンサンプリングは適用されないため、出力ファイルサイズが大きくなります。
- `HighQuality`: 一時的なディスクアクセスを使用して、最大限の圧縮を行います。 画像の最適化を最大限に行い、最小サイズの出力ファイルを生成します。 システムの一時ディレクトリへの書き込み権限が必要です。
バイト配列の代わりに `Stream` が必要な場合は、`CompressPdfToStream` を使用してください。 これは、HTTPレスポンス、クラウドストレージへのアップロード、またはストリームベースのワークフローに直接パイプする場合に便利です。
```csharp
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);
```
`CompressPdfToBytes` および `CompressPdfToStream` は、Java、Node.js、Pythonを含むすべてのIronPDFプラットフォームで利用可能になりました。
次に何ができるのかを見てみましょうか? チュートリアルのページはこちらをご覧ください:[追加機能](https://ironpdf.com/tutorials/pdf-assets-and-performance-csharp/)。
IronPDFの圧縮ツールを使ってPDFファイルサイズを縮小します。 PdfDocument.FromFile を使用して PDF を読み込み、CompressAndSaveAs を呼び出すことで、最適化された PDF を 1 ステップで圧縮・保存できます。このプロセスにより、品質を維持しながらファイルサイズを大幅に削減できます。
品質とファイルサイズのバランスをとるために、さまざまな値を試してください。画質の低下は、入力画像の種類によって異なります。ある画像は、他の画像よりも明瞭度の低下が目立ちます。 これは、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 PDFspdf.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);
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 PDFspdf.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);
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 reductionpdf.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);
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 filebyte[] 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);
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 destinationusing 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);
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.