Free Support Forum - aspose.com

Can we add format in the MERGEFIELD exist in the word template while binding with datasource in C#?

Hi Aspose team,

We have a predefined templates in which we are keeping some MergeFields like { MERGEFIELD Date_LHD }. We want to traverse through the word template and find all the MERGEFIELD and if any MERGEFIELD is not having a format we can add during runtime and update the template.
How we can update MERGEFIELD format in the word template at runtime in c#.
Can you please provide the solution if possible with Aspose.word API?

@shobhit13,

You can update MERGEFIELD format in the word template at runtime by using the following C# code:

foreach(Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        FieldFormat ff = mf.Format;
        // code to set field format
    }
}

Thanks for quick response,

As per your above code, we can only set format for decimal and date type of fields. I didn’t find more option where we can set custom format for text fields for ssn masking and title case.
e.g. format like “Title_Case”, “@@@-@@-@@@@”,”(@@@) @@@-@@@@”, “@@@@”

Can you please provide the solution if possible with Aspose.word API for text format setup during runtime in C#?

I used below code for applying formatting, please confirm if we will have better approach apart from below code
foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldMergeField)
{
FieldMergeField mf = (FieldMergeField)field;
FieldFormat ff = mf.Format;
var tempfield = ((FieldMergeField)field).FieldName;
// code to set field format

    if (dataFields.ContainsKey(tempfield))
    {
        var tempfieldValue = dataFields[tempfield];

        if (CheckDecimal(tempfieldValue.Item2) 
              && !string.IsNullOrEmpty(tempfieldValue.Item3) 
              && !tempfieldValue.Item3.Contains("none"))
        {
            ff.NumericFormat = ff.NumericFormat ?? tempfieldValue.Item3;
        }
        else if (CheckDate(tempfieldValue.Item2) 
                   && !string.IsNullOrEmpty(tempfieldValue.Item3))
        {
            ff.DateTimeFormat = ff.DateTimeFormat ?? tempfieldValue.Item3;
        }
    }
}

}

@shobhit13,

Please ZIP and attach your input Word document and your expected document showing the desired output here for testing. Please create expected document by using MS Word. We will investigate the structure of expected document and provide you code to achieve the same by using Aspose.Words.

test-forms.zip (203.0 KB)
Hi Aspose team,

i have attached a zip file, which contains two files, one is for doc template with MergeFields and second one is output file. In the doc template we have mentioned format column where we have defined format which is returning from the database. At run time we are passing format in the Merge Value column if format does not present in the doc template. How can we apply Title_Case and @@@-@@-@@@@ formats in the MergeFields. I am using the above code for merging decimal and and date formats but not sure how we can passed other formats apart decimal and date formats.
Please let me know if we have any generic solution with Aspose api.

@shobhit13,

You can specify Title case format to merge field by using the following code:

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        FieldFormat ff = mf.Format;

        ff.GeneralFormats.Add(GeneralFormat.Caps);   
    }
}

Regarding @@@-@@-@@@@ format, can you please provide some example? Is it a decimal format or date format. Please provide example for both cases. Thanks for your cooperation.

Thanks for quick response!

Regarding @@@-@@-@@@@ format: this is a string format when we are passing ssn then ssn will be formatted in this format.e.g SSN= 765456342 and formatted after applying @@@-@@-@@@@ format text will looks like 765-45-6342.

what is the definition of below formats for GeneralFormat. Do we have any documentation ?

None = 0,
Aiueo = 1,
UppercaseAlphabetic = 2,
LowercaseAlphabetic = 3,
Arabic = 4,
ArabicAbjad = 5,
ArabicAlpha = 6,
ArabicDash = 7,
BahtText = 8,
CardText = 9,
ChineseNum1 = 10,
ChineseNum2 = 11,
ChineseNum3 = 12,
Chosung = 13,
CircleNum = 14,
DBChar = 15,
DBNum1 = 16,
DBNum2 = 17,
DBNum3 = 18,
DBNum4 = 19,
DollarText = 20,
Ganada = 21,
GB1 = 22,
GB2 = 23,
GB3 = 24,
GB4 = 25,
Hebrew1 = 26,
Hebrew2 = 27,
Hex = 28,
HindiArabic = 29,
HindiCardText = 30,
HindiLetter1 = 31,
HindiLetter2 = 32,
Iroha = 33,
KanjiNum1 = 34,
KanjiNum2 = 35,
KanjiNum3 = 36,
Ordinal = 37,
OrdText = 38,
UppercaseRoman = 39,
LowercaseRoman = 40,
SBChar = 41,
ThaiArabic = 42,
ThaiCardText = 43,
ThaiLetter = 44,
VietCardText = 45,
Zodiac1 = 46,
Zodiac2 = 47,
Zodiac3 = 48,
Caps = 49,
FirstCap = 50,
Lower = 51,
Upper = 52,
CharFormat = 53,
MergeFormat = 54,
MergeFormatInet = 55,

@shobhit13,

We think this is a number format. So, the following should work for you:

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        FieldFormat ff = mf.Format;

        ff.NumericFormat = "###-##-####";
        // Or 
        // ff.NumericFormat = "###'-'##'-'####";
        // the quotation marks here are required by MS Word to indicate that this is some static text rather than negative sign. 
    }
}

Secondly, you can find description of all GeneralFormat values in the following page:
GeneralFormat Enumeration

Hi Aspose team,

Thanks for quick response!

We are passing collection of object (Merge Field, Merge Value, Merge Format) to the MS word merge template and traversing MergeField from the doc template and assigning format values in the NumericFormat if Merge Value is Decimal else if assign format values in the DateTimeFormat if Merge Value is of type DateTime.

Is there any generic way where we can directly pass format in string type to Aspose API in the genric term like Title_case instead of Caps?
Format string
##.####
##.00
#,###.00
#,###.0000
$#,###.00
(@@@) @@@-@@@@
(none)
@@@@
@@@-@@-@@@@
MM/DD/YYYY
Title_Case

@shobhit13

I am afraid, you need to define mappings yourself. And the correct final format should be passed to FieldFormat object.