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

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

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); 

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

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

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

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

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

第二行是一个看起来像这样的捷径:

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

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); 

试试这个

  var p = new Paragraph(); p.Add("First line text\n"); p.Add(" Second line text\n"); p.Add(" Third line text\n"); p.Add("Fourth line text\n"); myTable.AddCell(p); 

如果您需要更多控制,您也可能会变得复杂并使用子表:

 var subTable = new PdfPTable(new float[] { 10, 100 }); subTable.AddCell(new PdfPCell(new Phrase("First line text")) { Colspan = 2, Border = 0 }); subTable.AddCell(new PdfPCell() { Border = 0 }); subTable.AddCell(new PdfPCell(new Phrase("Second line text")) { Border = 0 }); subTable.AddCell(new PdfPCell() { Border = 0 }); subTable.AddCell(new PdfPCell(new Phrase("Third line text")) { Border = 0 }); subTable.AddCell(new PdfPCell(new Phrase("Fourth line text")) { Colspan = 2, Border = 0 }); myTable.AddCell(subTable); 

http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables