Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save openize-words-gists/69335fe2b99e26584f57eef1b9c58468 to your computer and use it in GitHub Desktop.
Save openize-words-gists/69335fe2b99e26584f57eef1b9c58468 to your computer and use it in GitHub Desktop.
C# Create Multiple Frame Paragraphs in Word Documents
// Prerequisite: Install <a href="https://www.nuget.org/packages/Openize.Words">Openize.Words</a>.
// 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");
// Create first framed paragraph
var para = new Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.Words.IElements.Run { Text = "This paragraph should have single width frames with blue color" });
para.ParagraphBorder.Width = Openize.Words.IElements.BorderWidth.Single;
para.ParagraphBorder.Color = Openize.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 Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.Words.IElements.Run { Text = "This paragraph should have double width frames with red color" });
para.ParagraphBorder.Width = Openize.Words.IElements.BorderWidth.Double;
para.ParagraphBorder.Color = Openize.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 Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.Words.IElements.Run { Text = "This paragraph should have dotted width frames with purple color" });
para.ParagraphBorder.Width = Openize.Words.IElements.BorderWidth.Dotted;
para.ParagraphBorder.Color = Openize.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 Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.Words.IElements.Run { Text = "This paragraph should have dotdash width frames with navy color" });
para.ParagraphBorder.Width = Openize.Words.IElements.BorderWidth.DotDash;
para.ParagraphBorder.Color = Openize.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 Openize.Words.IElements.Paragraph();
para.AddRun(new Openize.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