I want to loop through all sections, all characters in a document, and set the font to them.
When it's English, I want to process it using font A,
when it's numbers, I want to process it using font B,
and when it's another language, I want to process it using font C.
The problem is that when accessing TextRange, I can only specify the font for the entire TextRange,
and I don't know how to specify the font for each Char within the TextRange. How should I handle this?
- Code: Select all
private void SetFontsForNumberAndEnglish(Document doc) {
foreach (Section section in doc.Sections) {
foreach (Paragraph para in section.Paragraphs) {
SetFontsForParagraph(para);
}
foreach (Table table in section.Tables) {
foreach (TableRow row in table.Rows) {
foreach (TableCell cell in row.Cells) {
foreach (Paragraph para in cell.Paragraphs) {
SetFontsForParagraph(para);
}
}
}
}
}
}
private void SetFontsForParagraph(Paragraph para) {
foreach(DocumentObject item in para.ChildObjects) {
if(item.DocumentObjectType == DocumentObjectType.TextRange) {
TextRange range = item as TextRange;
if (range.Text != null) {
for (int i = 0; i < range.Text.Length; i++) {
char c = range.Text[i];
//How to access char font
if (IsNumber(c)) {
range.CharacterFormat.FontName = "Helvetica";
} else if (IsEnglish2(c)) {
range.CharacterFormat.FontName = "Arial";
}else ...
}
}
}
}
}