Working With Word Paragraphs Alignment
Paragrpah class under Openize.Words.IElements namespace in Openize.OpenXML-SDK for .NET exposes Alignment property which can be used to set or get alignment of word paragraphs. An enum ParagraphAlignment under Openize.Words.IElements provides all alignment options available to be used with Alignment property of Paragraph class. This structure lets the users conveniently work with word paragraph alignments in just a few lines of code.
Creating a Paragraph With Alignments
Please follow below simple steps:
- Initialize a new word document with the default template.
- Initialize the body with the new document.
- Create Normal Paragraph and add some run text.
- Set Alignment of the Paragraph.
- Create more paragraphs with different alignments.
- Append paragraph to the body of the document.
- Save the word document.
Code Example:
// Initialize a new word document with the default template
var doc = new Openize.Words.Document();
System.Console.WriteLine("Word Document with default template initialized");
// Initialize the body with the new document
var body = new Openize.Words.Body(doc);
System.Console.WriteLine("Body of the Word Document initialized");
// Add paragraph with left alignment
var para1 = new Openize.Words.IElements.Paragraph();
para1.AddRun(new Openize.Words.IElements.Run
{
Text = "First paragraph with left alignment"
});
// Setting the Paragraph Alignment to Center.
para1.Alignment = Openize.Words.IElements.ParagraphAlignment.Left; //"Left";
System.Console.WriteLine("Paragraph with left alignment created");
// Add paragraph with center alignment
var para2 = new Openize.Words.IElements.Paragraph();
para2.AddRun(new Openize.Words.IElements.Run
{
Text = "Second paragraph with center alignment"
});
System.Console.WriteLine("Paragraph with center alignment created");
// Setting the Paragraph Alignment to Center.
para2.Alignment = Openize.Words.IElements.ParagraphAlignment.Center; //"Center";
// Add paragraph with right alignment
var para3 = new Openize.Words.IElements.Paragraph();
para3.AddRun(new Openize.Words.IElements.Run
{
Text = "Third paragraph with right alignment"
});
System.Console.WriteLine("Paragraph with right alignment created");
// Setting the Paragraph Alignment to Center.
para3.Alignment = Openize.Words.IElements.ParagraphAlignment.Right; //"Right";
// Add paragraph with justify alignment
var para4 = new Openize.Words.IElements.Paragraph();
para4.AddRun(new Openize.Words.IElements.Run
{
Text = "Fourth paragraph with justify alignment"
});
System.Console.WriteLine("Paragraph with justify alignment created");
// Setting the Paragraph Alignment to Center.
para4.Alignment = Openize.Words.IElements.ParagraphAlignment.Justify; //"Justify";
// Append paragraphs to the document body
body.AppendChild(para1);
body.AppendChild(para2);
body.AppendChild(para3);
body.AppendChild(para4);
// Save the newly created Word Document.
doc.Save($"ParaAlignment.docx");
System.Console.WriteLine($"Word Document ParaAlignment.docx Created.");
Loading Word Paragraphs Alignment
Word paragraphs alignment can be loaded via Alignment property of the Paragraph object. Below are the steps to follow:
- Load the word document using
Documentclass underOpenize.Wordsnamespace. - Initialize the
BodywithDocumentobject. - Get the
Paragraphscollection associated with theBodyobject. - Traverse through
Paragraphs - Output
TextandAlignmentproperties of eachParagraph
Code Example
// Load the Word Document
var doc = new Openize.Words.Document($"ParaAlignment.docx");
// Initialize the body with the document
var body = new Openize.Words.Body(doc);
//System.Collections.Generic.List<Openize.Words.IElements.Paragraph>
// paragraphs = body.Paragraphs;
var paragraphs = body.Paragraphs;
foreach (Openize.Words.IElements.Paragraph paragraph in paragraphs)
{
System.Console.WriteLine($"Paragraph Text : {paragraph.Text}");
System.Console.WriteLine($"Paragraph Alignment : {paragraph.Alignment}");
}
Modifying Word Paragraphs Alignment
Steps to follow:
- Load the word document using
Documentclass underOpenize.Wordsnamespace. - Initialize the
BodywithDocumentobject. - Get the
Paragraphscollection associated with theBodyobject. - Traverse through
Paragraphs - Modify text and set
Alignmentof eachParagraphto Justify - Call
Updatemethod ofDocumentto reflect each paragraph change in the word document structure. - Call
Savemethod ofDocumentto save modified word document to disk or stream.
Code Example
// Load the Word Document
var doc = new Openize.Words.Document($"ParaAlignment.docx");
// Initialize the body with the document
var body = new Openize.Words.Body(doc);
//foreach (Openize.Words.IElements.Paragraph paragraph in body.Paragraphs)
foreach (var paragraph in body.Paragraphs)
{
paragraph.AddRun(new Openize.Words.IElements.Run
{ Text = " (alignment modified to justify)", Italic = true });
paragraph.Alignment = Openize.Words.IElements.ParagraphAlignment.Justify; //"Justify";
doc.Update(paragraph);
}
System.Console.WriteLine("Alignment of all paragraphs changed to Justify");
// Save the modified Word Document
doc.Save($"ParaAlignedModified.docx");
System.Console.WriteLine($"Word Document ParaAligned.docx Modified and Saved " +
$"As ParaAlignedModified.docx.");