- Demo
- C# source
- VB.Net source
The sample demonstrates how to set text style in Word document.
private void button1_Click(object sender, EventArgs e) { //Create word document Document document = new Document(); //Create a new secition Section section = document.AddSection(); //Create a new paragraph Paragraph paragraph = section.AddParagraph(); //Append Text paragraph.AppendText("The various ways to format paragraph text in Microsoft Word:"); paragraph.ApplyStyle(BuiltinStyle.Heading1); //Append alignment text AppendAligmentText(section); //Append indentation text AppendIndentationText(section); AppendBulletedList(section); //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc); //Launching the MS Word file. WordDocViewer("Sample.doc"); } private void AppendAligmentText(Section section) { Paragraph paragraph = null; paragraph = section.AddParagraph(); //Append Text paragraph.AppendText("Horizontal Aligenment"); paragraph.ApplyStyle(BuiltinStyle.Heading3); foreach (Spire.Doc.Documents.HorizontalAlignment align in Enum.GetValues(typeof(Spire.Doc.Documents.HorizontalAlignment))) { Paragraph paramgraph = section.AddParagraph(); paramgraph.AppendText("This text is " + align.ToString()); paramgraph.Format.HorizontalAlignment = align; } } private void AppendIndentationText(Section section) { Paragraph paragraph = null; paragraph = section.AddParagraph(); //Append Text paragraph.AppendText("Indentation"); paragraph.ApplyStyle(BuiltinStyle.Heading3); paragraph = section.AddParagraph(); paragraph.AppendText("Indentation is the spacing between text and margins. Word allows you to set left and right margins, as well as indentations for the first line of a paragraph and hanging indents"); paragraph.Format.FirstLineIndent = 15; } private void AppendBulletedList(Section section) { Paragraph paragraph = null; paragraph = section.AddParagraph(); //Append Text paragraph.AppendText("Bulleted List"); paragraph.ApplyStyle(BuiltinStyle.Heading3); paragraph = section.AddParagraph(); for (int i = 0; i < 5; i++) { paragraph = section.AddParagraph(); paragraph.AppendText("Item" + i.ToString()); if (i == 0) { paragraph.ListFormat.ApplyBulletStyle(); } else { paragraph.ListFormat.ContinueListNumbering(); } paragraph.ListFormat.ListLevelNumber = 1; } } private void WordDocViewer(string fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } }
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click 'Create word document Dim document_Renamed As New Document() 'Create a new secition Dim section_Renamed As Section = document_Renamed.AddSection() 'Create a new paragraph Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph() 'Append Text paragraph_Renamed.AppendText("The various ways to format paragraph text in Microsoft Word:") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading1) 'Append alignment text AppendAligmentText(section_Renamed) 'Append indentation text AppendIndentationText(section_Renamed) AppendBulletedList(section_Renamed) 'Save doc file. document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc) 'Launching the MS Word file. WordDocViewer("Sample.doc") End Sub Private Sub AppendAligmentText(ByVal section_Renamed As Section) Dim paragraph_Renamed As Paragraph = Nothing paragraph_Renamed = section_Renamed.AddParagraph() 'Append Text paragraph_Renamed.AppendText("Horizontal Aligenment") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading3) For Each align As Spire.Doc.Documents.HorizontalAlignment In System.Enum.GetValues(GetType(Spire.Doc.Documents.HorizontalAlignment)) Dim paramgraph As Paragraph = section_Renamed.AddParagraph() paramgraph.AppendText("This text is " & align.ToString()) paramgraph.Format.HorizontalAlignment = align Next align End Sub Private Sub AppendIndentationText(ByVal section_Renamed As Section) Dim paragraph_Renamed As Paragraph = Nothing paragraph_Renamed = section_Renamed.AddParagraph() 'Append Text paragraph_Renamed.AppendText("Indentation") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading3) paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendText("Indentation is the spacing between text and margins. Word allows you to set left and right margins, as well as indentations for the first line of a paragraph and hanging indents") paragraph_Renamed.Format.FirstLineIndent = 15 End Sub Private Sub AppendBulletedList(ByVal section_Renamed As Section) Dim paragraph_Renamed As Paragraph = Nothing paragraph_Renamed = section_Renamed.AddParagraph() 'Append Text paragraph_Renamed.AppendText("Bulleted List") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading3) paragraph_Renamed = section_Renamed.AddParagraph() For i As Integer = 0 To 4 paragraph_Renamed = section_Renamed.AddParagraph() paragraph_Renamed.AppendText("Item" & i.ToString()) If i = 0 Then paragraph_Renamed.ListFormat.ApplyBulletStyle() Else paragraph_Renamed.ListFormat.ContinueListNumbering() End If paragraph_Renamed.ListFormat.ListLevelNumber = 1 Next i End Sub Private Sub WordDocViewer(ByVal fileName As String) Try Process.Start(fileName) Catch End Try End Sub