Free Barcode Generator for RDLC PDF report using C# and ASP .net

1. Create .xsd report data and add a column ‘barCode’ of type System.Byte[]

2. Insert Image control to RDLC and right click image control –> properties  (general tab)

set image source database, fields ‘barCode’, type image/png

3. install font ‘Free 3 of 9’ to your computer/ server

4. Code module:

protected void lbApplicationReceived_Click(object sender, EventArgs e)
{
String Qs = ” SELECT TOP 1 APPLICATION_SN, PASSWORD, APPLICANT_NAME, FATHER_NAME,MOTHER_NAME, ” +
” MOBILE,EMAIL,BIRTH_DATE,SUBMISSION_DATE, SESSIONS, PHOTO, SIGNATURE, ” +
” ATP, REMOTEADDRESS, TELETALK_ISOK  FROM ug_applicant_general_info WHERE APPLICATION_SN = @APPLICATION_SN “;

ReportViewer ApplicationReportViewer = new ReportViewer();

try
{
String APPLICATION_SN = Session[“APPLICATION_SN”].ToString().Trim();

ApplicationReportViewer.Reset();
ApplicationReportViewer.LocalReport.Dispose();

DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“CONNECTIONSTRING”].ConnectionString))
{
SqlCommand cmd = new SqlCommand(Qs, con);
cmd.Parameters.AddWithValue(“@APPLICATION_SN”, APPLICATION_SN);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
}

DataTable dt = new DataTable();
dt = ds.Tables[0];

// adding new column to datatable and assign value
System.Data.DataColumn newColumn = new System.Data.DataColumn(“barCode”, typeof(System.Byte[]));
newColumn.DefaultValue = ImageToByte(CreateBarcode(APPLICATION_SN));
dt.Columns.Add(newColumn);

ReportDataSource rds = new ReportDataSource(“datasetApplicationReceipt”, dt);
ApplicationReportViewer.ProcessingMode = ProcessingMode.Local;
ApplicationReportViewer.LocalReport.ReportPath = Server.MapPath(“rptApplicationReceipt.rdlc”);
ApplicationReportViewer.LocalReport.DataSources.Add(rds);
ApplicationReportViewer.LocalReport.Refresh();
//Auto PDF // Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = ApplicationReportViewer.LocalReport.Render(“PDF”, null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader(“content-disposition”, “attachment; filename=SN_” + APPLICATION_SN + “.” + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download
}
catch (Exception ex) { }
}

// Barcode Module.
private Bitmap CreateBarcode(string data)
{
// Note: “Free 3 of 9” font mast be installed to the server
Bitmap barCode = new Bitmap(1, 1);
Font threeOfNine = new Font(“Free 3 of 9”,60, System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point);
Graphics graphics = Graphics.FromImage(barCode);
SizeF dataSize = graphics.MeasureString(data, threeOfNine);
barCode = new Bitmap(barCode, dataSize.ToSize());
graphics = Graphics.FromImage(barCode);
graphics.Clear(Color.White);
graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);
graphics.Flush();
threeOfNine.Dispose();
graphics.Dispose();
return barCode;
}

public static byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s