import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/composite.pdf"));
// Extract all the pages from the PDF file.
List<BufferedImage> extractedImages = pdf.toBufferedImages();
// With the ToImageOptions object, specify maximum image dimensions for each
// extracted image, as well as their DPI
ToImageOptions rasterOptions = new ToImageOptions();
rasterOptions.setImageMaxHeight(100);
rasterOptions.setImageMaxWidth(100);
// Call the toBufferedImage method along with a PageSelection object to choose the pages from which to
// extract the images
//
// Available PageSelection methods include: allPages, lastPage, firstPage, singlePage(int pageIndex),
// and pageRange(int startingPage, int endingPage)
List<BufferedImage> sizedExtractedImages = pdf.toBufferedImages(rasterOptions, PageSelection.allPages());
// Save all the extracted images to a file location
int i = 1;
for (BufferedImage extractedImage : sizedExtractedImages) {
String fileName = "assets/images/" + i++ + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
}
將 PDF 光柵化為圖像
一旦從文件中加載,從源內容轉換 (網頁、URL、HTML等...)或通过添加邊距、頁眉、頁腳或其他自訂功能來修改,IronPDF 可以将 PDF 文件的頁面导出为图像,这些图像可以保存到文件系统中、存储在数据库中或通过网络传送 (除其他用途外)toBufferedImages 方法返回一個包含 BufferedImage 物件的集合的 List。每個 BufferedImage 包含原始 PdfDocument 的單個頁面的圖像位元組內容。此外,BufferedImage 列表按頁碼升序排列。開發人員可以在方法調用中包含一個 PageSelection 物件作為參數,以生成 PDF 中一組頁面的這個列表。