Java での PDF への画像スタンプの追加
Contents
[
Hide
]
Aspose.PDF for Java は、オーバーレイとしての画像スタンプおよび画像を背景としたレイアウト要素をサポートしています。
画像スタンプの追加
ページにカスタム配置と透明度を持つ画像スタンプを表示する必要がある場合は、この例を使用してください。
- ソース PDF の Document を開いてください。
- ImageStamp を作成し、外観を構成してください。
- ページにスタンプを追加し、ドキュメントを保存してください。
public static void addImageStamp(Path inputFile, Path imageFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
ImageStamp imageStamp = new ImageStamp(imageFile.toString());
imageStamp.setBackground(true);
imageStamp.setXIndent(100);
imageStamp.setYIndent(100);
imageStamp.setHeight(300);
imageStamp.setWidth(300);
imageStamp.setRotate(Rotation.on270);
imageStamp.setOpacity(0.5);
document.getPages().get_Item(1).addStamp(imageStamp);
document.save(outputFile.toString());
}
}
品質管理付きで画像スタンプの追加
ImageStamp のレンダリング品質を調整する必要がある場合は、この例を使用してください。
- ソース PDF の Document を開いてください。
- ImageStamp を作成し、品質値を設定してください。
- ページにスタンプを追加し、結果を保存してください。
public static void addImageStampWithQualityControl(Path inputFile, Path imageFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
ImageStamp imageStamp = new ImageStamp(imageFile.toString());
imageStamp.setQuality(10);
document.getPages().get_Item(1).addStamp(imageStamp);
document.save(outputFile.toString());
}
}
画像をフローティングボックスの背景として使用
画像をスタイル付きレイアウトコンテナの背景として使用する場合は、この例を使用してください。
- ソース PDF の Document を開いて、対象ページにアクセスしてください。
- FloatingBox オブジェクトを、テキストと枠の設定で作成してください。
- 背景画像を設定し、ボックスをページに追加し、ドキュメントを保存してください。
public static void addImageAsBackgroundInFloatingBox(Path inputFile, Path imageFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
FloatingBox box = new FloatingBox(200.0f, 100.0f);
box.setLeft(40);
box.setTop(80);
box.setHorizontalAlignment(HorizontalAlignment.Center);
box.getParagraphs().add(new TextFragment("Text in Floating Box"));
box.setBorder(new BorderInfo(BorderSide.All, Color.getRed()));
Image image = new Image();
image.setFile(imageFile.toString());
box.setBackgroundImage(image);
box.setBackgroundColor(Color.getYellow());
page.getParagraphs().add(box);
document.save(outputFile.toString());
}
}