FieldMergeBarcodeBarcodeValue Property
Gets or sets the barcode value.

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public string BarcodeValue { get; set; }

Property Value

Type: String
Examples
Shows how to use MERGEBARCODE fields to integrate barcodes into mail merge operations.
public void FieldMergeBarcode_QR()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a QR code
    FieldMergeBarcode field = (FieldMergeBarcode) builder.InsertField(FieldType.FieldMergeBarcode, true);
    field.BarcodeType = "QR";

    // In a DISPLAYBARCODE field, the BarcodeValue attribute decides what value the barcode will display
    // However in our MERGEBARCODE fields, it has the same function as the FieldName attribute of a MERGEFIELD
    field.BarcodeValue = "MyQRCode";
    field.BackgroundColor = "0xF8BD69";
    field.ForegroundColor = "0xB5413B";
    field.ErrorCorrectionLevel = "3";
    field.ScalingFactor = "250";
    field.SymbolHeight = "1000";
    field.SymbolRotation = "0";

    Assert.AreEqual(" MERGEBARCODE  MyQRCode QR \\b 0xF8BD69 \\f 0xB5413B \\q 3 \\s 250 \\h 1000 \\r 0",
        field.GetFieldCode());
    builder.Writeln();

    // Create a data source for our mail merge
    // This source is a data table, whose column names correspond to the FieldName attributes of MERGEFIELD fields
    // as well as BarcodeValue attributes of DISPLAYBARCODE fields
    DataTable table = CreateTable("Barcodes", new[] { "MyQRCode" },
        new[,] { { "ABC123" }, { "DEF456" } });

    // During the mail merge, all our MERGEBARCODE fields will be converted into DISPLAYBARCODE fields,
    // with values from the data table rows deposited into corresponding BarcodeValue attributes
    doc.MailMerge.Execute(table);

    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[0].Type);
    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[1].Type);

    Assert.AreEqual("DISPLAYBARCODE \"ABC123\" QR \\q 3 \\s 250 \\h 1000 \\r 0 \\b 0xF8BD69 \\f 0xB5413B",
        doc.Range.Fields[0].GetFieldCode());
    Assert.AreEqual("DISPLAYBARCODE \"DEF456\" QR \\q 3 \\s 250 \\h 1000 \\r 0 \\b 0xF8BD69 \\f 0xB5413B",
        doc.Range.Fields[1].GetFieldCode());

    doc.Save(ArtifactsDir + "Field.MergeBarcode_QR.docx");
}

public void FieldMergeBarcode_EAN13()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a EAN13 barcode
    FieldMergeBarcode field = (FieldMergeBarcode) builder.InsertField(FieldType.FieldMergeBarcode, true);
    field.BarcodeType = "EAN13";
    field.BarcodeValue = "MyEAN13Barcode";
    field.DisplayText = true;
    field.PosCodeStyle = "CASE";
    field.FixCheckDigit = true;

    Assert.AreEqual(" MERGEBARCODE  MyEAN13Barcode EAN13 \\t \\p CASE \\x", field.GetFieldCode());
    builder.Writeln();

    DataTable table = CreateTable("Barcodes", new[] { "MyEAN13Barcode" },
        new[,] { { "501234567890" }, { "123456789012" } });

    doc.MailMerge.Execute(table);

    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[0].Type);
    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[1].Type);

    Assert.AreEqual("DISPLAYBARCODE \"501234567890\" EAN13 \\t \\p CASE \\x",
        doc.Range.Fields[0].GetFieldCode());
    Assert.AreEqual("DISPLAYBARCODE \"123456789012\" EAN13 \\t \\p CASE \\x",
        doc.Range.Fields[1].GetFieldCode());

    doc.Save(ArtifactsDir + "Field.MergeBarcode_EAN13.docx");
}

public void FieldMergeBarcode_CODE39()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a CODE39 barcode
    FieldMergeBarcode field = (FieldMergeBarcode) builder.InsertField(FieldType.FieldMergeBarcode, true);
    field.BarcodeType = "CODE39";
    field.BarcodeValue = "MyCODE39Barcode";
    field.AddStartStopChar = true;

    Assert.AreEqual(" MERGEBARCODE  MyCODE39Barcode CODE39 \\d", field.GetFieldCode());
    builder.Writeln();

    DataTable table = CreateTable("Barcodes", new[] { "MyCODE39Barcode" },
        new[,] { { "12345ABCDE" }, { "67890FGHIJ" } });

    doc.MailMerge.Execute(table);

    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[0].Type);
    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[1].Type);

    Assert.AreEqual("DISPLAYBARCODE \"12345ABCDE\" CODE39 \\d",
        doc.Range.Fields[0].GetFieldCode());
    Assert.AreEqual("DISPLAYBARCODE \"67890FGHIJ\" CODE39 \\d",
        doc.Range.Fields[1].GetFieldCode());

    doc.Save(ArtifactsDir + "Field.MergeBarcode_CODE39.docx");
}

public void FieldMergeBarcode_ITF14()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a ITF14 barcode
    FieldMergeBarcode field = (FieldMergeBarcode) builder.InsertField(FieldType.FieldMergeBarcode, true);
    field.BarcodeType = "ITF14";
    field.BarcodeValue = "MyITF14Barcode";
    field.CaseCodeStyle = "STD";

    Assert.AreEqual(" MERGEBARCODE  MyITF14Barcode ITF14 \\c STD", field.GetFieldCode());

    DataTable table = CreateTable("Barcodes", new[] { "MyITF14Barcode" },
        new[,] { { "09312345678907" }, { "1234567891234" } });

    doc.MailMerge.Execute(table);

    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[0].Type);
    Assert.AreEqual(FieldType.FieldDisplayBarcode, doc.Range.Fields[1].Type);

    Assert.AreEqual("DISPLAYBARCODE \"09312345678907\" ITF14 \\c STD",
        doc.Range.Fields[0].GetFieldCode());
    Assert.AreEqual("DISPLAYBARCODE \"1234567891234\" ITF14 \\c STD",
        doc.Range.Fields[1].GetFieldCode());

    doc.Save(ArtifactsDir + "Field.MergeBarcode_ITF14.docx");
}

/// <summary>
/// Creates a DataTable named by dataTableName, adds a column for every element in columnNames
/// and fills rows with data from dataSet
/// </summary>
public DataTable CreateTable(string dataTableName, string[] columnNames, object[,] dataSet)
{
    if (dataTableName != string.Empty || columnNames.Length != 0)
    {
        DataTable table = new DataTable(dataTableName);

        foreach (string columnName in columnNames)
        {
            table.Columns.Add(columnName);
        }

        foreach (object data in dataSet)
        {
            table.Rows.Add(data);
        }

        return table;
    }

    throw new ArgumentException("DataTable name and Column name must be declared.");
}
See Also