Free Support Forum - aspose.com

Design a Badge using Word and Barcode

Hi,

I want to design and populate data for an event badge on Word using barcode. So its going to be a landscape word page which can have 4 badges that will contain Name of the person, Comapny, an image and a barcode(based on an ID field) genegated on the fly from the data comming from the database.

Please advice how do i design the badge template on word and then populate that with the data including the barcode.

Please see the attachment as an example.

thanks

Hello

Thanks for your request. I have created the template for you. Please see the attached input and output documents. I used the following code to fill the attached template with data:

Document doc = new Document("C:\\Temp\\in.doc");

// Create dummy datasource

DataTable dtLabelTable = new DataTable("MyTable");

dtLabelTable.Columns.Add("LogoImage");

dtLabelTable.Columns.Add("FirstName");

dtLabelTable.Columns.Add("LastName");

dtLabelTable.Columns.Add("Company");

dtLabelTable.Columns.Add("BarCode");

//Add some data

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Andrey", "N", "Aspose", "123456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Daniel", "Blah", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Sami", "Blah", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Ken", "Blah", "XZY Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Andrey1", "N1", "Aspose", "123456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Daniel1", "Blah1", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Sami1", "Blah1", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Ken1", "Blah1", "XZY Corp.", "987456" });

// Execute MailMerge with regions to fill the document with data.

doc.MailMerge.ExecuteWithRegions(dtLabelTable);

// Save document

doc.Save("C:\\Temp\\out.doc");

Best regards,

thank you so much.


How do i render the barcode into this template? I also want to show the number right below the barcode.

Here is what i am doing.

BarCodeBuilder lbc = new BarCodeBuilder();
//Set the symbology type to Code128
lbc.SymbologyType = Symbology.Code128;
lbc.BorderVisible = false;
lbc.BarHeight = 8f;
lbc.xDimension = 0.5f;

foreach (DataRow dr in objDataSet.Tables[0].Rows)
{
// for barcode
lbc.CodeText = dr[“wwid”].ToString();

//Add some data
dtLabelTable.Rows.Add(new object[] { Application[“PhysRoot”] + “images\icap_logo_sm.gif”, dr[“fst_nm”].ToString(), dr[“lst_nm”].ToString(), dr[“co_nm”].ToString(), lbc.BarCodeImage });

}

Hi

Thanks for your request. You can use the same approach as suggested in BarcodeDemo:

http://www.aspose.com/demos/.net-components/aspose.words/csharp/general/barcode.aspx

Hope this helps. Please let us know if you need more assistance, we are always glad to help you.

Best regards,

I still couldn’t get the barcode to work. Could you please help with what piece of code i should be using for barcode in the sample code i posted above.


Thanks

Hi

Thanks for your request. Could you please clarify what exactly does not work, you cannot generate a barcode image or cannot insert the image into the template upon mail merge?

The workflow is quite simple:

1. Generate a barcode image using Aspose.Barcode.

2. Insert a generated image into Word document using Aspose.Words.

Best regards,

This is what i am doing. And the out put that i get is that attached file. how do i get the barcode to show up in place of Aspose.Words.Drawing.Shape.


Document doc = new Document(Application[“PhysRoot”] + “admin\inBadge.doc”);
BarCodeBuilder lbc = new BarCodeBuilder();
//Set the symbology type to Code128
lbc.SymbologyType = Symbology.Code128;
lbc.BorderVisible = false;
lbc.BarHeight = 8f;
lbc.xDimension = 0.5f;

DataTable dtLabelTable = new DataTable(“MyTable”);
dtLabelTable.Columns.Add(“LogoImage”);
dtLabelTable.Columns.Add(“FirstName”);
dtLabelTable.Columns.Add(“LastName”);
dtLabelTable.Columns.Add(“Company”);
dtLabelTable.Columns.Add(“BarCode”);

DocumentBuilder builder = new DocumentBuilder(doc);

foreach (DataRow dr in objDataSet.Tables[0].Rows)
{
lbc.CodeText = dr[“wwid”].ToString();
//Add some data
dtLabelTable.Rows.Add(new object[] { Application[“PhysRoot”] + path, dr[“fst_nm”].ToString(), dr[“lst_nm”].ToString(), dr[“co_nm”].ToString(), builder.InsertImage(lbc.BarCodeImage) });

}

// Execute MailMerge with regions to fill the document with data.
doc.MailMerge.ExecuteWithRegions(dtLabelTable);

Hello

Thanks for your request. You can use IFieldMergingCallback to achieve this. For example, see the following code that demonstrates how to insert generated image upon executing mail merge:

Document doc = new Document("C:\\Temp\\in.doc");

// Create dummy datasource

DataTable dtLabelTable = new DataTable("MyTable");

dtLabelTable.Columns.Add("LogoImage");

dtLabelTable.Columns.Add("FirstName");

dtLabelTable.Columns.Add("LastName");

dtLabelTable.Columns.Add("Company");

dtLabelTable.Columns.Add("BarCode");

//Add some data

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Andrey", "N", "Aspose", "123456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Daniel", "Blah", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Sami", "Blah", "ABC Investment Corp.", "987456" });

dtLabelTable.Rows.Add(new object[] { "C:\\Temp\\img.png", "Ken", "Blah", "XZY Corp.", "987456" });

doc.MailMerge.FieldMergingCallback = new MergeImageFieldBarCode();

// Execute MailMerge with regions to fill the document with data.

doc.MailMerge.ExecuteWithRegions(dtLabelTable);

// Save document

doc.Save("C:\\Temp\\out.doc");

private class MergeImageFieldBarCode : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

// Do nothing.

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

if (e.FieldName == "BarCode")

{

// Create DocuemntBuilder and move its cursor to the mergefield

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

BarCodeBuilder lbc = new BarCodeBuilder();

//Set the symbology type to Code128

lbc.SymbologyType = Symbology.Code128;

lbc.BorderVisible = false;

lbc.BarHeight = 8f;

lbc.xDimension = 0.5f;

lbc.CodeText = e.FieldValue.ToString();

// Insert BarCode image

builder.InsertImage(lbc.BarCodeImage);

e.Field.Remove();

}

}

}

Also please see the attached input and output documents.

Best regards,