3

I have grid which binds some barcodes from databas. This barcodes I want to generate in pdf. How can I do this? Sorry I dont have any idea how to do this so there is no sample code to show. My gridview name "GrdBarcode".Please help me.Below is my grid with barcodes

  <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

I tried like elow mentioned methode.but it shows error 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();
        }
Semil Sebastian
  • 519
  • 2
  • 9
  • 24

2 Answers2

1

When you need a grid, it is best to use a PdfPTable.

If you don't have the barcodes yet, you can create them with iText or iTextSharp. Take a look at the Barcodes example for inspiration:

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

For some C# code on creating barcodes, see:

If you already have the images with the barcodes, you can still use a table, but then the question should be How do I organize images inside a table?

That is answered in questions such as:

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I have a doubt,I have getting barcode in datalist. Can I iterates each barcodes and add the value into pdfcell using foreach loop ? – Semil Sebastian Jul 10 '15 at 12:56
  • Define *barcode in datalist*. Are you talking about a list of images that you can use in a loop? In that case: yes, loop over that list and create a `PdfPCell` for every image and add those cells to a `PdfPTable`. – Bruno Lowagie Jul 10 '15 at 13:04
  • I cannot call the datalist control in my foreach loop. See my updated answer – Semil Sebastian Jul 10 '15 at 13:14
  • @SemilSebastian I don't understand the question. The reason I don't understand the question, is that I have no clue as to which format is used for ``. – Bruno Lowagie Jul 10 '15 at 13:20
  • Hmm... You're using `HTMLWorker`. As explained in many answers to many questions, `HTMLWorker` has been abandoned a long time ago. `HTMLWorker` is no longer supported and no questions about `HTMLWorker` are being answered anymore. – Bruno Lowagie Jul 10 '15 at 13:48
  • Oh I see. Then i wil use looping method – Semil Sebastian Jul 10 '15 at 14:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82961/discussion-between-semil-sebastian-and-bruno-lowagie). – Semil Sebastian Jul 10 '15 at 14:05
  • I solved my problem eventhough I have another doubt that I have posted as a separate question. Please have a look.You can help me – Semil Sebastian Jul 11 '15 at 04:44
-1

There is 2 ways to do this depending on the datatype you have in the database.

If you have it stored as a string :

  • 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

If you have it stored as an image :

  • 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
Franck
  • 4,438
  • 1
  • 28
  • 55
  • This is a Crystal report answer to a question tagged as an iTextSharp question. – Bruno Lowagie Jul 10 '15 at 12:19
  • @ Bruno Lowagie, any idea :) – Semil Sebastian Jul 10 '15 at 12:20
  • @Franck That's irrelevant: the OP doesn't mention Crystal Reports. – Bruno Lowagie Jul 10 '15 at 12:21
  • @BrunoLowagie he doesn't mention `String` either so does that prevent people from adding answer that make use of the string object ? – Franck Jul 10 '15 at 12:22
  • @Franck You don't mean that, do you? The `string` object is part of C#; Crystal Reports is a separate product. – Bruno Lowagie Jul 10 '15 at 12:25
  • @BrunoLowagie Feel happier if i say to use Microsoft reporting instead ? It's not a separate product and give the same exact result. – Franck Jul 10 '15 at 12:33
  • @Franck persuade the OP to add the tag crystal-reports and your answer will no longer be off-topic. Right now, the tags say C# and iTextSharp. – Bruno Lowagie Jul 10 '15 at 12:41
  • @Franck Also: you are missing *context*. The OP has posted several questions about iTextSharp and tables in iTextSharp in the last couple of days (http://stackoverflow.com/q/31152874 http://stackoverflow.com/q/31263533 http://stackoverflow.com/q/31283302 ). His question isn't tagged "iTextSharp" by accident. – Bruno Lowagie Jul 10 '15 at 12:55