请教,如何在pdf中某一个固定位置插入一个base64image的图片,固定位置需要判断横向纵向设置固定位置

public static void main(String[] args) {
    try {
        String inFilePath = "E:\\test\\pdf_signature\\a.pdf";
        String outFilePath = "E:\\test\\pdf_signature\\z" + Instant.now().toEpochMilli() + ".pdf";
        String base64Image ="iVBORw0KGgoAAAANSUhEUgAAAUAAAACgCAYA";

        // load
        Document pdfDocument = new Document(inFilePath);

        Page page = pdfDocument.getPages().get_Item(1);
        boolean isLandscape = page.getPageInfo().getWidth() > page.getPageInfo().getHeight();
        log.info("isLandscape:{}", isLandscape);

        Image image = new Image();
        image.setImageStream(new ByteArrayInputStream(Base64.getDecoder().decode(base64Image)));
        image.setFixWidth(100);
        image.setFixHeight(50);
        page.getParagraphs().add(image);
        pdfDocument.save(outFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这个代码,目前能插入图片,但是是竖的,要根据横向/纵向设置插入的样式,位置,应该如何写?请教。

@renchenyang

Cause

The issue you’re facing is due to the default orientation of the image being inserted. When you insert an image into a PDF, its orientation (portrait or landscape) can affect how it appears on the page. You need to adjust the image’s position and possibly its rotation based on the page’s orientation.

Solution

To insert the image at a fixed position based on the page’s orientation, you can set the image’s position using the setPosition method. Additionally, you can rotate the image if necessary. Here’s how you can modify your code:

Code

public static void main(String[] args) {
    try {
        String inFilePath = "E:\\test\\pdf_signature\\a.pdf";
        String outFilePath = "E:\\test\\pdf_signature\\z" + Instant.now().toEpochMilli() + ".pdf";
        String base64Image = "iVBORw0KGgoAAAANSUhEUgAAAUAAAACgCAYA";

        // Load the PDF document
        Document pdfDocument = new Document(inFilePath);
        Page page = pdfDocument.getPages().get_Item(1);
        boolean isLandscape = page.getPageInfo().getWidth() > page.getPageInfo().getHeight();
        log.info("isLandscape: {}", isLandscape);

        // Create an image object
        Image image = new Image();
        image.setImageStream(new ByteArrayInputStream(Base64.getDecoder().decode(base64Image)));
        image.setFixWidth(100);
        image.setFixHeight(50);

        // Set the position based on orientation
        if (isLandscape) {
            image.setPosition(400, 100); // Adjust X and Y for landscape
        } else {
            image.setPosition(100, 400); // Adjust X and Y for portrait
        }

        // Optionally rotate the image if needed
        // image.setRotation(90); // Uncomment to rotate

        // Add the image to the page
        page.getParagraphs().add(image);
        pdfDocument.save(outFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Next Steps

  1. Adjust the setPosition values based on where you want the image to appear on the page.
  2. If you need to rotate the image, uncomment the setRotation line and set the desired angle.
  3. Test the code to ensure the image appears correctly in both landscape and portrait orientations.

If you have any further questions or need additional assistance, please let me know!

Cannot resolve method ‘setPosition’ in ‘Image’

com.aspose
aspose-pdf
25.6

这个版本不存在setPosition坐标方法

@renchenyang

我们建议您使用 ImageStamp 类,而不是 Image 类,因为 ImageStamp 类提供了设置水平/垂直对齐方式以及 X/Y 坐标的属性。请注意,您可以选择使用 X/Y 坐标来设置图像位置,或者使用水平/垂直对齐方式。以下示例代码片段仅供参考,您需要根据自己的需求进行修改。

// 它也可以使用流进行初始化。您可以使用 base64 字符串或字节准备流并提供给它。
ImageStamp aa1 = new ImageStamp(dataDir + "lala.jpg");
aa1.setBackground(false);
aa1.setXIndent(80);
aa1.setYIndent(100);
aa1.setWidth(18);
aa1.setHeight(18);
aa1.setHorizontalAlignment(HorizontalAlignment.Center);
aa1.setVerticalAlignment(VerticalAlignment.Bottom);