Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Feb 17, 2020 10:26 am

Hello!
I am trying to use the function ApplyCharacterFormat in a Field:

Field field = row.Cells [cellInfo.CurrentColumn] .Paragraphs [0] .AppendField (varName, FieldType.FieldDocProperty);
field.ApplyCharacterFormat (cellInfo.CellFormat);

where cellformat is a previously saved CharacterFormat.

The fact is that the resulting document does not apply the format to the naked eye but if I press Alt + F9 I see that it really has taken it.
I attach an example.

How can I apply the format really and what is shown in the document?

If I do this with a text it works perfectly:
TextRng textRng = row.Cells [cellInfo.CurrentColumn] .Paragraphs [0] .AppendText (strFormatValue);
textRng.ApplyCharacterFormat (cellInfo.CellFormat);
FieldFormat.PNG


Thank you very much!,

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Tue Feb 18, 2020 3:44 am

Hello,

Thanks for your inquiry.
The code "field.ApplyCharacterFormat (cellInfo.CellFormat); "you used can only set character format for the field code. To set format for the displayed text, you need to use the code "textRng.ApplyCharacterFormat (cellInfo.CellFormat);".
Our Spire.Doc is based on Microsoft Word. When setting format for the field code in Microsoft Word, such as changing the field code text color from black to red, the color of the displayed text would not be changed after pressing Alt + F9 to hide the field code. You could verify it on your side.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Feb 18, 2020 7:17 am

Hello!
And how can I format the text of a field?
I have a cell in a table. Then, inside, I create a property type FieldDocProperty, and I assign value to the variable of that property.
Now I want to format the text of the property. and since I have created a field and not a textRange, I assign the format to the field through the field.ApplyCharacterFormat function. How can I format the text of a field?
I have made a AppendField not an AppendText .
Can you help me please? Is it possible to have a code example that works? Mine does not work for me.

Thank you!

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Tue Feb 18, 2020 8:12 am

Hello,

Thanks for your prompt response.
Please refer to the below code to set format for the displayed text. If there is any other question, please provide your input file (if any) as well as your full code for further investigation.
Code: Select all
            Document doc = new Document();
            Spire.Doc.Table table = doc.AddSection().AddTable(true);
            table.ResetCells(3, 3);

            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "MyStyle";
            style.CharacterFormat.FontName = "Arial";
            style.CharacterFormat.TextColor = Color.Red;
            doc.Styles.Add(style);

            TableRow row = table.Rows[0];
            string fieldName = "test";
            Field field = row.Cells[1].AddParagraph().AppendField(fieldName, FieldType.FieldDocProperty);
            Paragraph para = field.OwnerParagraph;

            //Method 1:
            //para.ApplyStyle(style.Name);

            //Method 2:
            foreach (DocumentObject obj in para.ChildObjects)
            {
                if (obj.DocumentObjectType == DocumentObjectType.TextRange)
                {
                    TextRange textRng = obj as TextRange;
                    if (fieldName.Equals(textRng.Text))
                    {
                        textRng.ApplyCharacterFormat(style.CharacterFormat);
                    }
                }
            }

            doc.SaveToFile("result.docx", FileFormat.Docx);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Feb 18, 2020 8:41 am

Hello!
Thank you very much. It works perfectly!

Sincerely,

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Tue Feb 18, 2020 8:54 am

Hello,

Thanks for your feedback.
If you need any help in the future, please feel free to contact us.
Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc