Java で PDF を画像形式に変換
Contents
[
Hide
]
Aspose.PDF for Java では、形式固有のデバイスオプションを使用して、PDF ページをラスタ画像およびベクタ画像フォーマットにレンダリングできます。
PDF から BMP への変換
PDF ページを BMP 画像としてレンダリングする必要がある場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 - 300 DPI の
BmpDeviceとResolutionを作成してください。 document.getPages()を繰り返し処理し、各ページに対してdevice.process(...)を呼び出してください。- 生成された BMP 画像を番号付きの出力パスに保存してください。
public static void convertPdfToBmp(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
BmpDevice device = new BmpDevice(new Resolution(300));
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "bmp"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から EMF への変換
PDF ページを EMF ベクター画像としてエクスポートする必要がある場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 EmfDeviceとResolutionを 300 DPI で作成してください。- ページを反復処理し、各ページに対して
device.process(...)を呼び出してください。 - EMF 出力を番号付きのファイルパスに保存してください。
public static void convertPdfToEmf(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
EmfDevice device = new EmfDevice(new Resolution(300));
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "emf"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から GIF への変換
PDF ページを GIF 画像に変換する必要がある場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 GifDeviceとResolutionを 300 DPI で作成してください。- ページを反復処理し、各ページをレンダリングするために
device.process(...)を呼び出してください。 - GIF ファイルを番号付きの出力パスに保存してください。
public static void convertPdfToGif(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
GifDevice device = new GifDevice(new Resolution(300));
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "gif"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から JPEG への変換
PDF ページを JPEG 画像としてエクスポートする必要がある場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 - 300 DPI の
Resolutionを指定してJpegDeviceを作成してください。 - ページを反復処理し、各ページを JPEG にラスタライズするために
device.process(...)を呼び出してください。 - JPEG 出力ファイルを番号付きのパスに保存してください。
public static void convertPdfToJpeg(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
JpegDevice device = new JpegDevice(new Resolution(300));
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "jpeg"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から PNG への変換
PDF ページを PNG 画像に変換する場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 - 300 DPI の
Resolutionを指定してPngDeviceを作成してください。 - ページを反復処理し、各 PDF ページごとに
device.process(...)を呼び出してください。 - PNG 出力を番号付きのファイルパスに保存してください。
public static void convertPdfToPng(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
PngDevice device = new PngDevice(new Resolution(300));
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "png"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から PNG への変換(デフォルトのフォントフォールバックの使用)
レンダリングで欠落したグリフに対してフォールバックフォントを使用すべき場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 - 300 DPI の
Resolutionを指定してPngDeviceを作成してください。 document.setAbsentFontTryToSubstitute(true)を有効にして、欠落したグリフがレンダリング中に代替フォントにフォールバックできるようにしてください。- ページをレンダリングし、PNG ファイルを保存してください。
public static void convertPdfToPngWithDefaultFont(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
PngDevice device = new PngDevice(new Resolution(300));
document.setAbsentFontTryToSubstitute(true);
for (int page = 1; page <= document.getPages().size(); page++) {
device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "png"));
}
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から SVG への変換
PDF ページを SVG グラフィックとしてエクスポートする必要がある場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 SvgSaveOptionsを作成し、生の.svg出力が必要な場合は ZIP 圧縮を無効にしてください。setTreatTargetFileNameAsDirectory(true)を有効にして、ページごとの SVG 出力をターゲット パスの下に整理できるようにしてください。- SVG 出力を保存してください。
public static void convertPdfToSvg(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
SvgSaveOptions saveOptions = new SvgSaveOptions();
saveOptions.setCompressOutputToZipArchive(false);
saveOptions.setTreatTargetFileNameAsDirectory(true);
document.save(outputPrefix + ".svg", saveOptions);
}
System.out.println(inputFile + " converted into " + outputPrefix);
}
PDF から TIFF への変換
PDF ページが 1 つ以上 TIFF にエクスポートされる場合は、この例を使用してください。
- ソース PDF を
Documentインスタンスで開いてください。 TiffSettingsを作成し、圧縮方式、色深度、および空白ページの処理を設定してください。- 300 DPI の
Resolutionと準備した TIFF 設定を指定して、TiffDeviceを作成してください。 - ページをレンダリングし、TIFF 出力を保存してください。
public static void convertPdfToTiff(Path inputFile, Path outputPrefix) {
try (Document document = new Document(inputFile.toString())) {
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.setCompression(CompressionType.LZW);
tiffSettings.setDepth(ColorDepth.Default);
tiffSettings.setSkipBlankPages(false);
TiffDevice tiffDevice = new TiffDevice(new Resolution(300), tiffSettings);
tiffDevice.process(document, outputPrefix + ".tiff");
}
System.out.println(inputFile + " converted into " + outputPrefix);
}