Hello.
1) Is there a way to create my own style from code and apply it to selected text ?
2) When I open an existing doc file, is there a way to check what style is used in selected text ?
Best regards,
Greg
Document doc = new Document();
doc.LoadFromFile(@"..\..\Blank.docx", FileFormat.Docx);
Section section = doc.Sections[0];
Paragraph paragraph = section.AddParagraph();
TextRange txtRange = paragraph.AppendText("hello");
//Font name
txtRange.CharacterFormat.FontName = "Tahoma";
//Font size
txtRange.CharacterFormat.FontSize = 20;
//Underline
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;
//Change text color
txtRange.CharacterFormat.TextColor = Color.Blue;
doc.SaveToFile(@"Sample.docx", FileFormat.Docx);
System.Diagnostics.Process.Start(@"Sample.docx");
Document doc = new Document();
doc.LoadFromFile(@"..\..\test.doc", FileFormat.Doc);
for (int j = 0; j < doc.Sections.Count;j++)
{
for (int i = 0; i < doc.Sections[0].Paragraphs.Count; i++)
{
Console.WriteLine(doc.Sections[j].Paragraphs[i].StyleName);
}
}
Console.ReadKey();
foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
{
paragraph = section.AddParagraph();
//Append Text
paragraph.AppendText(builtinStyle.ToString());
//Apply Style
paragraph.ApplyStyle(builtinStyle);
}
Document doc = new Document();
doc.LoadFromFile(@"..\..\Blank.docx", FileFormat.Docx);
Section section = doc.Sections[0];
ParagraphStyle myStyle = new ParagraphStyle(doc);
myStyle.Name = "myStyle";
myStyle.ParagraphFormat.BackColor = Color.Yellow;
myStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
doc.Styles.Add(myStyle);
//use the style created
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Free Style");
paragraph.ApplyStyle(myStyle.Name);
doc.SaveToFile(@"Sample.docx", FileFormat.Docx);
System.Diagnostics.Process.Start(@"Sample.docx");