private void UpdateDocument(DocX document, DocumentFormValue doc) { foreach (var p in doc.Components) { var key = string.Format("[[{0}]]", p.Key); if (string.IsNullOrEmpty(p.Value) && p.RemoveLineIfResultIsEmpty) { var paragraph = document.Paragraphs.FirstOrDefault(x => x.Text.Contains(key)); if (paragraph != null) { var lines = paragraph.Text.Split('\n'); if (lines.Length <= 1) { paragraph.Remove(false); } else { paragraph.ReplaceText("\n" + key, string.Empty); paragraph.ReplaceText(key + "\n", string.Empty); } } } else { document.ReplaceText(key, p.Value ?? string.Empty, false, RegexOptions.IgnoreCase); } } }
public HttpResponseMessage GenerateDocument([FromBody] DocumentFormValue doc) { var path = GetFilePath(doc.FileName); using (DocX document = DocX.Load(path)) { UpdateDocument(document, doc); return(SaveDocument(document)); } }