How to create an anchored gridview barcode to PDF?
I have a grid that links some barcodes to databases. These are the barcodes I want to generate in pdf format. How can i do this? Sorry, I don't know how to do this so there is no sample code. My grid name is "GrdBarcode". Please help me. Bowow 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>
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();
}
source to share
Whenever you need a mesh, it's best to use PdfPTable
.
If you don't have barcodes yet, you can create one using iText or iTextSharp. Take a look at 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 when generating barcodes see:
- Barcode with text when using ItextSharp
- Add the itextsharp barcode image to PdfPCell
- generate barcode in pdf when using itextsharp to generate pdf
- ...
If you already have barcode images you can still use a table, but then there must be a question. How do I organize images within a table?
Answers to questions such as:
source to share
There are two ways to do this, depending on the type of data you have in your database.
If you saved it as a string:
- Create your Crystal Report
- Drag the related field to the report
- change the font of the box to the barcode font you have (usually comes with a stick reader)
- Export report to PDF
If you saved it as an image:
- Create your crystal report
- Get image data from database as
Image
object- place the object
Image
inMemoryStream
- convert
MemoryStream
toByte[]
- add a column to your data type crystal report
Byte[]
- store the value in this column
- Drag a new data field to the report and it will be an image
- Export report to PDF
source to share