Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
DocXPlaceholderHelper.cs
1using System.Text.RegularExpressions;
2using Xceed.Document.NET;
3using Xceed.Words.NET;
4
6{
10 public static class DocXPlaceholderHelper
11 {
15 public const string TableRowIDPlaceholder = "ID";
16
17 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
19 #region Text Placeholders
20
24 public class TextPlaceholders
25 {
26 public Dictionary<string, string> Placeholders = new Dictionary<string, string>();
27
33 public void Add(string placeholder, string value)
34 {
35 if (Placeholders.ContainsKey(placeholder))
36 {
37 Placeholders[placeholder] = value;
38 }
39 else
40 {
41 Placeholders.Add(placeholder, value);
42 }
43 }
44 }
45
46 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
47
57 public static bool ReplaceTextPlaceholders(string templateFile, string outputFile, TextPlaceholders placeholders, string placeholderMarker, bool keepUnknownPlaceholders = true)
58 {
59 bool result;
60 using (DocX templateDocument = DocX.Load(templateFile))
61 {
62 // Replace all placeholders in the document
63 result = templateDocument.ReplaceText(new FunctionReplaceTextOptions()
64 {
65 FindPattern = placeholderMarker + "(.*?)" + placeholderMarker,
66 RegExOptions = RegexOptions.IgnoreCase,
67 RegexMatchHandler = (placeholderWithoutMarkers) =>
68 {
69 if (placeholders.Placeholders.ContainsKey(placeholderWithoutMarkers))
70 {
71 return placeholders.Placeholders[placeholderWithoutMarkers];
72 }
73 return keepUnknownPlaceholders ? (placeholderMarker + placeholderWithoutMarkers + placeholderMarker) : string.Empty;
74 }
75 });
76 // Save the document with the replaced placeholders as new file
77 templateDocument.SaveAs(outputFile);
78 }
79 return result;
80 }
81
82 #endregion
83
84 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
85
86 #region Table Placeholders
87
91 public class TablePlaceholders
92 {
93 public Dictionary<string, List<string>> Placeholders = new Dictionary<string, List<string>>();
94
100 public void Add(string placeholder, List<string> value)
101 {
102 if (Placeholders.ContainsKey(placeholder))
103 {
104 Placeholders[placeholder] = value;
105 }
106 else
107 {
108 Placeholders.Add(placeholder, value);
109 }
110 }
111 }
112
113 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114
125 public static bool ReplaceTablePlaceholders(string templateFile, string outputFile, TablePlaceholders placeholders, string placeholderMarker)
126 {
127 bool result = false;
128 using (DocX templateDocument = DocX.Load(templateFile))
129 {
130 foreach (Table templateTable in templateDocument.Tables)
131 {
132 if (templateTable == null) continue;
133
134 // Try to find at least one placeholder in this table
135 Row rowPattern = null;
136 foreach (Row row in templateTable.Rows)
137 {
138 foreach (string placeholder in placeholders.Placeholders.Keys)
139 {
140 if (row.FindUniqueByPattern(placeholderMarker + placeholder + placeholderMarker, RegexOptions.IgnoreCase).Count > 0)
141 {
142 // Placeholder found in this table
143 rowPattern = row;
144 break;
145 }
146 }
147 if (rowPattern != null)
148 {
149 break;
150 }
151 }
152 if (rowPattern == null)
153 {
154 // No placeholder found in this table
155 continue;
156 }
157
158 int numberOfRows = placeholders.Placeholders.Values.Select(v => v.Count).Max();
159 for (int i = 0; i < numberOfRows; i++)
160 {
161 // Insert new row at the end of this table
162 Row newRow = templateTable.InsertRow(rowPattern, templateTable.RowCount - 1, true);
163
164 // Replace all placeholders in the new row
165 result |= newRow.ReplaceText(new FunctionReplaceTextOptions()
166 {
167 FindPattern = placeholderMarker + "(.*?)" + placeholderMarker,
168 RegExOptions = RegexOptions.IgnoreCase,
169 RegexMatchHandler = (placeholderWithoutMarkers) =>
170 {
171 if (placeholders.Placeholders.ContainsKey(placeholderWithoutMarkers))
172 {
173 List<string> values = placeholders.Placeholders[placeholderWithoutMarkers];
174 return (i < values.Count) ? values[i] : string.Empty;
175 }
176 else if (placeholderWithoutMarkers == TableRowIDPlaceholder)
177 {
178 return (i + 1).ToString();
179 }
180 return placeholderMarker + placeholderWithoutMarkers + placeholderMarker;
181 }
182 });
183 }
184
185 // Remove the pattern row.
186 rowPattern.Remove();
187 }
188
189 // Save the document with the replaced placeholders as new file
190 templateDocument.SaveAs(outputFile);
191
192 return result;
193 }
194 }
195
196 #endregion
197
198 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
199
200 #region Conversion Methods
201
207 public static TablePlaceholders ConvertTextToTablePlaceholders(IEnumerable<TextPlaceholders> textPlaceholderList)
208 {
209 TablePlaceholders tablePlaceholders = new TablePlaceholders();
210
211 textPlaceholderList.SelectMany(dict => dict.Placeholders)
212 .GroupBy(kvp => kvp.Key)
213 .ToList()
214 .ForEach(group => tablePlaceholders.Add(group.Key, group.Select(kvp => kvp.Value).ToList()));
215
216 return tablePlaceholders;
217 }
218
219 #endregion
220 }
221}
Class to hold table placeholders and their list of values.
void Add(string placeholder, List< string > value)
Add a placeholder to the dictionary.
void Add(string placeholder, string value)
Add a placeholder to the dictionary.