Free Support Forum - aspose.com

Alignment and font changes while building dynamic table

I’ve figured out how to generate a dynamically growing table with the proper start and end merge fields – however, in my table I need to add rows that have sub totals and a final total row whose cells are right aligned and are in bold. Is this possible to do? My dataset table has these rows already defined.

I thought maybe by creating my own MergeFieldEventHandler, I could possibly do the formatting changes that I needed but the MergeFieldEventArgs object that is passed to my event doesn’t seem to allow me to make these changes. Am I missing something here or is Aspose.Word not able to do this?

Any help would be greatly appreciated.

Plus, any idea as to when importing RTF will be supported?

Steve

p.s. I’m using Aspose.Word version 1.7.5

Inside your event handler, create a DocumentBuilder object, call its MoveToMergeField(name) method and you can insert and format text from there. You can even create one DocumentBuilder just before starting mail merge, like thin this example:

private Document doc;

private DocumentBuilder builder;

///

/// Test if I can use the DocumentBuilder during mail merge.

///

public void TestMailMergeRegionWithDocBuilder()

{

this.doc = TestUtil.Open(@"MailMerge\TestMailMergeRegionSimple.doc");

this.doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeFieldEvent);

this.builder = new DocumentBuilder(doc);

DataSet ds = CreateTestDataSet();

doc.MailMerge.ExecuteWithRegions(ds);

TestUtil.Save(doc, @"MailMerge\TestMailMergeRegionWithDocBuilder Out.doc");

}


///

/// Makes product description blue if it contains the word "Blue", otherwise makes it red.

///

private void HandleMergeFieldEvent(object sender, MergeFieldEventArgs e)

{

Assert.AreEqual(this.doc, e.Document);

if (e.FieldName == "Description")

{

builder.MoveToMergeField(e.FieldName);

string fieldValue = e.FieldValue.ToString();

if (fieldValue.IndexOf("Blue") != -1)

builder.Font.Color = System.Drawing.Color.Blue;

else

builder.Font.Color = System.Drawing.Color.Red;

//Write the text using the builder, don't let the mail merge engine to insert text too.

builder.Write(fieldValue);

e.Text = "";

}

}

That did the trick! Thanks.