I want that the font size of a text merge field to adjust according to the size of text box i.e if the text length is increasing than the font size should shrink automatically. I am using ASPOSE.words and mail merge to bind values dynamically.
Hi Imran,
Automatically shrink font size in a text box is not supported in MS Word. You can grow text box size according to text length in MS Word or you can manually reduce font size. Same is supported in Aspose.Words for .NET. Following code (Sample document is also attached) executes mail merge by setting the font size manually and resizes the text box according to text length.
using Aspose.Words;
using Aspose.Words.Reporting;
namespace MailMerge
{
class Program
{
static void Main(string[] args)
{
try
{
// Open an existing document.
Document doc = new Document("SimpleMerge.docx");
// Add a handler for the MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
// Fill the fields in the document with user data.
doc.MailMerge.Execute(
new string[] { "Field1", "Field2" },
new object[] { "James Bond", "London, A quick brown fox jumps" });
Aspose.Words.NodeCollection nodes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Aspose.Words.Node node in nodes)
{
Aspose.Words.Drawing.Shape shape = node as Aspose.Words.Drawing.Shape;
if (shape.StoryType == StoryType.Textbox)
{
shape.TextBox.FitShapeToText = true;
shape.TextBox.TextBoxWrapMode = Aspose.Words.Drawing.TextBoxWrapMode.None;
}
}
doc.Save("Output.docx");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Done");
Console.ReadKey();
}
}
class HandleMergeField : IFieldMergingCallback
{
private DocumentBuilder mBuilder;
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
if (mBuilder == null)
mBuilder = new DocumentBuilder(args.Document);
// This way we catch the beginning of a new row.
if (args.FieldValue.ToString().Length > 7)
{
mBuilder.MoveToMergeField(args.FieldName);
mBuilder.InsertHtml("" + args.FieldValue.ToString() + "");
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{
// Do nothing.
}
}
}
If you want to shrink font size automatically in any case, then please share your sample document and we will try to find a workaround for this feature.
Best Regards,