Skip to main content Skip to footer

Barcode Cell Type in Spread for ASP.NET

As you may know, Spread is extremely flexible when it comes to designing your own cell types and even though a barcode cell isn't native in Spread for ASP.NET, creating your own is a very facile task. There are a few ways to do this and the path I chose was to use the Aspose barcode generator .NET control (Any barcode generator will work.) You can also use an online generator, as long as you're able to pass a query string to the URL. Firstly, what we'll do is create our cell type.

[Serializable()]  
public class BarcodeCellType : FarPoint.Web.Spread.TextCellType  
{  
 public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object value, bool upperLevel)  
 {  
  parent.Attributes.Add("FpCellType", "BarcodeCellType");  
  ApplyStyleTo(parent, style, margin, true);  

  System.Web.UI.WebControls.Image c = new System.Web.UI.WebControls.Image();  
  c.AlternateText = "";  
  c.ImageUrl = "~/bcimagemanager.aspx?value=" + value.ToString();  
  return c;  
 }  
} 

We'll inherit the TextCellType because its GetEditorControl class is already designed to accept a string value in the cell's edit mode and all we'll need to override is the PaintCell. Now that the cell type is complete, we'll now need to construct a dynamic URL to the barecode image. Add a new ASPX page to your project. In the server side code of this new page, we'll reference the assembly that's generating our barcode image and add the following code.

using Aspose.BarCode;

...

public partial class bcimagemanager : System.Web.UI.Page  
 {  
  protected void Page_Load(object sender, EventArgs e)  
  {  
   if (Request.QueryString["value"].ToString() == null) return;  
   byte[] imgContent = GenerateBarCode(Request.QueryString["value"].ToString());  
   Response.ContentType = "image/jpeg";  
   Response.BinaryWrite(imgContent);  
  }  
public byte[] GenerateBarCode(string codeInfo)  
  {  
  using (MemoryStream ms = new MemoryStream())  
   {  
    BarCodeBuilder bb = new BarCodeBuilder();  
    bb.CodeText = codeInfo;  
    bb.SymbologyType = Symbology.Code128;  
    bb.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
    byte[] imgContent = new Byte[ms.Length];  
    ms.Position = 0;  
    ms.Read(imgContent, 0, (int)ms.Length);  
    return imgContent;  
   }  
  }  
 }

Using a query string in the URL, we'll pass the value of the cell to this aspx page. The image is generated on the page and the page's url is referenced by the Image control's ImageURL property. And voila! barcodeCT Project

MESCIUS inc.

comments powered by Disqus