在 Java 中提取 PDF 页面
Contents
[
Hide
]
Aspose.PDF for Java 允许您将选定的页面复制到新目标文档中。
提取单页
当您需要将源 PDF 的单页保存为单独的文档时,请使用此示例。
- 打开源 PDF Document 并创建目标文档。
- 将目标页面复制到目标页面集合中。
- 保存新的 PDF。
public static void extractPage(Path inputFile, Path outputFile) {
try (Document srcDocument = new Document(inputFile.toString());
Document dstDocument = new Document()) {
dstDocument.getPages().add(srcDocument.getPages().get_Item(2));
dstDocument.save(outputFile.toString());
}
}
提取多个页面
当您需要将多个页面复制到单独的 PDF 时,请使用此示例。
- 打开源 PDF Document 并创建目标文档。
- 遍历所选页面索引并将其添加到目标文档中。
- 保存提取的页面文档。
public static void extractBunchPages(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString());
Document anotherDocument = new Document()) {
Integer[] pages = {2, 3};
for (Integer pageIndex : pages) {
anotherDocument.getPages().add(document.getPages().get_Item(pageIndex));
}
anotherDocument.save(outputFile.toString());
}
}