Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:57:07+00:00 2026-05-23T10:57:07+00:00

I have existing PDFs to which I need to dynamically add an image/images. The

  • 0

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

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

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:57:08+00:00Added an answer on May 23, 2026 at 10:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing image upload script (below) which works fine but I would
I have existing Linux shared object file (shared library) which has been stripped. I
I have existing java code and need to create Design Document based on that.
I have existing code with their own makefiles which I want to load into
I'm working on a project where the client wants to have existing pages dynamically
We have existing VS 2008 VSTO add-ins for Word 2003 and Excel 2003. We
I have an existing app in which I made the mistake of using String.GetHashCode
I am new to MongoDb but have an existing application which would benefit heavily
i have existing code that generates a spreadsheet from excel using OpenXML. It works
I've been using Prawn to generate simple invoice PDFs. I now have the need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.