ページ要素
このガイドでは、Aspose.PDF FOSS for Java におけるページジオメトリ、アーティファクト装飾、埋め込みファイル添付の扱い方を示します。getMediaBox() を使用してページ寸法を読み取り、Artifact オブジェクトを列挙し、EmbeddedFileCollection を管理する方法を学びます。
ページジオメトリ
各 Page はボックスプロパティを介してジオメトリを公開します:
getMediaBox()— ページ全体の境界getCropBox()— 可視領域getRotate()— ページの回転角度(0、90、180、または270)
メディアボックスから幅と高さを読み取り、コンテンツ配置の位置を計算します:
try (Document doc = new Document("input.pdf")) {
PageCollection pages = doc.getPages();
Page page = pages.get(1);
double width = page.getMediaBox().getWidth();
double height = page.getMediaBox().getHeight();
System.out.println("Page size: " + width + " x " + height + " pt");
}標準的な A4 は 595 × 842 ポイント、US Letter は 612 × 792 ポイントです。
Artifacts
Artifact クラスはページ装飾(ヘッダー、フッター、透かし、背景画像)をモデル化します。アーティファクトは PDF 仕様で定義されたコンテンツではない要素です。
ページのアーティファクトコレクションを反復処理し、各装飾のタイプとサブタイプを調べます:
try (Document doc = new Document("input.pdf")) {
PageCollection pages = doc.getPages();
Page page = pages.get(1);
for (Artifact artifact : page.getArtifacts()) {
System.out.println(artifact.getArtifactType() + " / " + artifact.getSubtype());
}
}埋め込みファイル
EmbeddedFileCollection は Document.getEmbeddedFiles() からアクセスでき、PDF ドキュメントに埋め込まれたファイル添付を管理します。添付ファイルの数を数えるには size() を使用してください。
try (Document doc = new Document("document.pdf")) {
EmbeddedFileCollection files = doc.getEmbeddedFiles();
int count = files.size();
System.out.println("Embedded files: " + count);
}ページ数
ドキュメントの総ページ数を取得するには、PageCollection.getCount() または PageCollection.size() を使用します:
try (Document doc = new Document("input.pdf")) {
int pageCount = doc.getPages().getCount();
System.out.println("Total pages: " + pageCount);
}