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());
}
}