在 Java 中更改 PDF 页面尺寸
Contents
[
Hide
]
Aspose.PDF for Java 既可以报告页面尺寸,也可以更新它们。
更改页面尺寸
当您需要调整现有页面大小并在更改前后检查页面框时,请使用此示例。
public static void setPageSize(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
printBoxes("Before set", page);
page.setPageSize(597.6, 842.4);
printBoxes("After set", page);
document.save(outputFile.toString());
}
}
获取页面尺寸
当您需要读取页面的可见尺寸时,请使用此示例。
- 打开源 PDF Document。
- 获取页面矩形,并启用旋转处理。
- 输出页面宽度和高度。
public static void getPageSize(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
Rectangle rectangle = document.getPages().get_Item(1).getPageRect(true);
System.out.println(rectangle.getWidth() + " : " + rectangle.getHeight());
}
}
获取已应用旋转的页面尺寸
当您需要比较在考虑旋转前后的页面尺寸时,请使用此示例。
public static void getPageSizeRotation(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
page.setRotate(Rotation.on90);
Rectangle rectangle = page.getPageRect(false);
System.out.println(rectangle.getWidth() + " : " + rectangle.getHeight());
rectangle = page.getPageRect(true);
System.out.println(rectangle.getWidth() + " : " + rectangle.getHeight());
}
}