2015-07-10 77 views
3

我有网格绑定从数据库的一些条形码。这个条形码我想用pdf生成。我怎样才能做到这一点?对不起,我没有任何想法如何做到这一点,所以没有示例代码显示。我的gridview的名字 “GrdBarcode”。请帮助me.Below是我的网格,条形码如何生成gridview绑定条形码为pdf?

<asp:GridView ID="grdBarcode" CssClass="table" 
OnRowDataBound="grdBarcode_RowDataBound" AutoGenerateColumns="false" 
GridLines="None" runat="server"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<table style="width:100%"> 
<tr> 
<p>Date:<asp:Label runat="server" ID="Label5" Text='<%#Eval("Date") %>'>  
</asp:Label> </p></td> 
<td align="center"></td> <td><p>No Of QP: <asp:Label runat="server" 
ID="Label6" Text='<%#Eval("NoOfQP") %>'></asp:Label></p> 
<p>Time: <asp:Label runat="server" ID="Label7" Text='<%#Eval("Timing") %>'> 
</asp:Label></p> 
<p>Durations: <asp:Label runat="server" ID="Label8" 
Text='<%#Eval("Duration") %>'></asp:Label>)</p> 
</td> 
</tr> 
<tr> 
<td colspan="3" align="center"> 
<asp:DataList ID="datalistBarcode" RepeatColumns="3" 
RepeatDirection="Horizontal" GridLines="None" 
OnItemDataBound="datalistBarcode_ItemDataBound" runat="server"> 
<ItemTemplate> <asp:Label ID="lblBarCode" runat="server" 
Text='<%#Eval("BarCode") %>' Visible="false"></asp:Label> 
<table class="table"> 
<tr> <td > 
<asp:Panel ID="pnlBarCode" HorizontalAlign="center" runat="server"> 
</asp:Panel> 
</tr> 
<tr> 
<td align="center"><asp:Label ID="lblStudCode" runat="server" 
Text='<%#Eval("StudCode") %>'></asp:Label> 
</td> 
</tr> 
</table> 
</ItemTemplate> 
</asp:DataList> 
</td> 
</tr> 
</table> 
</ItemTemplate></asp:TemplateField> 
</Columns> 
</asp:GridView>  

enter image description here

我试着像methode.but它提到elow显示错误Document has no pages

protected void btnPrint_Click(object sender, EventArgs e) 
    { 
     foreach (GridViewRow row in grdBarcode.Rows) 
     { 
      DataList dl = (DataList)row.FindControl("datalistBarcode"); 
      string attachment = "attachment; filename=Article.pdf"; 
      Response.ClearContent(); 
      Response.AddHeader("content-disposition", attachment); 
      Response.ContentType = "application/pdf"; 
      StringWriter stw = new StringWriter(); 
      HtmlTextWriter htextw = new HtmlTextWriter(stw); 
      dl .DataBind(); 
      dl .RenderControl(htextw); 
      Document document = new Document(); 
      PdfWriter.GetInstance(document, Response.OutputStream); 
      document.Open(); 
      StringReader str = new StringReader(stw.ToString()); 
      HTMLWorker htmlworker = new HTMLWorker(document); 
      htmlworker.Parse(str); 
      document.Close(); 
      Response.Write(document); 
      Response.End(); 
     } 
+0

条形码字符串或图像?你用什么PDF库来生成PDF文件? – Graffito

回答

1

报告当你需要一个网格,它最好使用PdfPTable

如果您还没有条形码,可以使用iText或iTextSharp创建它们。看看在Barcodes例子灵感:

public void createPdf(String dest) throws IOException, DocumentException { 
    Document document = new Document(); 
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(4); 
    table.setWidthPercentage(100); 
    for (int i = 0; i < 12; i++) { 
     table.addCell(createBarcode(writer, String.format("%08d", i))); 
    } 
    document.add(table); 
    document.close(); 
} 

public static PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException { 
    BarcodeEAN barcode = new BarcodeEAN(); 
    barcode.setCodeType(Barcode.EAN8); 
    barcode.setCode(code); 
    PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true); 
    cell.setPadding(10); 
    return cell; 
} 

对于一些C#代码创建条形码,请参见:

如果你已经有条形码的图像,你仍然可以使用表格,但问题应该是如何组织图像在表格中?

这是问题的答案,如:

+0

哇!伟大的代码亲爱的..我试试一次.. :) –

+0

我有一个怀疑,我已经获得条码在datalist。我可以迭代每个条形码并使用foreach循环将该值添加到pdfcell中吗? –

+0

在datalist *中定义*条形码。你在谈论可以在循环中使用的图像列表吗?在这种情况下:是的,遍历该列表并为每个图像创建一个“PdfPCell”,并将这些单元添加到“PdfPTable”中。 –

-1

有有两种方法可以根据数据库中的数据类型来执行此操作。

如果您有它保存为一个字符串:

  • 创建水晶报表
  • 将绑定的字段拖到报告
  • 变化领域的字体,你有条形码字体(通常附带读码器)
  • 将报告导出至PDF

如果您有它保存为图像:

  • 创建Crystal报表
  • 抓住从数据库Image对象
  • Image对象为MemoryStream
  • 转换的图像数据MemoryStreamByte[]
  • 在您的水晶报告数据中添加一列为类型
  • 店值此列
  • 将新的数据字段拖到报告,这将是一个像
  • 导出为PDF
+0

这是一个Crystal报告对标记为iTextSharp问题的问题的回答。 –

+0

是的,我textsharp可以合并其他pdf页 – Franck

+0

@ Bruno Lowagie,任何想法:) –