Mengonversi PDF ke Format gambar di Java
Contents
[
Hide
]
Aspose.PDF for Java dapat merender halaman PDF ke format gambar raster dan vektor dengan opsi perangkat khusus format.
Mengonversi PDF ke BMP
Gunakan contoh ini ketika halaman PDF harus dirender sebagai gambar BMP.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
BmpDevicedenganResolutiondari 300 DPI. - Iterasikan melalui
document.getPages()dan panggildevice.process(...)untuk setiap halaman. - Simpan gambar BMP yang dihasilkan ke jalur output yang bernomor.
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);
}
Mengonversi PDF ke EMF
Gunakan contoh ini ketika halaman PDF harus diekspor sebagai gambar vektor EMF.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
EmfDevicedenganResolutiondari 300 DPI. - Iterasikan melalui halaman dan panggil
device.process(...)untuk setiap halaman. - Simpan output EMF ke jalur file bernomor.
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);
}
Mengonversi PDF ke GIF
Gunakan contoh ini ketika halaman PDF harus diubah menjadi gambar GIF.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
GifDevicedenganResolutiondari 300 DPI. - Iterasikan melalui halaman dan panggil
device.process(...)untuk merender setiap halaman. - Simpan file GIF ke jalur output yang diberi nomor.
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);
}
Mengonversi PDF ke JPEG
Gunakan contoh ini ketika halaman PDF harus diekspor sebagai gambar JPEG.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
JpegDevicedenganResolutiondari 300 DPI. - Iterasikan melalui halaman dan panggil
device.process(...)untuk meraster setiap halaman ke JPEG. - Simpan file output JPEG ke jalur bernomor.
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);
}
Mengubah PDF ke PNG
Gunakan contoh ini ketika halaman PDF harus dikonversi menjadi gambar PNG.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
PngDevicedenganResolutiondari 300 DPI. - Iterasikan melalui halaman dan panggil
device.process(...)untuk setiap halaman PDF. - Simpan keluaran PNG ke jalur file yang bernomor.
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);
}
Mengonversi PDF ke PNG dengan fallback font default
Gunakan contoh ini ketika rendering harus menggunakan font fallback untuk glyph yang hilang.
- Buka PDF sumber dalam sebuah instans
Document. - Buat sebuah
PngDevicedenganResolutiondari 300 DPI. - Aktifkan
document.setAbsentFontTryToSubstitute(true)sehingga glif yang hilang dapat kembali ke font pengganti selama proses rendering. - Render halaman dan simpan file 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);
}
Mengonversi PDF ke SVG
Gunakan contoh ini ketika halaman PDF harus diekspor sebagai grafik SVG.
- Buka PDF sumber dalam sebuah instans
Document. - Buat
SvgSaveOptionsdan nonaktifkan kompresi ZIP saat mentah.svgoutput diperlukan. - Aktifkan
setTreatTargetFileNameAsDirectory(true)jadi output SVG per halaman dapat diatur di bawah jalur target. - Simpan output 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);
}
Mengonversi PDF ke TIFF
Gunakan contoh ini ketika satu atau lebih halaman PDF harus diekspor ke TIFF.
- Buka PDF sumber dalam sebuah instans
Document. - Buat
TiffSettingsdan mengkonfigurasi kompresi, kedalaman warna, dan perilaku halaman kosong. - Buat sebuah
TiffDevicedenganResolutionberukuran 300 DPI dan pengaturan TIFF yang disiapkan. - Render halaman dan simpan output 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);
}