How to add an image and text to the same cell?

I am generating bar codes in a table. Now I want to insert the student code under the bar code label. How can I do this? My code currently looks like this: foreach (GridViewRow row in grdBarcode

I am generating bar codes in a table. Now I want to insert the student code under the bar code label. How can I do this?

My code currently looks like this:

foreach (GridViewRow row in grdBarcode.Rows)
{
    DataList dl = (DataList)row.FindControl("datalistBarcode");
    PdfContentByte cb = new PdfContentByte(writer);
    PdfPTable BarCodeTable = new PdfPTable(6);
    BarCodeTable.SetTotalWidth(new float[] { 100,10,100,10,100,10 });
    BarCodeTable.DefaultCell.Border = PdfPCell.NO_BORDER;
    Barcode128 code128 = new Barcode128();
    code128.CodeType = Barcode.CODE128_UCC;
    foreach (DataListItem dli in dl.Items)
    {
    String barcodename= ((Label)dli.FindControl("lblBarCode")).Text;
    string studentcode= ((Label)dli.FindControl("lblStudCode")).Text;
    code128.Code = "*" + productID1 + "*";
    iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
    BarCodeTable.AddCell(image128);
    BarCodeTable.AddCell("");
}
doc.Add(BarCodeTable);
My present Output looks like this:

Bar codes in a table

I want to add the Student code under the bar code label. Please show me a way to achieve this, or let me know how to pass more than one parameter through the Addcell() method.

Posted on StackOverflow on Jul 11, 2015 by Semil Sebastian

You are adding the Image object directly to a PdfPCell like this:

iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
BarCodeTable.AddCell(image128);

The second line is a short cut for something that looks like this:

PdfPCell cell = new PdfPCell();
cell.SetImage(image128);
BarCodeTable.AddCell(cell);

This cell contains nothing more than an image. There is no room for text.

If you want to combine an image and text, you need something like this:

PdfPCell cell = new PdfPCell();
cell.AddElement(image128);
Paragraph p = new Paragraph("Student name");
p.Alignment = Element.ALIGN_CENTER;
cell.AddElement(p);
BarCodeTable.AddCell(cell);

Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now