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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:09:05+00:00 2026-05-27T21:09:05+00:00

I’m using iTextSharpText to build a PDF. I was asked to add some dynamic

  • 0

I’m using iTextSharpText to build a PDF.

I was asked to add some dynamic text over an image.
I’ve already tried some examples that I found on this forum and other websites, but I can’t seem to get this right, and it’s really driving me nuts.

I followed this thread (iTextSharp Adding Text plus Barcode in a single cell?), and tried both answers.

First I tried:

            var img_operacao = Image.GetInstance(String.Format("{0}{1}", Settings.Default.SiteRootURL, "/PublishingImages/PDF_operacao.png"));
            img_operacao.ScaleToFit(150, 41);

            img_operacao.Alignment = Image.UNDERLYING;

            var ph0 = new Phrase(title0, pageHeader);
            var cell = new PdfPCell();

            cell.AddElement(ph0);
            cell.AddElement(new Phrase(new Chunk(img_operacao, 0, 0)));
            table.AddCell(cell);

And I got this:
http://imageshack.us/photo/my-images/515/25265113.png/

My second attempt:

            var img_operacao = Image.GetInstance(String.Format("{0}{1}", Settings.Default.SiteRootURL, "/PublishingImages/PDF_operacao.png"));
            img_operacao.ScaleToFit(150, 41);

            img_operacao.Alignment = Image.UNDERLYING;

            var cell = new PdfPCell
                       {
                           Border = Rectangle.NO_BORDER,
                           PaddingTop = 0,
                           PaddingBottom = 0,
                           HorizontalAlignment = Element.ALIGN_CENTER,
                           VerticalAlignment = Element.ALIGN_MIDDLE
                       };

            var ph0 = new Phrase();
            ph0.Add(new Chunk(title0, pageHeader));
            ph0.Add(new Chunk(img_operacao, 0, 0, true));

            cell.AddElement(ph0);
            table.AddCell(cell);

And I got:
http://imageshack.us/photo/my-images/688/89424619.png/

I’ve tried several other variations with chunks, paragraphs, etc, but I can’t get the text over the image!!!

If there is someone that can help me, I would really appreciate it.
I’m stuck right now…

Thanks a lot

Paula

  • 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-27T21:09:06+00:00Added an answer on May 27, 2026 at 9:09 pm

    It looks like you want to add a background image to a PdfPCell. The recommended approach is to implement the IPdfPCellEvent interface. As documented, you only need to write one method, CellLayout, when you write the custom class that implements IPdfPCellEvent. From your code it looks like you’re in a web environment, so this simple example HTTP handler (.ashx) should be easy to follow:

    <%@ WebHandler Language="C#" Class="cellEvent" %>
    using System;
    using System.Web;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    public class cellEvent : IHttpHandler {
      public void ProcessRequest (HttpContext context) {
        HttpServerUtility Server = context.Server;
        HttpResponse Response = context.Response;
        Response.ContentType = "application/pdf";
        // replace with your image
        string imagePath = Server.MapPath("./Image1.png");
        Image image = Image.GetInstance(imagePath);
        using (Document document = new Document()) {
          PdfWriter.GetInstance(document, Response.OutputStream);      
          document.Open();
          PdfPTable table = new PdfPTable(2);
          for (int i = 1; i < 10; ++i) {
            PdfPCell cell = new PdfPCell(new PdfPCell());
            // add the text
            cell.Phrase = new Phrase(string.Format("CELL 1, ROW {0}", i));
            // your class that adds the background image
            cell.CellEvent = new TestCellEvent() { 
              CellImage = image
            };
            table.AddCell(cell);
            // add __new__ cell; CellEvent no longer applies;
            // (no background image)
            table.AddCell(new PdfPCell(new Phrase(
              string.Format("CELL 2, ROW {0}", i
            ))));
          }
          document.Add(table);
        }
      }
      public bool IsReusable { get { return false; } }
    
    // custom class to implement a cell background image
      class TestCellEvent : IPdfPCellEvent {
        public Image CellImage;
        public void CellLayout(
          PdfPCell cell, Rectangle position, PdfContentByte[] canvases
        ) 
        {
          PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
          // scale image to the cell's __full__ height
          CellImage.ScaleAbsoluteHeight(cell.Height);
          // scale image to the cell's __full__ width
          CellImage.ScaleAbsoluteWidth(cell.Width);
          // you must also explcitly set the image position!
          CellImage.SetAbsolutePosition(position.Left, position.Bottom);
          // add the image to the cell
          cb.AddImage(CellImage);
        }
      }
    }
    

    Hopefully the inline comments make sense. Notice that the Image object was instantiated once. If you’re reusing the same image doing it this way greatly reduces the file size, versus repeatedly instantiating the Image for each cell, since the image data is only added once to the PDF.

    Good luck.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.