This short guide assists you in setting the paragraph formatting in Word using C#. It has the details to set the IDE, a list of steps, and a sample code for changing the MS Word paragraph formatting using C#. It will explain various properties and demonstrate the customization of the paragraph format, borders, and style.
Steps for Setting Paragraph Writing Format using C#
- Set the IDE to use Aspose.Words for .NET to format the text
- Create a new Word file using the Document class for setting paragraph format
- Create a DocumentBuilder class object using the above Word document
- Set indentation and alignment properties in the paragraph format
- Set paragraph border and font settings
- Add some text for checking the above paragraph formatting
- Save the new Word file with formatted text
These steps explain how to set Word document paragraph formatting using C#. Create a new Word document, attach a DocumentBuilder object, and access the ParagraphFormat property for setting alignment, left indent, right indent, and space after. Similarly, you may set the borders and font properties such as size, color, and Bold.
Code to Set Microsoft Word Paragraph Formatting using C#
using Aspose.Words; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
Document document = new Document(); | |
DocumentBuilder docBuilder = new DocumentBuilder(document); | |
// Set formatting | |
ParagraphFormat paragraphFormat = docBuilder.ParagraphFormat; | |
paragraphFormat.Alignment = ParagraphAlignment.Justify; | |
paragraphFormat.LeftIndent = 45; | |
paragraphFormat.RightIndent = 45; | |
paragraphFormat.SpaceAfter = 20; | |
paragraphFormat.Borders.Horizontal.LineStyle = LineStyle.Double; | |
paragraphFormat.Style.Font.Size = 12; | |
paragraphFormat.Style.Font.Color = System.Drawing.Color.Blue; | |
paragraphFormat.Style.Font.Bold = true; | |
// Output text | |
docBuilder.Writeln("Text formatting in a Word file allows users to enhance the appearance of their documents, making them more readable and visually appealing. Common formatting options include changing the font type and size, applying bold, italics, or underline for emphasis, and adjusting text alignment (left, center, right, or justified)."); | |
docBuilder.Writeln("You can also modify line spacing, add bullet points or numbered lists, and use indentation to organize content."); | |
document.Save("Output.docx"); | |
System.Console.WriteLine("Paragraph formatted in Word file"); | |
} | |
} |
This code segment demonstrates indenting paragraphs using C#. When we create the DocumentBuilder object for the document, we get access to all its properties, such as the ParagraphFormat. You may also set the page setup, alignment, line spacing, page breaks, shading, word wrapping, and even clear the formatting with one command if required.
This article has guided us in setting the formatting of a new or an existing Word file. If you want to remove the headers and footers of a Word file, refer to the article on Remove header and footer in Word using C#.