Aspose.java.pdf 如何在A4右上角位置添加图片签章

public static void addPic() {
String myDir = “F:\demo\”;
Document doc = new Document(myDir + “test.pdf”);

    ImageStamp stamp = new ImageStamp(myDir+"logo.png");
    //我希望在A4纸尺寸的右上角添加图片,如何计算尺寸,默认是在页面的左下角添加。
    //I want to add a picture in the upper right corner of the A4 paper size, how to calculate the size, 
    // the default is to add it in the lower left corner of the page.
    stamp.setXIndent(0);
    stamp.setYIndent(10);
    stamp.setBackground(false);
    stamp.setHeight(100);
    stamp.setWidth(100);
    stamp.setOpacity(0.5);
    for(Page page:doc.getPages()){
        page.addStamp(stamp);
    }

    doc.save(myDir+"watermark.pdf");
    System.out.println("Done");
}

@LiuHUAQIANG888

要在A4纸张尺寸的右上角添加图像印记,您需要根据页面尺寸和图像印记尺寸计算图像印记原点的x和y坐标。例如,如果页面尺寸为 595 x 842 点(A4 纸张尺寸,72 dpi),图像印章尺寸为 100 x 50 点,则可以按如下方式设置图像印章原点的 x 和 y 坐标:

// create image stamp
ImageStamp imageStamp = new ImageStamp("sample.png");

// set origin
imageStamp.setXIndent(595 - 100 - 10); // subtract image width and some margin from page width
imageStamp.setYIndent(842 - 50 - 10); // subtract image height and some margin from page height

您还可以使用 ImageStamp 类的 setHorizo​​ntalAlignment 和 setVerticalAlignment 方法来相对于页边距对齐图像图章。例如,您可以使用以下代码将图像图章与右上角对齐:

// create image stamp
ImageStamp imageStamp = new ImageStamp("sample.png");

// set alignment
imageStamp.setHorizontalAlignment(HorizontalAlignment.Right);
imageStamp.setVerticalAlignment(VerticalAlignment.Top);

// set margin
imageStamp.setRightMargin(10); // set some margin from right edge of page
imageStamp.setTopMargin(10); // set some margin from top edge of page