Labels Printing

Hi,

I want to print mailing labels 3 columns and 8 rows across on one page. I have setup the word document with regions but it doesn’t merge my fields.
Sample Code

// Open the template document.
Document doc = new Document(dataDir + "Labels.docx");

var examAttendes = GetAvailableEnrolments();
if (examAttendes.Count() > 0)
{
    var examAttendances = ConvertToDataTable(examAttendes);

    doc.MailMerge.ExecuteWithRegions(examAttendances);
}

Please find the zip file attached.

Zip file contains:
Labels - the mail merge document
Labels output - expected output
Labels Actual output = LabelsMerged.pdf

When i use merge with the document i get repeat of the same values across the columns. I want unique values across the 3*8 (columns * rows).

Thanks

Hi,
You cannot merge data horizontally as well as vertically at the same time. The better solution in your case is to update your dataset and pass three records per row. I have modified your template to accept three records per row so you need to update your dataset accordingly. Please check attached updated template.
Best Regards,

Where is the updated template?

Hi,
Updated Labels.docx is attached to my previous post here. You can download this file and update your data source accordingly.
Best Regards,

Hi Support Team,

I have done as requested.

for(int i=0; i < count; i++)
{
    var smallDataSet = examAttendances.Rows.Cast().Skip(i*3).Take(3).CopyToDataTable();
    doc.MailMerge.ExecuteWithRegions(smallDataSet);
}

Mow i get a new issue every time i try to merge i get this error

Exception of type ‘System.OutOfMemoryException’ was thrown.

on the statement doc.MailMerge.ExecuteWithRegions(smallDataSet);

Also i want to merge a bar code and am using the true type font code 39 but it doesn’t show the text in the format

Can you please help.

Thanks,
Sonal

Hi Sonal,
Can you please share your data in XML format and your template with code to reproduce the issue?
Best Regards,

Hi Support Team,

I am sending you the sample xml file and the code which reads the data set and merges. Looks like i get no merge done in the output file saved.

Can you also tell me i am prinitng bar code using the BarCodeText field with AIB-198452 but it only converts the bar code text AIB-198452 and leaves the stars as it is in the merge process.

Can you please look into this and address this issue ASAP.

Thanks,
Sonal

Hi Sonal,
Field names in your template should match the column names in your table or tag names in your XML. It looks like the field names used in your template do not match column/XML tag names in your table/XML e.g. the repeating elements in your XML are Students but the table name in your template is TableStart:ExamAttendanceSheet. Similarly you have used FullName field in your template but there is no such element in the XML. Please note that your field names should match XML elements.
I have renamed the table in your input document to Students and now it merges the data (only matching fields). Also I can see stars in your barcode text after merge. Please use the following code with the attached updated template (Labels.docx) to check the result.

// Open an existing document.
Document doc = new Document("Labels.docx");
 
DataSet dataSet = new DataSet();
dataSet.ReadXml("Students.xml");
// Fill the fields in the document with user data.
doc.MailMerge.ExecuteWithRegions(dataSet.Tables[1]);
doc.Save("Labels_Out.docx");

Best Regards,

Hi Ijaz,

Do you have any updates on this issue?

Thanks,
Sonal

Still awaiting response… can you please let me know by when I can expect this resolved?

Hi Sonal,
In your case, you can add three child tables to the respective cells of your main table and then divide your data into three tables. I have updated your template and XML according to your requirement and also written the code to divide your XML data into three tables.
Please use attached XML and template with the following code and let us know if the output is acceptable for you.

// Open an existing document.
Document doc = new Document("LabelsTest.docx");

