程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

IText 图像设置绝对位置

发布于2022-05-19 07:43     阅读(1267)     评论(0)     点赞(30)     收藏(2)


我需要使用 iText 库将 PDF 中的图像放置在与 Adob​​e Acrobat 下面的屏幕截图所示完全相同的位置。我需要像这样将什么值传递给图像 setabsolute 位置:

        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 到 % 的转换。这是一个近似值,取决于字体、浏览器和操作系统,但它是一个很好的起点。

https://answers.yahoo.com/question/index?qid=20070723201114AAzsYjC

否则,我会为图像执行类似于 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
    }


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/442854/75014bc89ac50a242e45/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

30 0
收藏该文
已收藏

评论内容:(最多支持255个字符)