简体   繁体   English

如何使用 IText 在特定 xy 坐标处将图像添加到 PDF?

[英]How can I add an image to a PDF at specific x-y coordinates using IText?

I have existing PDFs to which I need to dynamically add an image/images.我有现有的 PDF,需要动态添加图像/图像。 The image comes from a file upload.图片来自文件上传。 Once I have the file uploaded, how can specify where to place the image on the PDF.上传文件后,如何指定在 PDF 上放置图像的位置。 One code snippet I found does not work correctly.我发现的一个代码片段无法正常工作。 This needs to work for PDFs with any number of pages.这需要适用于具有任意页数的 PDF。 From what I understand, absolute positioning is set from the bottom-left corner of the last page of the PDF.据我了解,绝对定位是从PDF最后一页的左下角设置的。 If I need an image to be displayed 30 pixels from the top and 50 pixels from the left of page 1, how can I accomplish this?如果我需要在第 1 页顶部和左侧 50 像素处显示图像,我该如何实现? Or, if I need to display an image 50px from the top/100 px from the left on page 2?或者,如果我需要在第 2 页上显示距顶部 50 像素/距左侧 100 像素的图像?

I've tried using the code found at http://rip747.wordpress.com/2009/03/26/add-an-image-dynamically-to-a-pdf-with-cf-and-itext/ .我尝试使用在http://rip747.wordpress.com/2009/03/26/add-an-image-dynamically-to-a-pdf-with-cf-and-itext/找到的代码。 I've modified it for my needs below:我已根据以下需要对其进行了修改:

<cfscript>
    myLeft = 30;
    myTop = 50;
    myPageNum = 1;

    // output buffer to write PDF
    fileIO = createObject("java","java.io.FileOutputStream").init(myOutputPath);

    // reader to read our PDF
    reader = createObject("java","com.lowagie.text.pdf.PdfReader").init(mySourcePath);

    // stamper so we can modify our existing PDF
    stamper = createObject("java","com.lowagie.text.pdf.PdfStamper").init(reader, fileIO);

    // get the content of our existing PDF
    content = stamper.getOverContent(reader.getNumberOfPages());

    // create an image object so we can add our dynamic image to our PDF
    image = createobject("java", "com.lowagie.text.Image");

    // initalize our image
    img = image.getInstance(imgPath);

    x = (reader.getPageSize(1).width() - img.scaledWidth()) - myLeft;
    y = (reader.getPageSize(1).height() - img.scaledHeight()) - myTop;

    // now we assign the position to our image
    img.setAbsolutePosition(javacast("float", x), javacast("float", y));

    // add our image to the existing PDF
    content.addImage(img);

    // flattern our form so our values show
    stamper.setFormFlattening(true);

    // close the stamper and output our new PDF
    stamper.close();

    // close the reader
    reader.close();
</cfscript>

The above code places my image at the top-right corner of page 2 - 50px form the top/30px from the left.上面的代码将我的图像放在第 2 页的右上角 - 距左上角 50px/30px。

I know I'm close...just need a little help getting this nailed down for my needs.我知道我很接近......只需要一点帮助来确定我的需要。

I've updated my code.我已经更新了我的代码。 This gets the image to the top left corner of page 2 - correct positioning, but I want it on page 1:这会将图像带到第 2 页的左上角 - 正确定位,但我希望它在第 1 页:

x = myLeft;
y = (reader.getPageSize(1).height()) - img.scaledHeight() - myTop;

I thought maybe I needed to add the height of page 1 to get the image up to page 1, but the image completely disappears when I try either of the options below:我想也许我需要添加第 1 页的高度才能使图像达到第 1 页,但是当我尝试以下任一选项时,图像会完全消失:

// I figure I'll need something like this to handle multi-page docs
y = (reader.getPageSize(1).height() * reader.getNumberOfPages()) - img.scaledHeight() - myTop;

y = reader.getPageSize(1).height() + reader.getPageSize(1).height() - img.scaledHeight() - myTop;

You're getting your "OverContent" from stamper.getOverContent(reader.getNumberOfPages());你从stamper.getOverContent(reader.getNumberOfPages());得到你的“OverContent”。 . . The parameter for getOverContent() is the page number. getOverContent()的参数是页码。 So your code is getting a PdfContentByte for the last page, not the first.因此,您的代码正在获取最后一页的PdfContentByte ,而不是第一页。

I found my answer:我找到了答案:

The page number has to be set in com.lowagie.text.pdf.PdfStamper.getOverContent() :页码必须在com.lowagie.text.pdf.PdfStamper.getOverContent()中设置:

content = stamper.getOverContent(myPageNum);

Knew it was easy.知道这很容易。

are you using CF8+?你用的是CF8+吗? You can use您可以使用

<cfpdf action="addWatermark" source="myPDF.pdf" image="myImage.jpg" 
       position="0,0" rotation="0" showOnPrint="true" opacity="10">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何围绕xy坐标动态裁剪图像? - how to dynamically crop an image around x-y coordinates? 如何将图像添加到现有的特定 pdf 页面 (Itext) 中? - How can i add an image into an existing and specific pdf page (Itext)? 将鼠标悬停在图像上时,如何显示带有xy坐标的十字光标? - How could I display a crosshair cursor with x-y coordinates when hovering over an image? 如何使用itext将图像导入PDF - How can I import an image to my PDF using itext 使用 iText 将图像添加到 PDF - Add Image to a PDF using iText xy坐标和html5中sprite图片的宽度和高度 - x-y coordinates & width-height of an image from sprite in html5 如何将道路上某个点在其图像坐标中的 (x,y) 坐标转换为其在世界坐标中的 (x,y) 坐标? - How do I convert the (x,y) coordinates of a point on the road in its image coordinates, to its (x,y) coordinates in the world coordinates? 如何从地理标记图像的 xy 坐标计算纬度和经度 - How to calculate lattitude and longitude from x-y coordinates for geotagged images Python - 在xy坐标上绘制多个图像 - Python - plotting multiple images on x-y coordinates 如何使用给定的X,Y,Width和Height坐标在php中裁剪图像 - How can I crop the image in php with given coordinates of X,Y,Width and Height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM