1using System.Text.RegularExpressions;
2using Xceed.Document.NET;
10 public static class DocXPlaceholderHelper
15 public const string TableRowIDPlaceholder =
"ID";
19 #region Text Placeholders
26 public Dictionary<string, string> Placeholders =
new Dictionary<string, string>();
33 public void Add(
string placeholder,
string value)
35 if (Placeholders.ContainsKey(placeholder))
37 Placeholders[placeholder] = value;
41 Placeholders.Add(placeholder, value);
57 public static bool ReplaceTextPlaceholders(
string templateFile,
string outputFile, TextPlaceholders placeholders,
string placeholderMarker,
bool keepUnknownPlaceholders =
true)
60 using (DocX templateDocument = DocX.Load(templateFile))
63 result = templateDocument.ReplaceText(
new FunctionReplaceTextOptions()
65 FindPattern = placeholderMarker +
"(.*?)" + placeholderMarker,
66 RegExOptions = RegexOptions.IgnoreCase,
67 RegexMatchHandler = (placeholderWithoutMarkers) =>
69 if (placeholders.Placeholders.ContainsKey(placeholderWithoutMarkers))
71 return placeholders.Placeholders[placeholderWithoutMarkers];
73 return keepUnknownPlaceholders ? (placeholderMarker + placeholderWithoutMarkers + placeholderMarker) : string.Empty;
77 templateDocument.SaveAs(outputFile);
86 #region Table Placeholders
93 public Dictionary<string, List<string>> Placeholders =
new Dictionary<string, List<string>>();
100 public void Add(
string placeholder, List<string> value)
102 if (Placeholders.ContainsKey(placeholder))
104 Placeholders[placeholder] = value;
108 Placeholders.Add(placeholder, value);
125 public static bool ReplaceTablePlaceholders(
string templateFile,
string outputFile, TablePlaceholders placeholders,
string placeholderMarker)
128 using (DocX templateDocument = DocX.Load(templateFile))
130 foreach (Table templateTable
in templateDocument.Tables)
132 if (templateTable ==
null)
continue;
135 Row rowPattern =
null;
136 foreach (Row row
in templateTable.Rows)
138 foreach (
string placeholder
in placeholders.Placeholders.Keys)
140 if (row.FindUniqueByPattern(placeholderMarker + placeholder + placeholderMarker, RegexOptions.IgnoreCase).Count > 0)
147 if (rowPattern !=
null)
152 if (rowPattern ==
null)
158 int numberOfRows = placeholders.Placeholders.Values.Select(v => v.Count).Max();
159 for (
int i = 0; i < numberOfRows; i++)
162 Row newRow = templateTable.InsertRow(rowPattern, templateTable.RowCount - 1,
true);
165 result |= newRow.ReplaceText(
new FunctionReplaceTextOptions()
167 FindPattern = placeholderMarker +
"(.*?)" + placeholderMarker,
168 RegExOptions = RegexOptions.IgnoreCase,
169 RegexMatchHandler = (placeholderWithoutMarkers) =>
171 if (placeholders.Placeholders.ContainsKey(placeholderWithoutMarkers))
173 List<string> values = placeholders.Placeholders[placeholderWithoutMarkers];
174 return (i < values.Count) ? values[i] :
string.Empty;
176 else if (placeholderWithoutMarkers == TableRowIDPlaceholder)
178 return (i + 1).ToString();
180 return placeholderMarker + placeholderWithoutMarkers + placeholderMarker;
190 templateDocument.SaveAs(outputFile);
200 #region Conversion Methods
207 public static TablePlaceholders ConvertTextToTablePlaceholders(IEnumerable<TextPlaceholders> textPlaceholderList)
211 textPlaceholderList.SelectMany(dict => dict.Placeholders)
212 .GroupBy(kvp => kvp.Key)
214 .ForEach(group => tablePlaceholders.Add(group.Key, group.Select(kvp => kvp.Value).ToList()));
216 return tablePlaceholders;
Class to hold table placeholders and their list of values.
void Add(string placeholder, List< string > value)
Add a placeholder to the dictionary.
Class to hold text placeholders and their values.
void Add(string placeholder, string value)
Add a placeholder to the dictionary.