4

Ok, I'm having to make a PDF that has some styles to it. It's for labels. I have the following code:

Document doc = new Document(PageSize.A4);

    int labelHeight = 394;
    int labelWidth = 556;

    float labelTop = doc.PageSize.Height - 2;
    float labelBottom = doc.PageSize.Height - labelHeight;

    MemoryStream ms = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(doc, ms);
    doc.Open();

    PdfContentByte cb = writer.DirectContent;

    // Generate Boxes For Content

    cb.SetColorStroke(new CMYKColor(207, 115, 255, 133));
    cb.SetLineWidth(4);
    cb.SetColorFill(new CMYKColor(207, 71, 255, 38));

    cb.MoveTo(2, labelBottom);
    cb.LineTo(labelWidth - 2, labelBottom);
    cb.LineTo(labelWidth - 2, labelTop);
    cb.LineTo(2, labelTop);

    cb.ClosePathFillStroke(); // Draw Main Green Box

    cb.SetColorStroke(new CMYKColor(0, 0, 0, 0));
    cb.SetLineWidth(1);
    cb.SetColorFill(new CMYKColor(0, 0, 0, 0));

    int whiteBoxTop = 100;
    int whiteBoxHeight = 260;

    cb.MoveTo(5, labelTop - (whiteBoxTop + whiteBoxHeight));
    cb.LineTo(labelWidth - 5, labelTop - (whiteBoxTop + whiteBoxHeight));
    cb.LineTo(labelWidth - 5, labelTop - whiteBoxTop);
    cb.LineTo(5, labelTop - whiteBoxTop);

    cb.ClosePathFillStroke(); // Draw Inner White Content Box

    cb.SetColorStroke(new CMYKColor(20, 15, 41, 0));
    cb.SetLineWidth(0);
    cb.SetColorFill(new CMYKColor(20, 15, 41, 0));

    cb.MoveTo(labelWidth / 2, labelTop - ((whiteBoxTop + whiteBoxHeight) - 4));
    cb.LineTo(labelWidth - 9, labelTop - ((whiteBoxTop + whiteBoxHeight) - 4));
    cb.LineTo(labelWidth - 9, labelTop - (whiteBoxTop + 4));
    cb.LineTo(labelWidth / 2, labelTop - (whiteBoxTop + 4));

    cb.ClosePathFillStroke();

    string logoPath = Server.MapPath(".") + "/pdf_logo.jpg";

    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(logoPath);

    logo.SetAbsolutePosition(labelWidth / 4, labelTop - (whiteBoxTop + whiteBoxHeight) + 25);
    logo.ScaleToFit(150, 30);


    doc.Add(logo);    

    doc.Close();

The issue lies in the added logo. If I add it and I show it below the boxes I'm creating it shows up, but when I put the absolute position to where the boxes are it disappears behind the drawn boxes. What do I need to do to bring the image above the boxes I've drawn? This same issue is happening as well for any text I try to add on top of the boxes.

Nick Shepherd
  • 544
  • 6
  • 19

1 Answers1

6

Try adding the logo to your PdfContentByte object rather than your Document.

Replace this:

doc.Add(logo);

with this:

cb.AddImage(logo);
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • I love you...this is so obvious and I feel kind of retarded for not realizing this. – Nick Shepherd Nov 11 '10 at 20:03
  • Sometimes it just takes another pair of eyes! – Jay Riggs Nov 11 '10 at 20:11
  • Yep. Z-order within pdf is determined by the order in which things are drawn. But iText will use several different PdfContentBytes in various places then append them together again later. `DirectContent` happens to be above the content `add()`ed to the page. In PdfStamper there's explicit `getOverContent()` and `getUnderContent()`. – Mark Storer Nov 24 '10 at 18:03