Hello:
For a long paragraph of text, how to make it Times New Roman in English and Bold in Chinese?
And,The first line is indented to 2 characters,Unit is not centimeter.How to set it?
Document document = new Document();
document.LoadFromFile("style.docx");
//Create a new style and set font
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "Mystyle";
style.CharacterFormat.FontName = "Times New Roman";
document.Styles.Add(style);
//Apply style
foreach (Section s in document.Sections)
{
foreach (Paragraph p in s.Paragraphs)
{
p.ApplyStyle(style.Name);
}
}
//Find all Chinese character
Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
TextSelection[] textSelections = document.FindAllPattern(regex);
foreach (TextSelection selection in textSelections)
{
TextRange range = selection.GetAsOneRange();
//Set bold
range.CharacterFormat.Bold = true;
}
string filePath = "result.docx";
document.SaveToFile(filePath, FileFormat.Docx);
Document document = new Document();
Section sec = document.AddSection();
Paragraph paragraph = sec.AddParagraph();
TextRange tr = paragraph.AppendText("Sets the value that represents the first line indent for paragraph.");
//Get the font size
int size = (int)tr.CharacterFormat.FontSize;
//Set the first line indent to 2 characters
paragraph.Format.SetFirstLineIndent(2 * size);
string filePath = "result.docx";
document.SaveToFile(filePath, FileFormat.Docx);
//load document
Document document = new Document();
document.LoadFromFile("test.docx");
//get the second section
Section section = document.Sections[1];
//find all Chinese character
Regex regex = new Regex(@"[\u4e00-\u9fa5]+");
TextSelection[] textSelections = document.FindAllPattern(regex);
//Bold the specified section
foreach (TextSelection selection in textSelections)
{
TextRange range = selection.GetAsOneRange();
Paragraph para = range.OwnerParagraph;
Section findSection = para.Owner.Owner as Section;
//Determine if it is specified section
if (findSection == section)
{
range.CharacterFormat.Bold = true;
}
}
string filePath = "result.docx";
document.SaveToFile(filePath, FileFormat.Docx);