Mengonversi PDF ke Format gambar di Java

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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah BmpDevice dengan Resolution dari 300 DPI.
  3. Iterasikan melalui document.getPages() dan panggil device.process(...) untuk setiap halaman.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah EmfDevice dengan Resolution dari 300 DPI.
  3. Iterasikan melalui halaman dan panggil device.process(...) untuk setiap halaman.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah GifDevice dengan Resolution dari 300 DPI.
  3. Iterasikan melalui halaman dan panggil device.process(...) untuk merender setiap halaman.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah JpegDevice dengan Resolution dari 300 DPI.
  3. Iterasikan melalui halaman dan panggil device.process(...) untuk meraster setiap halaman ke JPEG.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah PngDevice dengan Resolution dari 300 DPI.
  3. Iterasikan melalui halaman dan panggil device.process(...) untuk setiap halaman PDF.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat sebuah PngDevice dengan Resolution dari 300 DPI.
  3. Aktifkan document.setAbsentFontTryToSubstitute(true) sehingga glif yang hilang dapat kembali ke font pengganti selama proses rendering.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat SvgSaveOptions dan nonaktifkan kompresi ZIP saat mentah .svg output diperlukan.
  3. Aktifkan setTreatTargetFileNameAsDirectory(true) jadi output SVG per halaman dapat diatur di bawah jalur target.
  4. 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.

  1. Buka PDF sumber dalam sebuah instans Document.
  2. Buat TiffSettings dan mengkonfigurasi kompresi, kedalaman warna, dan perilaku halaman kosong.
  3. Buat sebuah TiffDevice dengan Resolution berukuran 300 DPI dan pengaturan TIFF yang disiapkan.
  4. 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);
}