Created
July 9, 2024 17:57
C# Create Multiple Frame Paragraphs in Word Documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prerequisite: Install <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>. | |
// Initialize a new word document with the default template | |
var doc = new FileFormat.Words.Document(); | |
System.Console.WriteLine("Word Document with default template initialized"); | |
// Initialize the body with the new document | |
var body = new FileFormat.Words.Body(doc); | |
System.Console.WriteLine("Body of the Word Document initialized"); | |
// Create first framed paragraph | |
var para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = "This paragraph should have single width frames with blue color" }); | |
para.ParagraphBorder.Width = FileFormat.Words.IElements.BorderWidth.Single; | |
para.ParagraphBorder.Color = FileFormat.Words.IElements.Colors.Blue; | |
para.ParagraphBorder.Size = 4; | |
System.Console.WriteLine("Single width blue color frame paragraph created"); | |
body.AppendChild(para); | |
System.Console.WriteLine($"Single width blue color frame paragraph appended to word document Body"); | |
// Create second framed paragraph | |
para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = "This paragraph should have double width frames with red color" }); | |
para.ParagraphBorder.Width = FileFormat.Words.IElements.BorderWidth.Double; | |
para.ParagraphBorder.Color = FileFormat.Words.IElements.Colors.Red; | |
para.ParagraphBorder.Size = 4; | |
System.Console.WriteLine("Double width red color frame paragraph created"); | |
body.AppendChild(para); | |
System.Console.WriteLine($"Double width red color frame paragraph appended to word document Body"); | |
// Create third framed paragraph | |
para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = "This paragraph should have dotted width frames with purple color" }); | |
para.ParagraphBorder.Width = FileFormat.Words.IElements.BorderWidth.Dotted; | |
para.ParagraphBorder.Color = FileFormat.Words.IElements.Colors.Purple; | |
para.ParagraphBorder.Size = 4; | |
System.Console.WriteLine("Dotted width purple color frame paragraph created"); | |
body.AppendChild(para); | |
System.Console.WriteLine($"Dotted width purple color frame paragraph appended to word document Body"); | |
// Create fourth framed paragraph | |
para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = "This paragraph should have dotdash width frames with navy color" }); | |
para.ParagraphBorder.Width = FileFormat.Words.IElements.BorderWidth.DotDash; | |
para.ParagraphBorder.Color = FileFormat.Words.IElements.Colors.Navy; | |
para.ParagraphBorder.Size = 4; | |
System.Console.WriteLine("Dotdash width navy color frame paragraph created"); | |
body.AppendChild(para); | |
System.Console.WriteLine($"Dotdash width navy color frame paragraph appended to word document Body"); | |
// Create normal paragraph | |
para = new FileFormat.Words.IElements.Paragraph(); | |
para.AddRun(new FileFormat.Words.IElements.Run { Text = "This paragraph should have no frames/borders" }); | |
System.Console.WriteLine("Normal paragraph without frames/borders created"); | |
body.AppendChild(para); | |
System.Console.WriteLine($"Normal paragraph without frames/borders appended to word document Body"); | |
// Save the newly created Word Document. | |
doc.Save($"WordParagraphsFrame.docx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment