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

How can I add two rows in a single pdf cell?

我正在生成条形码。现在我想在条形码标签下插入学生代码。我该怎么做?我的代码是

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

我现在的输出是

我想把学生代码也放在条形码标签下。请告诉我实现它的方法

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

试试这个

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

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

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

您正在将 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);