Java を使用した PDF からの画像抽出
Contents
[
Hide
]
埋め込みグラフィックを再利用したり、ドキュメント資産を検査したり、下流処理のために画像をエクスポートしたりする必要がある場合に、PDF ページから画像を抽出します。
- ソース PDF を Document インスタンスで開き、抽出された画像ファイル用の出力ストリームを開いてください。
- 対象の Page をドキュメントから取得し、その
Resources.Imagesコレクションにアクセスしてください。 - 必要な XImage オブジェクトを、その画像コレクションからインデックスで取得してください。
image.save(outputImage)を呼び出して、抽出された画像バイトをターゲットストリームに書き込んでください。
public static void extractImage(Path inputFile, Path outputFile) throws Exception {
try (Document document = new Document(inputFile.toString());
OutputStream outputImage = Files.newOutputStream(outputFile)) {
XImage image = document.getPages().get_Item(1).getResources().getImages().get_Item(1);
image.save(outputImage);
}
}