开发者

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. The image comes from a file upload. Once I have the file uploaded, how can specify where to place the image on the PDF. One code snippet I found does not work correctly. This needs to work for PDFs with any number of pages. From what I understand, absolute positioning is set from the bottom-left corner of the last page of the 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? Or, if I need to display an image 50px from the top/100 px from the left on page 2?

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/. 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);

    // clo开发者_开发技巧se 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.

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:

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:

// 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());. The parameter for getOverContent() is the page number. So your code is getting a PdfContentByte for the last page, not the first.


I found my answer:

The page number has to be set in com.lowagie.text.pdf.PdfStamper.getOverContent():

content = stamper.getOverContent(myPageNum);

Knew it was easy.


are you using CF8+? You can use

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