officefileapi-116832-word-processing-document-api-examples-styles-how-to-create-new-linked-style.md
This example explains how to create a linked style – a style that can be applied both to the character and paragraph, depending on the range to which it is applied.
To create such a style, perform the following steps:
Note
All the actions should be performed using the SubDocument.BeginUpdate - SubDocument.EndUpdate paired methods. Otherwise, flickering might occur when starting the application.
Document document = server.Document;
document.BeginUpdate();
document.AppendText("Line One\nLine Two\nLine Three");
document.EndUpdate();
ParagraphStyle lstyle = document.ParagraphStyles["MyLinkedStyle"];
if (lstyle == null)
{
document.BeginUpdate();
lstyle = document.ParagraphStyles.CreateNew();
lstyle.Name = "MyLinkedStyle";
lstyle.LineSpacingType = ParagraphLineSpacing.Double;
lstyle.Alignment = ParagraphAlignment.Center;
document.ParagraphStyles.Add(lstyle);
CharacterStyle lcstyle = document.CharacterStyles.CreateNew();
lcstyle.Name = "MyLinkedCStyle";
document.CharacterStyles.Add(lcstyle);
lcstyle.LinkedStyle = lstyle;
lcstyle.ForeColor = System.Drawing.Color.DarkGreen;
lcstyle.Strikeout = StrikeoutType.Single;
lcstyle.FontSize = 24;
document.EndUpdate();
document.SaveDocument("LinkedStyleSample.docx", DevExpress.XtraRichEdit.DocumentFormat.Docx);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "LinkedStyleSample.docx");
}
Dim document As Document = server.Document
document.BeginUpdate()
document.AppendText("Line One" & vbLf & "Line Two" & vbLf & "Line Three")
document.EndUpdate()
Dim lstyle As ParagraphStyle = document.ParagraphStyles("MyLinkedStyle")
If lstyle Is Nothing Then
document.BeginUpdate()
lstyle = document.ParagraphStyles.CreateNew()
lstyle.Name = "MyLinkedStyle"
lstyle.LineSpacingType = ParagraphLineSpacing.Double
lstyle.Alignment = ParagraphAlignment.Center
document.ParagraphStyles.Add(lstyle)
Dim lcstyle As CharacterStyle = document.CharacterStyles.CreateNew()
lcstyle.Name = "MyLinkedCStyle"
document.CharacterStyles.Add(lcstyle)
lcstyle.LinkedStyle = lstyle
lcstyle.ForeColor = System.Drawing.Color.DarkGreen
lcstyle.Strikeout = StrikeoutType.Single
lcstyle.FontSize = 24
document.EndUpdate()
document.SaveDocument("LinkedStyleSample.docx", DevExpress.XtraRichEdit.DocumentFormat.Docx)
System.Diagnostics.Process.Start("explorer.exe", "/select," & "LinkedStyleSample.docx")
End If