Java での PDF ページサイズの変更

Aspose.PDF for Java は、ページ寸法の取得だけでなく、更新も可能です。

ページサイズの変更

既存のページのサイズを変更し、変更前後のページボックスを検査する必要がある場合は、この例を使用してください。

  1. ソース PDF の Document を開いてください。
  2. 対象の Page オブジェクトを取得し、現在のボックス値を出力してください。
  3. 新しいページサイズを設定し、ドキュメントを保存してください。
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());
    }
}

ページサイズの取得

ページの表示寸法を読み取る必要がある場合は、この例を使用してください。

  1. ソース PDF の Document を開いてください。
  2. 回転処理を有効にしてページの矩形を取得してください。
  3. ページの幅と高さを出力してください。
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());
    }
}

回転を適用したページサイズの取得

回転を考慮する前後のページ寸法を比較する必要がある場合は、この例を使用してください。

  1. ソース PDF の Document を開いてください。
  2. 対象のPageを回転してください。
  3. ページの矩形を回転あり・なしで読み取り、両方の値を出力してください。
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());
    }
}