2

I'm using iTextSharp to create a PDF using an HTML page as an example. I am trying to put a border around text to highlight certain items. Using methods from these posts (Draw Rect at curr position, and Add Text over image ) I have used OnGenericTag events to create the rectangle for each Chunk that matches certain criteria. The event fires correctly and the rectangles are drawn on the correct elements. However the problem occurs when I add the background for the tablecell, it appears on top of the Chunk rectangles. Is there a way to make the rectangle drawn from the OnGenericTag event drawn above the table cell background?


OnGenericTags Method:

public class GenericTags : PdfPageEventHelper
    {
        public override void OnGenericTag(PdfWriter writer, Document pdfDocument, iTextSharp.text.Rectangle rect, String text)
        {
            if ("ellipse".Equals(text)) //I know it's not needed. 
                ellipse(writer.DirectContent, rect);
        }

        public void ellipse(PdfContentByte content, iTextSharp.text.Rectangle rect)
        {
            content.SaveState();
            content.SetRGBColorStroke(0x00, 0x00, 0xFF);
            content.SetLineWidth(2);
            content.Rectangle(rect.Left - 2f, rect.Bottom - 3f, rect.Width, rect.Height + 3f);
            content.Stroke();
            content.RestoreState();
        }
    }

Each PdfPCell is defined as:

PdfPCell med = new PdfPCell(ph[4])
   {
     HorizontalAlignment = Element.ALIGN_CENTER,
     VerticalAlignment = Element.ALIGN_CENTER,
     BackgroundColor = hm_yellow //this draws above rect from onGenericTag()
   };

The Chunks are created and assign via a foreach loop:

ph[j] = new Phrase();
foreach (var x in p.Risks)
   {
      c[i] = new Chunk("R" + x.APP_RISK_ID.ToString() + ", ");
      if (x.RISK_STATUS_CD == "57")
      {
        //c[i].SetBackground(hm_top_tier); // still shows behind table cell background
        c[i].SetGenericTag("ellipse");
      }
    ph[j].Add(c[i]);
   }

This is the HTML page I'm trying to turn into a PDF. It shows the table cell background colors and the values that need to have a rectangle around them to highlight them. Heat map from HTML

This is the result when rendering a PDF. The highlighted R#'s match, however, when I apply a background color to the PdfPCell it is drawn over the rectangle from the Chunk heat map from pdf

Community
  • 1
  • 1
Auzi
  • 337
  • 3
  • 13

1 Answers1

1

Draw the table using the layer writer.DirectContentUnder to make sure that the rectangle stays on top (writer.DirectContent).

Paulo Soares
  • 1,896
  • 8
  • 21
  • 19