使用 Java 从 PDF 文件中提取图像
Contents
[
Hide
]
Aspose.PDF for Java 支持直接图像资源提取和基于位置的过滤。
按索引提取嵌入的图像
当您需要从 PDF 页面保存特定图像资源时,请使用此示例。
public static void extractImage(Path inputFile, Path outputFile) throws Exception {
try (Document document = new Document(inputFile.toString());
OutputStream outputImage = Files.newOutputStream(outputFile)) {
XImage image = document.getPages().get_Item(1).getResources().getImages().get_Item(1);
image.save(outputImage);
}
}
从特定页面区域提取图像
当仅需导出位于选定矩形内部的图像时,请使用此示例。
- 定义目标 Rectangle 并打开源 PDF。
- 使用 ImagePlacementAbsorber 以检查页面上的图像位置。
- 仅保存放置位置位于选定区域内的图像。
public static void extractImageFromSpecificRegion(Path inputFile, Path outputFile) throws Exception {
Rectangle rectangle = new Rectangle(0, 0, 590, 590, true);
try (Document document = new Document(inputFile.toString())) {
ImagePlacementAbsorber absorber = new ImagePlacementAbsorber();
document.getPages().get_Item(1).accept(absorber);
int index = 1;
for (ImagePlacement imagePlacement : absorber.getImagePlacements()) {
Point point1 = new Point(imagePlacement.getRectangle().getLLX(), imagePlacement.getRectangle().getLLY());
Point point2 = new Point(imagePlacement.getRectangle().getURX(), imagePlacement.getRectangle().getURX());
if (rectangle.contains(point1, true) && rectangle.contains(point2, true)) {
Path indexedOutputFile = Path.of(outputFile.toString().replace("index", String.valueOf(index)));
try (OutputStream outputImage = Files.newOutputStream(indexedOutputFile)) {
imagePlacement.getImage().save(outputImage);
}
index++;
}
}
}
}