PDFサイズを削減する方法(Java) – JPG品質の最適化

Balancing file size and visual fidelity is a common challenge when working with PDFs. In this tutorial you’ll discover how to reduce PDF size Java by adjusting the JPG image quality inside PDF documents using GroupDocs Viewer for Java. We’ll walk through the setup, code implementation, and practical tips so you can confidently compress PDF images without sacrificing readability.

GroupDocs.Viewer for Java を使用した PDF の JPG品質最適化

クイック回答

  • 「reduce PDF size Java」とは何ですか? It means lowering image quality, applying compression, and optimizing resources so the final PDF occupies less storage and loads faster.
  • JPG品質を制御する設定はどれですか? PdfViewOptions.setJpgQuality(byte quality) where the value ranges from 0 (lowest) to 100 (highest).
  • 同じフローで PPTX を PDF に変換することもできますか? Yes—point the Viewer at a .pptx source and the same options apply.
  • Web 公開向けの一般的な品質レベルはどれですか? A value around 50‑70 delivers a good balance of clarity and size for most web scenarios.
  • この機能にはライセンスが必要ですか? A free trial works for evaluation; a permanent GroupDocs Viewer license is required for production use.

「reduce PDF size Java」とは何か?

Reducing PDF size Java refers to the process of shrinking PDF files within Java applications by compressing embedded resources, especially raster images. Lowering JPG quality directly cuts the bulk of a PDF’s footprint, often delivering 30‑70 % size reductions while preserving readable text.

なぜ GroupDocs Viewer で JPG 品質を調整するのか?

Adjusting JPG quality with GroupDocs Viewer gives you a single‑pass, server‑side solution that eliminates the need for an external image‑processing step. The library supports 50+ input formats and can handle PDFs with hundreds of pages without loading the entire file into memory, resulting in faster conversions and lower memory consumption.

前提条件

  • GroupDocs.Viewer for Java バージョン 25.2 以上。
  • Maven ベースの Java プロジェクト(JDK 8 以上)。
  • Java と PDF 処理の基本的な知識。

GroupDocs.Viewer for Java のセットアップ

Add the GroupDocs repository and dependency to your pom.xml:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/viewer/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-viewer</artifactId>
      <version>25.2</version>
   </dependency>
</dependencies>

Pro tip: Keep the version up‑to‑date to benefit from performance improvements and new compression options.

実装ガイド

手順 1: 出力ディレクトリのパスを解決する

Create a helper class that builds the output folder where the PDF will be saved.

import java.nio.file.Path;
import java.nio.file.Paths;

public class FeatureResolveOutputDirectoryPath {
    public static Path getOutputDirectoryPath(String subdirectory) {
        String directory = Paths.get("YOUR_OUTPUT_DIRECTORY", "AdjustQualityOfJpgImages", subdirectory).toString();
        
        try {
            return Paths.get(directory);
        } catch (IOException e) {
            throw new RuntimeException("Failed to create output directory.", e);
        }
    }
}

手順 2: PdfViewOptions を目的の JPG品質で設定する

PdfViewOptions is the configuration object that tells GroupDocs how to render the output PDF.
The setJpgQuality(byte quality) method specifies the compression level for all JPG images that appear in the resulting document.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;

public class FeatureAdjustQualityOfJpgImages {
    public static void run() {
        Path outputDirectory = FeatureResolveOutputDirectoryPath.getOutputDirectoryPath("YOUR_DOCUMENT_DIRECTORY");
        Path filePath = outputDirectory.resolve("output.pdf");

        PdfViewOptions viewOptions = new PdfViewOptions(filePath);
        
        // Set desired JPG quality (0-100 scale)
        byte quality = 10;
        viewOptions.setJpgQuality(quality);

        try (Viewer viewer = new Viewer("YOUR_DOCUMENT_DIRECTORY/source.pptx")) {
            viewer.view(viewOptions);
        }
    }
}

Explanation:

  • 値が低いほどファイルは小さくなりますが、視覚的な鮮明さが低下する可能性があります。
  • この例では source.pptx を使用して、convert PPTX to PDF Java を実演しながら画像を同時に圧縮しています。

手順 3: コードを実行し結果を検証する

FeatureAdjustQualityOfJpgImages is a sample class that runs the conversion with the configured JPG quality. Execute FeatureAdjustQualityOfJpgImages.run(). The generated output.pdf will contain JPG images at the quality level you specified, effectively compressing PDF images and reducing overall file size.

よくある問題とトラブルシューティング

  • ファイルパスが正しくない: 作業ディレクトリに対して source.pptx が存在することを確認してください。
  • 権限不足: 出力フォルダーは書き込み可能である必要があります。そうでない場合 RuntimeException がスローされます。
  • 予期せず大きな PDF: quality の値がサイズ目標に対して十分に低いか確認してください。

実用的な活用例

  1. 文書アーカイブ: 小さな PDF はストレージコストを削減し、検索速度を向上させます。
  2. Web 公開: PDF が埋め込まれたりリンクされたりする際のページ読み込みが高速化します。
  3. メール添付: 送信前に画像品質を下げることで、一般的なサイズ制限を満たせます。

パフォーマンス上の考慮点

  • バッチ処理: 大量の場合は、メモリ使用量を監視しながら並列スレッドで文書を処理します。
  • 最適な品質設定: 印刷用 PDF には高品質 (80‑100) を、Web プレビューには 30‑50 が十分なことが多いです。

結論

You now know how to reduce PDF size Java by adjusting JPG image quality with GroupDocs Viewer. Experiment with different quality levels, integrate the code into your existing pipelines, and enjoy faster, lighter PDFs.

次のステップ

  • さまざまな品質設定をテストし、ユースケースに最適なバランスを見つけます。
  • 透かしやパスワード保護など、他の GroupDocs 機能も調査します。

よくある質問

Q: JPG 品質を調整するとファイルサイズにどのような影響がありますか?
A: JPG 品質を下げると、各画像に保存されるデータ量が減少し、PDF のサイズが 30‑70 % 縮小されることがありますが、テキストは鮮明に保たれます。

Q: JPG 以外のフォーマットの画像品質も調整できますか?
A: この設定は JPG 画像のみを対象とします。他のラスタ形式は GroupDocs Viewer 内のそれぞれの圧縮オプションがあります。

Q: Web 用の理想的な JPG 品質設定は何ですか?
A: 品質値を 50〜70 の間に設定すると、ほとんどの Web アプリケーションで適度なファイルサイズと明瞭な画像が得られます。

Q: バッチワークフローでこのプロセスを自動化できますか?
A: はい、ディレクトリ内のソースファイルをループし、同じ PdfViewOptions 設定を適用して、並列に圧縮 PDF を生成できます。

Q: 本番環境での展開にはライセンスが必要ですか?
A: はい、製品版の使用には有効な GroupDocs Viewer ライセンスが必要です。評価用に無料トライアルがあります。

Q: 実際の品質低下をどのように確認できますか?
A: 変換前後のファイルサイズを比較し、PDF を開いて画像の鮮明さを目視で確認してください。サイズ差は設定した品質レベルを反映します。

Q: 個別ページごとに異なる品質レベルを設定できますか?
A: 現在、GroupDocs Viewer は変換ごとに均一な JPG 品質設定を適用します。ページ単位で制御したい場合は、専用の画像ライブラリでの後処理が必要です。

リソース


最終更新日: 2026-08-13
テスト環境: GroupDocs.Viewer 25.2 for Java
作者: GroupDocs

関連チュートリアル