Need to change font color and font family for Autotext fields - Word document - C#

Hi Team,

We are trying to change the font color and font family for the auto text fields in word document. But we couldn’t achieve this. We are using Aspose.Words version 20.6.0.0.
We use the below .net code. Can you please help us on this asap?

							foreach(Field field in wordDoc.Range.Fields) // Load all fields
							{
								if(field.Type.Equals(Aspose.Words.Fields.FieldType.FieldAutoText)) // Retrieve only autotext fields
								{
									DocumentBuilder builder = new DocumentBuilder(doc); // Load the document
									FieldAutoText txt = (FieldAutoText)field; //Get the auto text
									
									if (txt.Result.Equals("Error! AutoText entry not defined."))
									{
										builder.MoveToField(txt, false);
										builder.Font.ClearFormatting();
										builder.Font.Color = Color.Blue;  //Change the color for autotext
									} 
								}
							}

@NanthiniSenthil123,

Please ZIP and attach the following resources here for testing:

  • Your simplified source Word document containing the AutoText fields
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same output by using Aspose.Words for .NET.

SampleSourceFile.zip (27.7 KB)

For autotext, we need to change the font color as Blue and font size as 18 and font family as Times new roman. Can you please help us?

@NanthiniSenthil123,

You can replace AutoText field in Word document with static formatted text by using the following code:

Document doc = new Document("C:\\temp\\SampleSourceFile\\SampleDoc.dot");

foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(FieldType.FieldAutoText))
    {
        if (((Aspose.Words.Fields.FieldAutoText)field).EntryName == "CusName")
        {
            DocumentBuilder builder = new DocumentBuilder(doc);

            if (field.Result.Equals("Error! AutoText entry not defined.")) //cntrl+f9 to see field code (CustomerName) in word doc
            {
                builder.MoveToField(field, false);

                builder.Font.ClearFormatting();
                builder.Font.Color = Color.Blue;
                builder.Font.Size = 18;
                builder.Font.Name = "Times New Roman (Headings CS)";

                builder.Write("Swetha");

                field.Remove();
                break;
            }
        }
    }
}

doc.Save("C:\\Temp\\SampleSourceFile\\21.3.docx");

Thank you. This helps. Is there any way to check whether the Autotext is in Header/Footer/MainBody?

Document doc = new Document(“C:\temp\SampleSourceFile\SampleDoc.dot”);

foreach (Field field in doc.Range.Fields)
{
if (field.Type.Equals(FieldType.FieldAutoText))
{
if (((Aspose.Words.Fields.FieldAutoText)field).EntryName == “CusName”) //How to check whether the cusName autotext is in Header or Body or Footer?
{
DocumentBuilder builder = new DocumentBuilder(doc);


        if (field.Result.Equals("Error! AutoText entry not defined.")) 
        {
          
        }
    }
}
}

@NanthiniSenthil123,

Please follow your other thread for further proceedings: