Java IText图像集绝对位置

Java IText图像集绝对位置,java,android,itext,Java,Android,Itext,我需要使用iText库在PDF中定位图像,位置与下面Adobe Acrobat截图中显示的位置完全相同。我需要向图像集绝对位置传递什么值,如下所示: Image image = Image.getInstance(stream.toByteArray()); image.scaleAbsolute(150f, 150f); image.setAbsolutePosition(???f, ???f); PdfContentByte

我需要使用iText库在PDF中定位图像,位置与下面Adobe Acrobat截图中显示的位置完全相同。我需要向图像集绝对位置传递什么值,如下所示:

        Image image = Image.getInstance(stream.toByteArray());
        image.scaleAbsolute(150f, 150f);
        image.setAbsolutePosition(???f, ???f);

        PdfContentByte overContent = stamper.getOverContent(1);
        overContent.addImage(image);
        stamper.setFormFlattening(true);
        stamper.close();
我正在尝试所有的值,图像在输出文件中向四周跳跃。我应该使用什么值?我还包括了getFieldPosition()输出的屏幕截图


如果您的问题是无法计算pt到x,y位置的转换,这可能会有帮助:

要从像素到Em,只需除以16即可

使用Reed Design的表格,该表格列出从点到像素的转换 至Em至%。这是一个近似值,取决于字体和浏览器 和操作系统,但这是一个很好的起点

否则,我会对图像执行类似于yo的操作:

    /**
     * Adds an image to the PDF
     * @param imageData The data for the image to add
     * @param page The page number of where to add the photo
     * @param xPos The x coordinate of where to put the image. Note that 0 is the left of the page
     * @param yPos The y coordinate of where to put the image. Note that 0 is the top of the page. This is
     * different than the addText function for some reason.
     * @param layer
     * @throws Exception If there was an error adding the image to the PDF
     */
    public void addImageToPDF(float xPos, float yPos, byte[] imageData, int page,  Layer layer) throws Exception
    {
        PdfContentByte content = getPdfContentByte(page, layer);
        Image img = Image.getInstance(imageData);
        img.setAbsolutePosition(xPos, yPos);
        content.addImage(img);
    }


    /**
     * Gets a PdfContentByte. This is used to make changes to the PDF
     * @param page The page number that is to be modified
     * @param layer The layer that is to be modified
     * @return A PdfContentByte that can be used to modify the PDF
     */
    private PdfContentByte getPdfContentByte(int page, Layer layer)
    {
        return layer == Layer.OVER ? stamper.getOverContent(page) : stamper.getUnderContent(page);
    }
其中Layer是一个简单的枚举,用于确定PDF的哪一层:

    /**
     * Represents a layer of the PDF relative to the original file. For example,
     * if an operation requires a Layer argument, OVER will make the changes ontop
     * of the original PDF. UNDER will make the changes under the original PDF. So
     * if there is text in the original PDF and you choose UNDER, the changes you make
     * may be covered up by the original text. If you choose OVER, you changes will be
     * ontop of whatever was in the original PDF
     */
    public enum Layer{
        /**
         * Represents ontop of the original PDF
         */
        OVER,

        /**
         * Represents under the original PDF
         */
        UNDER
    }

谢谢你的回复。对于这个文档,我没有文档的实例,而是PdfStamper的实例。我得到一个表单中所有字段(AcroFields)的引用。其中一个字段是一个占位符文本框,我想做的是在该文本框的确切位置添加一个图像,然后删除该文本框。我在这方面取得了很大的进步,我将继续努力。谢谢你还想知道这个问题的答案吗?