如何生成 gridview 绑定条形码到 pdf?

How to generate gridview binded barcode to pdf?

我有一个网格,它绑定了数据库中的一些条形码。我想以 pdf 格式生成此条形码。我怎样才能做到这一点?抱歉,我不知道如何执行此操作,因此没有可显示的示例代码。我的网格视图名称 "GrdBarcode"。请帮助 me.Below 是我的带有条形码的网格

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

我试过像下面提到的那样 methode.but 它显示错误 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();
        }

根据数据库中的数据类型,有两种方法可以做到这一点。

如果您将其存储为字符串:

  • Create your Crystal Report
  • Drag the binded field onto the report
  • change the field font to the barcode font you have (usually shipped with wand reader)
  • Export the report to PDF

如果您将其存储为图像:

  • Create your crystal report
  • Grab the image data from database as Image object
  • put the Image object into a MemoryStream
  • convert the MemoryStream to Byte[]
  • add a column to your crystal report data to be of type Byte[]
  • store the value into this column
  • Drag the new data field onto the report and it will be an image
  • Export the report to PDF

当你需要网格时,最好使用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# 代码,请参阅:

  • Barcode with Text Under using ItextSharp
  • Add itextsharp barcode image to PdfPCell
  • generate barcode in pdf when using itextsharp to build the pdf
  • ...

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

这在以下问题中得到了回答:

  • How to add an image to a table cell in iTextSharp using webmatrix
  • Image auto resizes in PdfPCell with iTextSharp
  • itextsharp and images sizes
  • Putting several images next to each other in a pdfcell with itextsharp
  • iTextSharp: How to resize an image to fit a fix size?
  • Image resizes when inserted in PdfCell
  • ...