Monday, August 09, 2010

Rendering barcode in MVC project

To render barcode on web page, we could use TTS barcode font, some of them can be downloaded here for free.

I found some other method that doesn't requiring third-party fonts. One is call Zen Barcode Rendering Framework, it encapsulates the native rendering of barcode symbologies. Zen Barcode has a server side control BarcodeLabel, together with the barcode handler, it can display barcode on ASP.Net page.


To work with MVC project, I wrote a HTML helper method to render the image tag. Also to display a much friend jpg url, I also config a URL rewrite rule on IIS7. Below is the step by step guide:


1. First step is reference two DLLs:
Zen.Barcode.Core.dll
Zen.Barcode.Web.dll

2. Add HttpHandler to web.config so Zen Barcode will handling the .Barcode extension


type="Zen.Barcode.Web.BarcodeImageHandler, Zen.Barcode.Web,
Culture=Neutral, Version=2.0.3.0"/>


In IIS7, the handler also need to be added to system.webServer section.







Also add a namespace to web.config




3. Create HTML helper method for MVC view

With the helper function below, we can write the code in view like


<%= Html.BarcodeImage("634169458190082014")%>


Below is the helper function:


public static string BarcodeImage(this HtmlHelper htmlHelper, string text)
{
return BarcodeImage(htmlHelper, text, BarcodeSymbology.Code128);
}

public static string BarcodeImage(this HtmlHelper htmlHelper, string text, BarcodeSymbology symbology)
{
return BarcodeImage(htmlHelper, text, symbology, 2, 40, 1, 30);
}

public static string BarcodeImage(this HtmlHelper htmlHelper, string text, BarcodeSymbology symbology, int maxWidth, int maxHeight, int minWidth, int minHeight)
{
// Setup barcode image url
BarcodeImageUriBuilder builder = new BarcodeImageUriBuilder();
builder.Text = text;
builder.EncodingScheme = symbology;
builder.BarMinHeight = minHeight;
builder.BarMaxHeight = maxHeight;
builder.BarMinWidth = minWidth;
builder.BarMaxWidth = maxWidth;
string url = BuildEncodedParameter(builder.ToString());
return htmlHelper.Image(url, new { style = "border-width: 0px;" }).ToString();
}

private static string BuildEncodedParameter(string url)
{
if (string.IsNullOrEmpty(url))
url = "";
else
url = url.Replace(".Barcode","");

return "/barcode.jpg?" + url;

}


This helper method will render a img tag like below

 



The query string contains the base64 hash string of barcode information. It should be rewrited to an URL recognized by Zen HTTP handler.

4. URL Rewrite Rules in IIS7

 








This rule will rewrite the above URL to a format like below so Zen Handler can process

 
77u_QmFyY29kZVs0LDMwLDQwLDEsMl06NjM0MTY5NDU4MTkwMDgyMDE0OjE4MTQ4OTMyMQ2.Barcode



Done!


7 comments:

Anonymous said...

Hi, I think your blog might be having browser compatibility issues.
When I look at your blog site in Opera, it looks fine
but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, great blog!


Feel free to surf to my site; Will be gynecomastiarelated to steroids?

Anonymous said...

Today, I went to the beach with my children. I found a sea shell and gave it
to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.

There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!


Here is my weblog: chestfatburner.com

Anonymous said...

For most up-to-date information you have to pay a visit internet and on internet I found this site as a most excellent web page
for newest updates.

My web-site Workouts for a larger chest

Anonymous said...

I wanted to thank you for this wonderful read!! I definitely loved every bit of it.
I've got you book-marked to look at new things you post…

My webpage ... chestfatburner.com

Anonymous said...

bookmarked!!, I really like your blog!

Feel free to surf to my web-site; chestfatburner.com

Anonymous said...

I’m not that much of a internet reader to be honest but your sites really nice,
keep it up! I'll go ahead and bookmark your site to come back later on. Cheers

Visit my web-site - http://chestfatburner.com

Anonymous said...

Simply, it is not really difficult finding watches whether it is for routine usage or for party or special occasions.
A classic and timeless design, this watch boasts a round polished and brushed
stainless steel case and bracelet, all of which is coated in a gold-tone finish for chic
sophistication. The company was his legacy and he taught his sons the art of watch making as well.


Here is my page :: michael kors watches