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 3456022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:41:09+00:00 2026-05-18T09:41:09+00:00

We have an ASP.NET application that users use to generate certain reports. So far

  • 0

We have an ASP.NET application that users use to generate certain reports. So far we had one PDF template that had one image on it, and we would just replace that image with our programatically generated one (graph).
We have used code from this site for that:http://blog.rubypdf.com/2007/12/12/how-to-replace-images-in-a-pdf/

Problem now is that we have two different images on one PDF page, and the code from link above selects both images on one page and replaces them all at once with our generated image.

Does anyone have any idea how to replace multiple different images on one page with itext?

Thanks

  • 1 1 Answer
  • 2 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-18T09:41:09+00:00Added an answer on May 18, 2026 at 9:41 am

    Ugh. First, let me rewrite some of that source.

    PdfReader pdf = new PdfReader("in.pdf");
    PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
    PdfWriter writer = stp.getWriter();
    Image img = Image.getInstance("image.png");
    PdfDictionary pg = pdf.getPageN(1);
    PdfDictionary res = pg.getAsDict.get(PdfName.RESOURCES);
    PdfDictionary xobj = res.getAsDict(PdfName.XOBJECT);
    if (xobj != null) {
      for (Iterator<PdfName> it = xobj.getKeys().iterator(); it.hasNext(); ) {
        PdfObject obj = xobj.get(it.next());
        if (obj.isIndirect()) {
          PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
          PdfName type = tg.getAsName(PdfName.SUBTYPE));
          if (PdfName.IMAGE.equals(type)) {
            PdfReader.killIndirect(obj);
            Image maskImage = img.getImageMask();
            if (maskImage != null)
              writer.addDirectImageSimple(maskImage);
            writer.addDirectImageSimple(img, (PRIndirectReference)obj);
            break;
          }
        }
      }
    }
    

    Whew. the getAs functions can save you quite a bit of knuckle-grease and make your code much clearer.

    Now. You need to be able to differentiate between the various images. If you’re willing to hard-code things, you could find out what the resource names are and go that route:

    String imageResName[] = {"Img1", "Img2" ... };
    Image img[] = {Image.getInstance("foo.png"), Image.getInstance("bar.png"), ... };
    for (int i = 0; i < imageResName.length; ++i) {
      PdfName curKey = new PdfName(imageResName[i]);
      PdfIndirectReference ref = xobj.getAsIndirect(curKey);
      PdfReader.killIndirect( ref );
      Image maskImage = img[i].getImageMask();
      if (maskImage != null) {
        writer.addDirectImageSimple(maskImage);
      }
      writer.addDirectImageSimple(img[i], (PRIndirectReference)ref);
    }
    

    If you’re not willing to go with hardcoded resource names (and no one would fault you, quite the opposite, particularly when the order they appear (and thus the number on the end) depends on their order in a hash map… [shudder]), you may be able to differentiate based on image width and height.

    //keep the original for loop, stepping through resource names
    if (PdfName.IMAGE.equals(type)) {
      float width = tg.getAsNumber(PdfName.WIDTH).floatValue();
      float height = tg.getAsNumber(PdfName.HEIGHT).floatValue();
    
      Image img = getImageFromDimensions(width, height);
    
      Image maskImage = img.getImageMask();
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET 3.0 application that works fine on one server. It uses
I have a file upload function in my asp.net mvc application that allows users
Summary We have an ASP.NET application that allows users to query a SQL Server
We have an asp.net application that needs to be expanded to notify users when
I have an ASP.NET application that uses a custom .NET library (a .DLL file).
I have a asp.net web application that uses C#. It logs in on a
I have an ASP.NET MVC application that uses Fluent NHibernate and AutoMapper. I am
I have an ASP.NET MVC application that uses Linq to SQL repositories for all
I have an ASP.NET (v2.0) web application that uses a reference to a SQL
I have an application (ASP.NET MVC) that uses a Next New method to get

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.