With the help of Spire.Doc for .NET, developers can easily set bullet style for the existing word documents via invoke p.ListFormat.ApplyBulletStyle() method to format. This article will show you how to convert HTML list into word and set the bullet style for the word list in C#.

Firstly, please view the effective screenshot for the result word document:

How to set word bullet style by appending the HTML code in C#

Here comes to the steps:

Step 1: Create a new document and add a section to the document.

Document document = new Document();
Section section=document.AddSection();

Step 2: Add word list to the paragraph by appending HTML.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendHTML("<ol><li>Version 1</li><li>Version 2</li><li>Version 3</li></ol>");

Step 3: Set the bullet style for the paragraph.

foreach (Paragraph p in section.Paragraphs)
{
 p.ApplyStyle(BuiltinStyle.Heading2);
 p.ListFormat.CurrentListLevel.NumberPosition = 20;
 p.ListFormat.CurrentListLevel.TextPosition = 30;
 }

Step 4: Save the document to file

document.SaveToFile("result.docx",FileFormat.Docx);

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
namespace SetWordBullet
{
 class Program
    {
     
      static void Main(string[] args)
        {
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendHTML("
  1. Version 1
  2. Version 2
  3. Version 3
"); foreach (Paragraph p in section.Paragraphs) { p.ApplyStyle(BuiltinStyle.Heading2); p.ListFormat.CurrentListLevel.NumberPosition = 20; p.ListFormat.CurrentListLevel.TextPosition = 30; } document.SaveToFile("result.docx", FileFormat.Docx); } } }
Published in Paragraph

Line spacing is the amount of white space between each line in a paragraph, while paragraph spacing is the amount of white space before and after each paragraph in a document. In MS Word, you can adjust the spacing manually if the default spacing does not meet your needs. In this article, you will learn how to programmatically set line spacing and paragraph spacing in Word using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Set Line Spacing and Paragraph Spacing in Word

Loose line or paragraph spacing can make text more readable, while tight line or paragraph spacing can fit more text in a document. Below are the steps to adjust the line spacing and paragraph spacing in a Word document.

  • Create a Document instance.
  • Add a section to the document using Document.AddSection() method, and then add a paragraph to the section.
  • Set the before and after spacing for the paragraph using Paragraph.Format.BeforeSpacing and Paragraph.Format.AfterSpacing properties.
  • Add another paragraph and set line spacing in the paragraph using Paragraph.Format.LineSpacing property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace SetSpacing
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();

            //Add a paragraph
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Spire.Doc for .NET is a professional Word .NET library specifically designed for developers to " +
                "create, read, write, convert, compare and print Word documents on any .NET platform " +
                "(Target .NET Framework, .NET Core, .NET Standard, .NET 5.0, .NET 6.0, Xamarin & Mono Android ) with fast and high quality performance.");

            //Set spacing before the paragraph
            paragraph.Format.BeforeSpacing = 30;

            //Set spacing after the paragraph
            paragraph.Format.AfterSpacing = 30;

            //Add another paragraph
            Paragraph paragraph2 = section.AddParagraph();
            paragraph2.AppendText("Spire.Doc for .NET is a reliable API which enables to perform many Word document processing tasks. " +
                "It supports C#, VB.NET, ASP.NET and ASP.NET MVC. Spire.Doc supports Word 97-2003 /2007/2010/2013/2016/2019 " +
                "and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, HTML and vice versa. ");

            //Set line spacing in the paragraph
            paragraph2.Format.LineSpacing = 25;

            //Save the result document
            document.SaveToFile("SetSpacing.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Set Line Spacing and Paragraph Spacing in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Paragraph

Word Heading can be taken as title of each part in Word document. This guide demonstrates solution to manage these Word headings and form them as a catalogue in C# and VB.NET.

In Word document, users can set some contents, for example, phrases of summary of the following paragraphs, as headings. These Word headings can be displayed in the first page of whole document and form as catalogue after arrangement so that readers can get outline of whole document. Solution in this guide presents how to manage Word headings with numbered style in a new created document in C# and VB.NET via Spire.Doc for .NET and screenshot below shows results of managing Word headings.

Word Paragraph Headings

Heading is one of kind of styles for paragraphs and Spire.Doc for .NET provides several BuiltinStyle types for paragraphs. In this example, three BuiltinStyle types will be applied: Heading1, Heading2 and Heading3. Following, details will be presented step by step.

Firstly, initialize a Document instance and add section, paragraph for this instance. Secondly, invoke AppendText method with parameter string text and ApplyStyle(BuiltinStyle.Heading1) method of Paragraph class to add text and set heading style for new added paragraph. Then, invoke ListFromat.ApplyNumberedStyle() method of Paragraph class to set number list for it. Thirdly, add new paragraph and set Heading2 style for it as the previous step. Initialize a ListStyle instance for document with Numbered ListType. Then, initialize a ListLevel instance from ListStyle and set UsePrevLevelPattern and NumberPrefix properties for this instance. After that, invoke ListStyleCollection.Add(ListStyle) method of Document class and ListFormat.ApplyStyle method of Paragraph class with parameter string styleName to add ListStyle for Heading2. Fourthly, add a new paragraph and set BuiltinStyle as Heading3 and apply ListStyle for this paragraph as the previous step. Finally, invoke SaveToFile method of Document class with parameter string fileName and FileFormat to save this document. Refer the code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordHeading
{
    class Heading
    {
        static void Main(string[] args)
        {
            //Create Document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

            //Add Heading 1
            paragraph = section.AddParagraph();
            paragraph.AppendText(BuiltinStyle.Heading1.ToString());
            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ListFormat.ApplyNumberedStyle();

            //Add Heading 2
            paragraph = section.AddParagraph();
            paragraph.AppendText(BuiltinStyle.Heading2.ToString());
            paragraph.ApplyStyle(BuiltinStyle.Heading2);

            //List Style for Headings 2
            ListStyle listSty2 = new ListStyle(document, ListType.Numbered);
            foreach (ListLevel listLev in listSty2.Levels)
            {
                listLev.UsePrevLevelPattern = true;
                listLev.NumberPrefix = "1.";
            }
            listSty2.Name = "MyStyle2";
            document.ListStyles.Add(listSty2);
            paragraph.ListFormat.ApplyStyle(listSty2.Name);
            
            //Add List Style 3
            ListStyle listSty3 = new ListStyle(document, ListType.Numbered);
            foreach (ListLevel listLev in listSty3.Levels)
            {
                listLev.UsePrevLevelPattern = true;
                listLev.NumberPrefix = "1.1.";
            }
            listSty3.Name = "MyStyle3";
            document.ListStyles.Add(listSty3);

            //Add Heading 3
            for (int i = 0; i < 4; i++)
            {
                paragraph = section.AddParagraph();

                //Append Text
                paragraph.AppendText(BuiltinStyle.Heading3.ToString());

                //Apply List Style 3 for Heading 3
                paragraph.ApplyStyle(BuiltinStyle.Heading3);
                paragraph.ListFormat.ApplyStyle(listSty3.Name);
            }

            //Save and Launch
            document.SaveToFile("Word Headings.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Word Headings.docx");   
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordHeading
    Friend Class Heading
        Shared Sub Main(ByVal args() As String)
            'Create Document
            Dim document As New Document()
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = If(section.Paragraphs.Count > 0, section.Paragraphs(0), section.AddParagraph())

            'Add Heading 1
            paragraph = section.AddParagraph()
            paragraph.AppendText(BuiltinStyle.Heading1.ToString())
            paragraph.ApplyStyle(BuiltinStyle.Heading1)
            paragraph.ListFormat.ApplyNumberedStyle()

            'Add Heading 2
            paragraph = section.AddParagraph()
            paragraph.AppendText(BuiltinStyle.Heading2.ToString())
            paragraph.ApplyStyle(BuiltinStyle.Heading2)

            'List Style for Headings 2
            Dim listSty2 As New ListStyle(document, ListType.Numbered)
            For Each listLev As ListLevel In listSty2.Levels
                listLev.UsePrevLevelPattern = True
                listLev.NumberPrefix = "1."
            Next listLev
            listSty2.Name = "MyStyle2"
            document.ListStyles.Add(listSty2)
            paragraph.ListFormat.ApplyStyle(listSty2.Name)

            'Add List Style 3
            Dim listSty3 As New ListStyle(document, ListType.Numbered)
            For Each listLev As ListLevel In listSty3.Levels
                listLev.UsePrevLevelPattern = True
                listLev.NumberPrefix = "1.1."
            Next listLev
            listSty3.Name = "MyStyle3"
            document.ListStyles.Add(listSty3)

            'Add Heading 3
            For i As Integer = 0 To 3
                paragraph = section.AddParagraph()

                'Append Text
                paragraph.AppendText(BuiltinStyle.Heading3.ToString())

                'Apply List Style 3 for Heading 3
                paragraph.ApplyStyle(BuiltinStyle.Heading3)
                paragraph.ListFormat.ApplyStyle(listSty3.Name)
            Next i

            'Save and Launch
            document.SaveToFile("Word Headings.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Word Headings.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, as professional Word component, is very powerful on fast generating, loading, writing, modifying and saving Word documents in .NET, WPF, Silverlight without Word automation and any third party tools.

Published in Paragraph
Thursday, 07 April 2022 06:38

C#/VB.NET: Set Paragraph Indents in Word

In Word documents, indentation is a paragraph format used to adjust the distance between paragraph body and page margin. It includes left indent, right indent, first line indent and hanging indent. Left indent and right indent can be applied to all lines of a paragraph, while first line indent can only be applied to first line of a paragraph. As for the hanging indent, it can be applied to every line of the paragraph except the first one. This article introduces how to programmatically set paragraph indents in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Set Paragraph Indents in Word

The table below lists some of the core classes and properties that are used to set different paragraph indents in a Word document.

Name Description
ParagraphFormat Class Represents the format of a paragraph.
ParagraphFormat.LeftIndent Property Returns or sets the value that represents the left indent for paragraph.
ParagraphFormat.RightIndent Property Returns or sets the value that represents the right indent for paragraph.
ParagraphFormat.FirstLineIndent Property Gets or sets the value for first line or hanging indent.  Positive value represents first-line indent, and Negative value represents hanging indent.

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph using Section.Paragraphs[] property.
  • Get the paragraph format using Paragraph.Format property, and then set the paragraph indent using the above listed properties of ParagraphFormat class.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordIndent
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile("sample.docx");

            //Get the first paragraph and set left indent
            Paragraph para1 = doc.Sections[0].Paragraphs[0];
            para1.Format.LeftIndent = 30;

            //Get the second paragraph and set right indent
            Paragraph para2 = doc.Sections[0].Paragraphs[1];
            para2.Format.RightIndent = 30;

            //Get the third paragraph and set first line indent
            Paragraph para3 = doc.Sections[0].Paragraphs[2];
            para3.Format.FirstLineIndent = 30;

            //Get the fourth paragraph and set hanging indent
            Paragraph para4 = doc.Sections[0].Paragraphs[3];
            para4.Format.FirstLineIndent = -30;

            //Save the document to file
            doc.SaveToFile("Indent.docx", FileFormat.Docx2010);
        }
    }
}

C#/VB.NET: Set Paragraph Indents in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Paragraph
Thursday, 14 April 2022 06:38

C#/VB.NET: Align Text in Word

Text alignment is a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. There are four types of text alignments available in Microsoft Word: left-aligned, center-aligned, right-aligned, and justified. In this article, you will learn how to programmatically set different text alignments for paragraphs in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Align Text in Word

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph using Section.Paragraphs[] property.
  • Get the paragraph format using Paragraph.Format property
  • Set text alignment for the specified paragraph using ParagraphFormat.HorizontalAlignment property.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace AlignText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile(@"D:\Files\sample.docx");

            //Get the first section
            Section section = doc.Sections[0];

            //Get the first paragraph and make it center-aligned
            Paragraph p = section.Paragraphs[0];
            p.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Get the second paragraph and make it left-aligned
            Paragraph p1 = section.Paragraphs[1];
            p1.Format.HorizontalAlignment = HorizontalAlignment.Left;

            //Get the third paragraph and make it right-aligned
            Paragraph p2 = section.Paragraphs[2];
            p2.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Get the fourth paragraph and make it justified
            Paragraph p3 = section.Paragraphs[3];
            p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;

            //Save the document
            doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Align Text in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Paragraph
Page 2 of 2