-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParagraphIndentExamples.cs
259 lines (244 loc) · 12.8 KB
/
ParagraphIndentExamples.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System.Linq;
namespace FileFormat.Words.Examples
{
/// <summary>
/// Provides C# code examples for creating, reading, and modifying indentation of Word paragraphs
/// using the <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a> library.
/// </summary>
/// <example>
/// <code>
/// // Prepares directory Documents/Paragraph/Indent at the root of your project.
/// // Check reference for more options and details.
/// ParagraphIndentExamples paragraphIndentExamples = new ParagraphIndentExamples();
/// // Creates a word document with paragraphs having various indentations and saves word document
/// // to the specified directory. Check reference for more options and details.
/// paragraphIndentExamples.CreateIndent();
/// // Reads Paragraphs from the specified Word Document and displays plain text with indentation.
/// // Check reference for more options and details.
/// paragraphIndentExamples.ReadIndent();
/// // Modifies Paragraph's indentation in the Word Document and saves the modified word document.
/// // Check reference for more options and details.
/// paragraphIndentExamples.ModifyIndent();
/// </code>
/// </example>
public class ParagraphIndentExamples
{
private const string docsDirectory = "../../../Documents/Paragraph/Indent";
/// <summary>
/// Initializes a new instance of the <see cref="ParagraphIndentExamples"/> class.
/// Prepares the directory 'Documents/Paragraph/Indent' 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 ParagraphIndentExamples()
{
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 paragraphs with different indents including left, right, firstline and hanging.
/// Saves the newly created Word Document.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word 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 (default is "WordParagraphs.docx").
/// </param>
public void CreateIndent(string documentDirectory = docsDirectory, string filename = "WordParagraphsIndented.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");
// Add paragraph with left indentation
FileFormat.Words.IElements.Paragraph para1 = new FileFormat.Words.IElements.Paragraph();
para1.AddRun(new FileFormat.Words.IElements.Run
{
Text = "First paragraph with left indentation"
});
// Setting the Paragraph Indent 2 inches to left.
para1.Indentation.Left = 2;
// Add paragraph with right indentation
FileFormat.Words.IElements.Paragraph para2 = new FileFormat.Words.IElements.Paragraph();
para2.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Second paragraph with right indentation"
});
// Setting the Paragraph Indent 2 inches to right.
para2.Indentation.Right = 2;
// Add paragraph with firstline indentation
FileFormat.Words.IElements.Paragraph para3 = new FileFormat.Words.IElements.Paragraph();
para3.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Third paragraph with firstline indentation"
});
// Setting the Paragraph Indent 2 inches to firstline.
para3.Indentation.FirstLine = 2;
// Add paragraph with hanging indentation
FileFormat.Words.IElements.Paragraph para4 = new FileFormat.Words.IElements.Paragraph();
para4.AddRun(new FileFormat.Words.IElements.Run
{
Text = "Fourth paragraph with hanging indentation"
});
// Setting the Paragraph Indent 2 inches to hanging.
para4.Indentation.Hanging = 2;
// 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($"{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 its text along with indentation.
/// </summary>
/// <param name="documentDirectory">
/// The directory where the Word Document to load is present
/// (default is the 'Documents/Paragraph/Indent' directory auto-created at the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file to load (default is "WordParagraphsIndented.docx").
/// </param>
public void ReadIndent(string documentDirectory = docsDirectory, string filename = "WordParagraphsIndented.docx")
{
try
{
// Load the Word Document
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}");
// Initialize the body with the document
var body = new FileFormat.Words.Body(doc);
System.Collections.Generic.List<FileFormat.Words.IElements.Paragraph>
paragraphs = body.Paragraphs;
foreach (FileFormat.Words.IElements.Paragraph paragraph in paragraphs)
{
System.Console.WriteLine($"Paragraph Text : {paragraph.Text}");
switch (paragraph.Indentation)
{
case var indent when indent.Left > 0:
System.Console.WriteLine($"Paragraph Left Indent: {indent.Left}");
break;
case var indent when indent.Right > 0:
System.Console.WriteLine($"Paragraph Right Indent: {indent.Right}");
break;
case var indent when indent.FirstLine > 0:
System.Console.WriteLine($"Paragraph Firstline Indent: {indent.FirstLine}");
break;
case var indent when indent.Hanging > 0:
System.Console.WriteLine($"Paragraph Hanging Indent: {indent.Hanging}");
break;
default:
System.Console.WriteLine("No Indentation.");
break;
}
}
}
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 through all paragraphs within the document.
/// Modifies paragraphs by appending the relevant indent message in italic format
/// and modifies the indent (if found) to 0.
/// 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/Indent' directory auto-created at the root of your project).
/// </param>
/// <param name="filename">
/// The name of the Word Document file to modify (default is "WordParagraphsIndented.docx").
/// </param>
/// <param name="filenameModified">
/// The name of the modified Word Document (default is "ModifiedWordParagraphsIndented.docx").
/// </param>
public void ModifyIndent(string documentDirectory = docsDirectory,
string filename = "WordParagraphsIndented.docx", string filenameModified = "ModifiedWordParagraphsIndented.docx")
{
try
{
// Load the Word Document
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}");
// Initialize the body with the document
var body = new FileFormat.Words.Body(doc);
foreach (FileFormat.Words.IElements.Paragraph paragraph in body.Paragraphs)
{
switch (paragraph.Indentation)
{
case var indent when indent.Left > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (left indent set to 0)", Italic = true });
paragraph.Indentation.Left = 0;
break;
case var indent when indent.Right > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (right indent set to 0)", Italic = true });
paragraph.Indentation.Right = 0;
break;
case var indent when indent.FirstLine > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (FirstLine indent set to 0)", Italic = true });
paragraph.Indentation.FirstLine = 0;
break;
case var indent when indent.Hanging > 0:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (hanging indent set to 0)", Italic = true });
paragraph.Indentation.Hanging = 0;
break;
default:
paragraph.AddRun(new FileFormat.Words.IElements.Run
{ Text = " (No indent and no change)", Italic = true });
break;
}
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);
}
}
}
}