Skip to main content
Skip table of contents

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.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

In iText 7 we use Cell instead of PdfPCell and Table instead of PdfPTable. I’ll write my code in Java, but if you need an iText for C# example, you'll discover that it is very easy to port the Java to C# as the terminology is identical.

You are adding the Image object directly to a cell using AddCell() method. This is not what you want. Such cell contains nothing more than an image. There is no room for text.

If you want to combine an image and text, you need to create a Cell instance and add any sort of data to it. And when you are done, use addCell() method to add it to the table. In iText 7 it looks like this:

   public void createPdf(String dest) throws IOException {

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc);

    String code = "675-FH-A12";
    Barcode128 code128 = new Barcode128(pdfDoc);
    code128.setFont(null);
    code128.setCode(code);
    code128.setCodeType(Barcode128.CODE128);

    Table table = new Table(2);
    table.addCell("Add text under the barcode:");

    Image code128Image = new Image(code128.createFormXObject(pdfDoc)).setAutoScale(true);
    Paragraph paragraph = new Paragraph("Student code:" + code).setTextAlignment(TextAlignment.CENTER);

    Cell cell = new Cell();
    cell.add(code128Image);
    cell.add(paragraph);
    table.addCell(cell);

    doc.add(table);
    doc.close();

}

Click How to add an image and text to the same cell? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.