Convert PDF to Image Formats in Java
Contents
[
Hide
]
Aspose.PDF for Java can render PDF pages to raster and vector image formats with format-specific device options.
Convert PDF to BMP
Use this example when PDF pages should be rendered as BMP images.
- Open the source PDF in a
Documentinstance. - Create a
BmpDevicewith aResolutionof 300 DPI. - Iterate through
document.getPages()and calldevice.process(...)for each page. - Save the generated BMP images to numbered output paths.
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);
}
Convert PDF to EMF
Use this example when PDF pages should be exported as EMF vector images.
- Open the source PDF in a
Documentinstance. - Create an
EmfDevicewith aResolutionof 300 DPI. - Iterate through the pages and call
device.process(...)for each page. - Save the EMF outputs to numbered file paths.
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);
}
Convert PDF to GIF
Use this example when PDF pages should be converted into GIF images.
- Open the source PDF in a
Documentinstance. - Create a
GifDevicewith aResolutionof 300 DPI. - Iterate through the pages and call
device.process(...)to render each page. - Save the GIF files to numbered output paths.
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);
}
Convert PDF to JPEG
Use this example when PDF pages should be exported as JPEG images.
- Open the source PDF in a
Documentinstance. - Create a
JpegDevicewith aResolutionof 300 DPI. - Iterate through the pages and call
device.process(...)to rasterize each page to JPEG. - Save the JPEG output files to numbered paths.
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);
}
Convert PDF to PNG
Use this example when PDF pages should be converted into PNG images.
- Open the source PDF in a
Documentinstance. - Create a
PngDevicewith aResolutionof 300 DPI. - Iterate through the pages and call
device.process(...)for each PDF page. - Save the PNG outputs to numbered file paths.
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);
}
Convert PDF to PNG with a default font fallback
Use this example when rendering should use a fallback font for missing glyphs.
- Open the source PDF in a
Documentinstance. - Create a
PngDevicewith aResolutionof 300 DPI. - Enable
document.setAbsentFontTryToSubstitute(true)so missing glyphs can fall back to substitute fonts during rendering. - Render the pages and save the PNG files.
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);
}
Convert PDF to SVG
Use this example when PDF pages should be exported as SVG graphics.
- Open the source PDF in a
Documentinstance. - Create
SvgSaveOptionsand disable ZIP compression when raw.svgoutput is required. - Enable
setTreatTargetFileNameAsDirectory(true)so per-page SVG output can be organized under the target path. - Save the SVG output.
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);
}
Convert PDF to TIFF
Use this example when one or more PDF pages should be exported to TIFF.
- Open the source PDF in a
Documentinstance. - Create
TiffSettingsand configure compression, color depth, and blank-page behavior. - Create a
TiffDevicewith aResolutionof 300 DPI and the prepared TIFF settings. - Render the pages and save the TIFF output.
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);
}