如何在单个pdf单元格中添加两行?

Sem*_*ian 5 c# itextsharp

我是条码.现在我想在条形码标签下插入学生代码.我怎么能这样做?我的代码是

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);
Run Code Online (Sandbox Code Playgroud)

我现在的输出是 在此输入图像描述

我想将学生代码也带到条形码标签下.请告诉我一个实现它的方法

或者让我知道如何传递多个参数throgh pdftable.Addcell()函数.. !!

Bru*_*gie 2

您将Image对象直接添加到PdfPCell如下所示:

iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
BarCodeTable.AddCell(image128);
Run Code Online (Sandbox Code Playgroud)

第二行是类似这样的内容的快捷方式:

PdfPCell cell = new PdfPCell();
cell.SetImage(image128);
BarCodeTable.AddCEll(cell);
Run Code Online (Sandbox Code Playgroud)

cell仅包含图像。没有文字空间。

如果你想组合图像和文本,你需要这样的东西:

PdfPCell cell = new PdfPCell();
cell.AddElement(image128);
Paragraph p = new Paragraph("Student name");
p.Alignment = Element.ALIGN_CENTER;
cell.AddElement(p);
BarCodeTable.AddCEll(cell);
Run Code Online (Sandbox Code Playgroud)