2015-07-10 27 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,任何想法:) –