-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParagraphFrameExamples.cs
More file actions
241 lines (224 loc) · 12.8 KB
/
ParagraphFrameExamples.cs
File metadata and controls
241 lines (224 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System.Linq;
namespace FileFormat.Words.Examples
{
/// <summary>
/// Provides C# code examples for creating, reading, and modifying Word paragraphs frames/borders
/// using the <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a> library.
/// </summary>
/// <example>
/// <code>
/// // Prepares directory Documents/Paragraph/Frame at the root of your project.
/// // Check reference for more options and details.
/// var paraFrameExamples = new FileFormat.Words.Examples.ParagraphFrameExamples();
/// // Creates a word document with 4 different paragraphs with frames + one paragraph without frames and saves
/// // word document to the specified directory. Check reference for more options and details.
/// paraFrameExamples.CreateParagraphsFrames();
/// // Reads Paragraphs from the specified Word Document and displays plain text with borders/frames info.
/// // Check reference for more options and details.
/// paraFrameExamples.ReadParagraphsFrames();
/// // Modifies border and text of framed paragraphs in the Word Document and saves the modified word document.
/// // Check reference for more options and details.
/// paraFrameExamples.ModifyParagraphsFrames();
/// </code>
/// </example>
public class ParagraphFrameExamples
{
private const string docsDirectory = "../../../Documents/Paragraph/Frame";
/// <summary>
/// Initializes a new instance of the <see cref="ParagraphFrameExamples"/> class.
/// Prepares the directory 'Documents/Paragraph/Frame' for storing or loading Word documents
/// at the root of the project.
/// If the directory doesn't exist, it is created. If it already exists,
/// existing files are deleted, and the directory is cleaned up.
/// </summary>
public ParagraphFrameExamples()
{
if (!System.IO.Directory.Exists(docsDirectory))
{
// If it doesn't exist, create the directory
System.IO.Directory.CreateDirectory(docsDirectory);
System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(docsDirectory)}' " +
$"created successfully.");
}
else
{
var files = System.IO.Directory.GetFiles(System.IO.Path.GetFullPath(docsDirectory));
foreach (var file in files)
{
System.IO.File.Delete(file);
System.Console.WriteLine($"File deleted: {file}");
}
System.Console.WriteLine($"Directory '{System.IO.Path.GetFullPath(docsDirectory)}' " +
$"cleaned up.");
}
}
/// <summary>
/// Creates a new Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Generates 4 different paragraphs with frames/borders + one paragraph without frames/borders.
/// Saves the newly created Word Document.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document will be saved (default is the 'Documents/Paragraph/Frame' directory auto-created at the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file (default is "WordParagraphsFrame.docx").
/// </param>
public void CreateParagraphsFrames(string documentDirectory = docsDirectory, string filename = "WordParagraphsFrame.docx")
{
try
{
// 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($"{documentDirectory}/{filename}");
System.Console.WriteLine($"Word Document {filename} Created. Please check directory: " +
$"{System.IO.Path.GetFullPath(documentDirectory)}");
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}
/// <summary>
/// Loads a Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Traverses paragraphs and displays thier plain text along with border/frame info.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document to load is present
/// (default is the 'Documents/Paragraph/Frame' directory auto-created at the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file to load (default is "WordParagraphsFrame.docx").
/// </param>
public void ReadParagraphsFrames(string documentDirectory = docsDirectory, string filename = "WordParagraphsFrame.docx")
{
try
{
// Load the Word Document
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}");
var body = new FileFormat.Words.Body(doc);
// Traverse and display paragraphs with plain text
foreach (var paragraph in body.Paragraphs)
{
System.Console.WriteLine($" Paragraph Text: {paragraph.Text}");
if (paragraph.ParagraphBorder.Size > 0)
{
System.Console.WriteLine($" Paragraph Border Width: {paragraph.ParagraphBorder.Width}");
System.Console.WriteLine($" Paragraph Border Color: {paragraph.ParagraphBorder.Color}");
System.Console.WriteLine($" Paragraph Border Size: {paragraph.ParagraphBorder.Size}");
}
else
{
System.Console.WriteLine($" Paragraph Border Width: ");
System.Console.WriteLine($" Paragraph Border Color: ");
System.Console.WriteLine($" Paragraph Border Size: ");
}
}
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}
/// <summary>
/// Loads a Word Document with structured content using
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>.
/// Modifies paragraphs frames to single border width and black color.
/// Modifies the text of framed paragraph with modified frames info.
/// Saves the modified Word Document.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document to load is present and
/// the modified document will be saved (default is the 'Documents/Paragraph' directory auto-created at the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file to modify (default is "WordParagraphs.docx").
/// </param>
/// <param name="filenameModified">
/// The name of the modified Word Document (default is "ModifiedWordParagraphs.docx").
/// </param>
public void ModifyParagraphsFrames(string documentDirectory = docsDirectory,
string filename = "WordParagraphsFrame.docx", string filenameModified = "ModifiedParagraphsFrame.docx")
{
try
{
// Load the Word Document
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}");
var body = new FileFormat.Words.Body(doc);
foreach (var paragraph in body.Paragraphs)
{
if (paragraph.ParagraphBorder.Size > 0)
{
paragraph.ParagraphBorder.Width = FileFormat.Words.IElements.BorderWidth.Single;
paragraph.ParagraphBorder.Color = FileFormat.Words.IElements.Colors.Black;
foreach (var run in paragraph.Runs)
{
// Modified paragraph text
run.Text = "Paragraph border modified to single width with black color";
}
System.Console.WriteLine("Frames/Borders changed to single width with black color");
}
// Update the paragraph in the document
doc.Update(paragraph);
}
// Save the modified Word Document
doc.Save($"{documentDirectory}/{filenameModified}");
System.Console.WriteLine($"Word Document {filename} Modified and Saved As " +
$"{filenameModified}. Please check directory: " +
$"{System.IO.Path.GetFullPath(documentDirectory)}");
}
catch (System.Exception ex)
{
throw new FileFormat.Words.FileFormatException("An error occurred.", ex);
}
}
}
}