How to Align Text in Word with C#/VB.NET?
Word Text Alignment contains four styles: align
left, align center, align right, and justify. All these kinds are frequently
used when we set our word text styles. Alignment allows you to make your
document pages look neat and tidy. Thus,
text alignment can be indispensable for word users. In this article, you will
learned how to align text in Word with
C#/VB.NET. Before I show the method to alignment text in word with C#,
VB.NET. I suggest use a word component Free
Spire.Doc for .NET to help me finish this task. Read on for more information.
Programming
Environment
In this test, Free Spire.Doc for .NET is
introduced into the program. The Spire.Doc.dll file can be referenced by:
Method 1: Download Free Spire.Doc for
.NET locally, unzip it, and install it. After the installation is
complete, find Spire.Doc.dll in the BIN folder under the installation path.
Then open the "Solution Explorer" in Visual Studio, right-click
"References", "Add Reference", and add a reference to the
dll file in the BIN folder of the local path to the program.
Method 2: Install via NuGet.
It can be installed by the following 2 methods:
(1) You can open the "Solution
Explorer" in Visual Studio, right-click "References",
"Manage NuGet Packages", then search for "Free Spire.Doc",
and click "Install". Wait for the program installation to complete.
(2) Copy the following content to the PM
console installation.
Install-Package FreeSpire.Doc -Version 10.8.0
Specific
Steps:
- 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#】
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("TEST.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);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace AlignText
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create
a Document instance
Dim doc As Document = New Document()
'Load
a sample Word document
doc.LoadFromFile("TEST.docx")
'Get
the first section
Dim section As Section = doc.Sections(0)
'Get
the first paragraph and make it center-aligned
Dim p As Paragraph = section.Paragraphs(0)
p.Format.HorizontalAlignment =
HorizontalAlignment.Center
'Get
the second paragraph and make it left-aligned
Dim p1 As Paragraph = section.Paragraphs(1)
p1.Format.HorizontalAlignment =
HorizontalAlignment.Left
'Get
the third paragraph and make it right-aligned
Dim p2 As Paragraph = section.Paragraphs(2)
p2.Format.HorizontalAlignment = HorizontalAlignment.Right
'Get
the fourth paragraph and make it justified
Dim p3 As Paragraph = section.Paragraphs(3)
p3.Format.HorizontalAlignment =
HorizontalAlignment.Justify
'Save
the document
doc.SaveToFile("WordAlignment.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
Effective Shot
Conclusion:
In this post,
you have learned how to align text in Word in C#/VB.NET. Not only that, we also
have other functions, such as, C#/VB.NET:
Remove Paragraphs in a Word Document, C#/VB.NET:
Remove Empty Lines in Word and so on. Apart from
that, if you'd like to learn more, you can visit this
link to explore more about for Spire.Doc for .NET.
Comments
Post a Comment