Inserting a Barcode while merging documents

We are currently using 6.4.0.0 of Words to create order forms customized for each customer. Can you point me to sample documentation on how to insert a bar code into a mail merge field.
Bill

Hi

Thanks for your request. I think, you can use MergeImaeField event handler to achieve this. Please see the following link for more information:
https://docs.aspose.com/words/net/working-with-images/
To generate barcode you can use Aspose.Barcode:
https://docs.aspose.com/barcode/net/product-overview/
Here simple code example:

// Open template.
Document doc = new Document(@"Test001\in.doc");
// Add MergeImageField event handler. We will generate barcode image usng it.
doc.MailMerge.MergeImageField += MailMerge_InsertBarcode;
// As a datasource, we will use object array.
string[] names = {"Name", "Barcode"};
object[] values = {"Alexey", "This is code of your barcode 123456798*"};
// Execute mail merge.
doc.MailMerge.Execute(names, values);
// Save output document.
doc.Save(@"Test001\out.doc");

===========================================================

void MailMerge_InsertBarcode(object sender, Aspose.Words.Reporting.MergeImageFieldEventArgs e)
{
    if (e.FieldName == "Barcode")
    {
        // Create barcode iamges using Aspose.Barcode.
        Aspose.BarCode.BarCodeBuilder bcBuilder = new Aspose.BarCode.BarCodeBuilder();
        bcBuilder.SymbologyType = Aspose.BarCode.Symbology.UPCA;
        bcBuilder.CodeLocation = Aspose.BarCode.CodeLocation.None;
        bcBuilder.CodeText = e.FieldValue.ToString();
        // Set image,which will be inserted into the document (in our case it is barcode).
        e.Image = bcBuilder.GenerateBarCodeImage();
    }
}

I also attached input and output documents. Also Barcode demo could be useful for you:
https://products.aspose.app/
Hope this helps.
Best regards.

Thanks for the quick response.
This look straight forward.
Bill

I have since found out that I will be exporting a list of Persons with addresses to a Pitney-Bowes software. That software will process those address and send them back out with a tray number and an integer representing the barcode for each person. I will update my database with these two data elements so that I can print the mailing documents in tray order and with the barcode.
The barcode font is provided by Pitney-Bowes called uspsplanet font. Manually you if you type the integer produced by the Pitney-Bowes into a word document and surround it with square brackets, highlight this text, and change the font to uspsplanet the barcode appears.
Given this additional information, do I still need the Aspose Barcode? Can the merge field be set to a particular font at design time of the mail merge template or at runtime?
Bill

Hi Bill,

Thanks for your request. If you have barcode font, you can use it during MailMerge, just specify this font as font of the appropriate merge field. Please let me know in case of any issues.
Best regards,

Two last clarifications:
And the barcode and photos (which we also insert) will be preserve if we use the XPS print function? And finally, what is the first version in which you implemented the print function. It appears that our version 6.4 does not.
Bill

Hi

Thanks for your request. Could you please attach your barcode font here for testing? I will check it on my side and provide you more information. Export documents or pages as XPS is supported starting from 6.5.0 version of Aspose.Words.
Best regards,

Turns out the barcode works. Just setting the font on the template for that field works.
I have concern whether it will work when using your print function. Attached are the fonts.
Regarding the Words version see my post at:
Thank you again for your time and interest.

Hi

Thank you for additional information. I tried converting a Word document with barcode, which uses the fonts, you attached and an exception occurs while saving to XPS. I will notify you as soon as this problem is resolved.
Best regards,

I forgot one small detail. The barcorde is represented textually as an integer up to 12 digits enclosed by square brackets.
When I created the merge field I put the [ and ] in the text before and after respectfully. When you set the mergefield and its enclosing brackets to the barcode font, it works. Again, I have no way of knowing if it would work when printing from within Aspose.
Bill

Hi

Thank you for additional information. You can attach sample template here for testing. I will check how it goes on my side.
Best regards,

Attached is the template: Currently I have the PitneyBowlesRegular as the font of the Barcode merge field on the template. I sent an earlier reply with the fonts attached.

Hi

Thank you for additional information. Printing works fine with barcode. I used the following code for testing:

Document doc = new Document(@"Test001\in.doc");
doc.MailMerge.Execute(new string[] { "BarCode" }, new object[] { 123456789012 });
PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "Microsoft Office Document Image Writer";
settings.PrintToFile = true;
settings.PrintFileName = @"Test001\out.tif";
doc.Print(settings);

I attached the output TIF file.
Best regards,

The issues you have found earlier (filed as 13544) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(20)

Hi, Since this article was written, it appears that many namespace changes have taken place. For example, doc.MailMerge.MergeImageField does not exist. I am trying to do a mail merge on a 6 labels to a page Word document and insert a bar code based on an integer ID that is a MailMerge field inserted into the template, and this sample is the closest thing I can find in your documents, but I am missing something. Any assistance much appreciated. Thanks.

Hi Karl,

Thanks for the inquiry.

First off, please note that we release a new version of Aspose.Words every month. Each new release contains many improvements, bug fixes and new features. So, we suggest you always use latest version of Aspose.Words. In addition, please follow up the appropriate documentation links below:

https://reference.aspose.com/words/net/aspose.words.mailmerging/imagefieldmergingargs/
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
https://reference.aspose.com/words/net/aspose.words.replacing/ireplacingcallback/

I hope, this will help. In case of further assistance please let me know.

Hi,
I have a similar requirement. I have to add a 2D Matrix Barcode in an existing document using JAVA. The 2D matrix is to be added at only the even number page. Can you please share a sample code for doing this in Java.
Thanks.

Hi Sayandip,

Thanks for your inquiry. I believe you can achieve what you are looking for after using the following code snippet:

DocumentBuilder builder = new DocumentBuilder();
builder.getDocument().getFirstSection().getPageSetup().setOddAndEvenPagesHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
Shape shape = builder.insertImage("C:\Temp\barcodeImage.png");
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.moveToDocumentEnd();

for (int i = 0; i < 200; i++)
    builder.writeln("Some Paragraphs");

builder.getDocument().save("C:\\Temp\\out.docx");

Please let me know if I can be of any further assistance.

Best regards,

I need to do something similar. I am doing a mail merge into a word template and outputting pdf documents. What I now need to do is create an intelligent mail barcode to also merge into the document. The barcode needs to be created at runtime and then that barcode needs to be merged to a mergefield location in the document.

Hi Olen,
Please check https://docs.aspose.com/words/net/types-of-mail-merge-operations/ to insert images during mail merge. You need to insert image merge fields (name the field like Image:FieldName) in the template and then pass path to image as value during runtime to load barcode images.
You can use the code from above mentioned link if you want to pass image streams.
Best Regards,