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.
Fri Jun 21, 2024 10:30 am
hi,
in "TableCell" with "CellFormat" propriety, i can change background color.
but how can i change my font color?
- Code: Select all
cell.CellFormat.BackColor = System.Drawing.Color.Blue;
-
emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Mon Jun 24, 2024 2:13 am
Hello,
Thanks for your inquiry.
Please refer to the following code to set the font color of text in cells. If you have any other questions, please feel free to write back.
- Code: Select all
// Create a Document object
Document doc = new Document();
// Add a section
Section sec = doc.AddSection();
// Add a table
Table table = sec.AddTable(true);
table.ResetCells(3, 3);
// Get first row as head
TableRow head = table.Rows[0];
// Add content
Paragraph paragraph = head.Cells[0].AddParagraph();
TextRange textRange = paragraph.AppendText("hello");
// Set font color
textRange.CharacterFormat.TextColor = Color.Red;
doc.SaveToFile("res.docx",FileFormat.Docx2016);
Sincerely,
William
E-iceblue support team
-
William.Zhang
-
- Posts: 451
- Joined: Mon Dec 27, 2021 2:23 am
Mon Jun 24, 2024 8:41 am
hi,
thank you for reply.
if a have already a table with some rows, how can i get or change text color from existing cells?
-
emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Mon Jun 24, 2024 9:25 am
Hello,
Thanks for your reply.
Please refer to the modified code below for testing.
- Code: Select all
Document document = new Document();
document.LoadFromFile("test.docx");
Spire.Doc.Interface.ITable table = document.Sections[0].Tables[0];
Paragraph paragraph = table.Rows[0].Cells[0].Paragraphs[0];
foreach (DocumentObject item in paragraph.ChildObjects)
{
if(item is TextRange)
{
TextRange range = (TextRange)item;
range.CharacterFormat.TextColor = Color.Red;
}
}
document.SaveToFile("res.docx", Spire.Doc.FileFormat.Docx2016);
Sincerely,
William
E-iceblue support team
-
William.Zhang
-
- Posts: 451
- Joined: Mon Dec 27, 2021 2:23 am
Wed Jun 26, 2024 12:10 pm
thank you,
my mistake was to change text directly in paragraph like
- Code: Select all
cell.Paragraphs[i].Text="text"
.
that lose all styles properties
-
emamanu_1987
-
- Posts: 14
- Joined: Fri Jun 21, 2024 10:23 am
Thu Jun 27, 2024 3:00 am
Hello,
Thanks for your reply.
Is your problem solved? If so, I will close this case. Looking forward to your reply.
Sincerely,
William
E-iceblue support team
-
William.Zhang
-
- Posts: 451
- Joined: Mon Dec 27, 2021 2:23 am