2015-07-11 64 views
5

我是generatng条形码。现在我想在条形码标签下插入学生代码。我该怎么做?我的代码是如何在单个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); 

我现在的输出是 enter image description here

我希望把学生代码也条形码标签下。请告诉我一种方法来实现它

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

回答

2

您直接添加Image对象a 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); 
+0

再次保存me..Thaaankz一个looooot亲爱的:) –

0

试试这个

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

相关问题