// Add a handler for the MergeField event.
// doc.MailMerge.FieldMergingCallback = new HandleMergeField();
DataSet dataSet = new DataSet();
dataSet.ReadXml("Students1.xml");
DataTable students1 = dataSet.Tables["Students"].Clone();
students1.TableName = "Students1";
DataTable students2 = dataSet.Tables["Students"].Clone();
students2.TableName = "Students2";
DataTable students3 = dataSet.Tables["Students"].Clone();
students3.TableName = "Students3";
int totalRows = dataSet.Tables[1].Rows.Count;
int remainder = totalRows % 3;
int rowsPerTable = (totalRows - remainder) / 3;
if (remainder == 1)
{
    int index = 0;
    foreach (DataRow row in dataSet.Tables["Students"].Rows)
    {
        if (index <= rowsPerTable)
        {
            students1.Rows.Add(row.ItemArray);
        }
        else if (index > rowsPerTable && index <= (rowsPerTable + rowsPerTable))
        {
            students2.Rows.Add(row.ItemArray);
        }
        else
        {
            students3.Rows.Add(row.ItemArray);
        }
        index++;
    }
}
else if (remainder == 2)
{
    int index = 0;
    foreach (DataRow row in dataSet.Tables["Students"].Rows)
    {
        if (index <= rowsPerTable)
        {
            students1.Rows.Add(row.ItemArray);
        }
        else if (index > rowsPerTable && index <= (rowsPerTable + rowsPerTable + 1))
        {
            students2.Rows.Add(row.ItemArray);
        }
        else
        {
            students3.Rows.Add(row.ItemArray);
        }
        index++;
    }
}
else
{
    int index = 0;
    foreach (DataRow row in dataSet.Tables["Students"].Rows)
    {
        if (index < rowsPerTable)
        {
            students1.Rows.Add(row.ItemArray);
        }
        else if (index >= rowsPerTable && index < (rowsPerTable + rowsPerTable))
        {
            students2.Rows.Add(row.ItemArray);
        }
        else
        {
            students3.Rows.Add(row.ItemArray);
        }
        index++;
    }
}
// Fill the fields in the document with user data.
doc.MailMerge.ExecuteWithRegions(students1);
doc.MailMerge.ExecuteWithRegions(students2);
doc.MailMerge.ExecuteWithRegions(students3);
doc.Save("Labels_Out.docx");

Best Regards,

HI Ijaz,

Can you check if you get bar code with stars or not for this file?

Thanks,
Sonal

HI Ijaz,

Can you check if you resolve the issue with stars in the bar code and update the issue.

Thanks,
Sonal

Hi Sonal,
We will try to share a solution as soon as possible.
Best Regards,

Hi IJaz,

Do you know by when we can get a solution for this one. It is critical it is resolved ASAP.

Thanks,
Sonal

Hi Sonal,
You can use FieldMergingCallback event in this case. Please call the following code before executing mail merge.

doc.MailMerge.FieldMergingCallback = new HandleMergeField();

HandleMergeField class will be like the following.

class HandleMergeField : IFieldMergingCallback
{
    private DocumentBuilder mBuilder;
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if
        (mBuilder == null)
            mBuilder = new
            DocumentBuilder(args.Document);

        if
        (args.FieldName == "BarCodeText")
        {
            string
            updatedValue = args.FieldValue.ToString();

            mBuilder.MoveToMergeField(args.FieldName);
            mBuilder.Font.Name = "Free 3 of 9 Extended";
            mBuilder.Font.Size = 20;
            mBuilder.Write(updatedValue);
        }

    }


    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do
        nothing.
    }
}

Hopefully this will resolve the issue at your end.
Best Regards,

Hi Ijaz,

Thanks, the bar code are working as expected now.

Just one small issue left.

When I merge my dataset at the table end it introduces blank spaces so the document is not lining correctly if you can help to sort it out that would be awesome.

I have attached the document and labelled the area as extra spacing in red.

Thanks,
Sonal

Hi Sonal,
In fact your requirement is a special case where we need to use three nested tables to present data. There is a limitation in MS Word that there must be one paragraph space between two tables or between text and a table. This limitation was not present in older versions of MS Word and it was possible to remove space in those versions however it is not possible with the newer versions.
In your case, we add a table start field and then add a nested table after that field. Now table start field and nested table cannot be inserted on the same line and there must be one paragraph space between them; if you try to place them on the same line, MS Word will automatically shift the table to next line. This is the root cause of the extra space you see in your document.
The only thing possible in this case is remove either top or bottom borders of three nested tables. This will not remove the space however it will show only one line instead of empty box after every record.
Best Regards,