在 Java 中添加 PDF 页面
Contents
[
Hide
]
Aspose.PDF for Java 让您可以插入空白页或从其他文档导入页面。
在特定位置插入空白页
当您需要在现有 PDF 的中间添加空白页时,请使用此示例。
- 打开源 PDF Document。
- 在页面集合中将新页面插入目标位置。
- 保存更新后的文档。
public static void insertEmptyPage(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.getPages().insert(2);
document.save(outputFile.toString());
}
}
在末尾追加一个空白页
当您需要在文档末尾添加一个新的空白页时,请使用此示例。
- 打开源 PDF Document。
- 向页面集合的末尾添加一个新页面。
- 保存修改后的 PDF。
public static void addEmptyPageToEnd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.getPages().add();
document.save(outputFile.toString());
}
}
从另一个文档添加页面
当您想将一个 PDF 的页面导入到另一个 PDF 时,请使用此示例。
- 创建目标 Document 并打开源文档。
- 添加任何所需的目标内容,并从源 PDF 导入目标页面。
- 保存生成的文档。
public static void addPageFromAnotherDocument(Path inputFile, Path outputFile) {
try (Document document = new Document();
Document anotherDocument = new Document(inputFile.toString())) {
document.getPages().add().getParagraphs().add(new TextFragment("This is first page!"));
document.getPages().add(anotherDocument.getPages().get_Item(1));
document.save(outputFile.toString());
}
}