Document styles are the pre-defined set of formatting instructions which can be re-used any number of times in a document. For instance, a document style once defined for a list can be used to represent all other similar lists in the document.
GcWord provides you the ability to style a Word document using built-in styles. It offers different style types through BuiltInStyleId enumeration which can be applied to the corresponding content type. In addition to the built-in styles, GcWord also allows you to define styles on your own using the Style class. You can add a defined style to the style collection using Add method of the StyleCollection class and the style collection can be accessed using Styles property of the GcWordDocument class.
To create new style for a Word document:
C# |
Copy Code |
---|---|
doc.Load("SampleDoc.docx"); //Access the first paragraph Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.First; //Access the text in the first run Run run = p.GetRange().Runs.First; // Create a new char style "style1" for the first half Style style1 = doc.Styles.Add("Style1", StyleType.Character); style1.Font.Name = "Times New Roman"; style1.Font.Size = 16; style1.Font.Bold = true; style1.Font.Italic = true; style1.Font.Color.RGB = Color.Blue; style1.Font.Underline = Underline.Thick; //Apply style to the text run run.Style = style1; //Save the document doc.Save("StyleAdded.docx"); |
To modify document style:
C# |
Copy Code |
---|---|
doc.Load("StyleAdded.docx"); //Access the first paragraph Paragraph para1 = doc.Body.Sections.First.GetRange().Paragraphs.First; //Access the first run Run run = para1.GetRange().Runs.First; //Modify the existing style's font name and color Style s1 = run.Style; s1.Font.Color.RGB = Color.Brown; s1.Font.Name = "Arial"; //Apply style to the run run.Style = s1; //Save the document doc.Save("ModifyStyles.docx"); |
To delete the style applied on a Word document:
C# |
Copy Code |
---|---|
doc.Load("StyleAdded.docx"); //Access the first paragraph Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.First; //Access the text in the first run Run run = p.GetRange().Runs.First; //Delete the style applied on the first half run.Style.Delete(); //Save the document doc.Save("StyleDeleted.docx"); |
For more information on how to apply different document styles using GcWord, see GcWord sample browser.