/// <summary> /// Данный метод возвращает Объект текста контент-контрола /// </summary> /// <param name = "document" > Документ Word</param> /// <param name = "name" > Название поля</param> /// <param name = "value" > Значение поля</param> /// <returns>Text объект поля</returns> private void FillContentControls(WordprocessingDocument document, string name, string value) { List <SdtElement> blocks = new List <SdtElement>(); blocks.AddRange(document.MainDocumentPart.Document.Body.Descendants <SdtElement>()); IEnumerable <SdtElement> matchBlocks = blocks.Where(x => { var alias = x.SdtProperties.OfType <SdtAlias>().FirstOrDefault(); return(alias != null && alias.Val.Value == name); }); foreach (SdtElement block in matchBlocks) { if (block is SdtRun) { SdtContentRun contRun = block.Descendants <SdtContentRun>().First(); Run run = contRun.Descendants().OfType <Run>().FirstOrDefault(); if (run == null) { run = new Run(); if (!run.Elements <RunProperties>().Any()) { run.PrependChild(new RunProperties()); } RunProperties runProp = run.RunProperties; if (runProp.RunStyle == null) { runProp.RunStyle = new RunStyle(); } runProp.RunStyle.Val = (block.SdtProperties.FirstChild.FirstChild as RunStyle)?.Val; SetContentControlValue(run, value); contRun.Append(run); } else { SetContentControlValue(run, value); } } else { Paragraph contentFieldParagraph = block.Descendants().OfType <Paragraph>().FirstOrDefault(); Run run = contentFieldParagraph.OfType <Run>().FirstOrDefault(); if (run == null) { run = new Run(); SetContentControlValue(run, value); contentFieldParagraph.Append(run); } SetContentControlValue(run, value); } } }
/// <summary> /// FillDataObject /// </summary> /// <param name="dataInputObject"></param> /// <param name="stream"></param> public void FillDataObject(object dataInputObject, Stream stream) { if (dataInputObject != null) { if (stream != null && stream.CanRead && stream.CanWrite) { // Use OpenXML to process var dictionary = GetDictionaryFromObject(dataInputObject); using (WordprocessingDocument word = WordprocessingDocument.Open(stream, true)) { var part = word.MainDocumentPart; var elements = part.Document.Descendants <SdtElement>().ToList(); foreach (SdtElement element in elements) { SdtAlias alias = element.Descendants <SdtAlias>().FirstOrDefault(); if (alias != null) { // Get title of content control var title = alias.Val.Value; if (dictionary.ContainsKey(title)) { object value = dictionary[title]; if (element.ToString().Equals("DocumentFormat.OpenXml.Wordprocessing.SdtRun")) { SdtRun run = element as SdtRun; if (run != null) { SdtContentRun contentRun = run.Descendants <SdtContentRun>().FirstOrDefault(); Run xRun = contentRun.Descendants <Run>().FirstOrDefault(); if (xRun == null) { contentRun.AppendChild(new Run()); xRun = contentRun.Descendants <Run>().FirstOrDefault(); } Text text = xRun.Descendants <Text>().FirstOrDefault(); if (text == null) { xRun.AppendChild(new Text(value.ToString())); text = xRun.Descendants <Text>().FirstOrDefault(); } text.Text = value.ToString(); } } else if (element.ToString().Equals("DocumentFormat.OpenXml.Wordprocessing.SdtBlock")) { SdtBlock block = element as SdtBlock; if (block != null) { SdtContentBlock contentBlock = block.Descendants <SdtContentBlock>().FirstOrDefault(); Run xRun = contentBlock.Descendants <Run>().FirstOrDefault(); if (xRun == null) { contentBlock.AppendChild(new Run()); xRun = contentBlock.Descendants <Run>().FirstOrDefault(); } Text text = xRun.Descendants <Text>().FirstOrDefault(); if (text == null) { xRun.AppendChild(new Text(value.ToString())); text = xRun.Descendants <Text>().FirstOrDefault(); } text.Text = value.ToString(); } } } } } part.Document.Save(); } } } }
/// <summary> /// Fill data object into word file that has arrBytesSource data. /// </summary> /// <param name="dataInputObject"></param> /// <param name="arrBytesSource"></param> /// <returns>The MemoryStream object.</returns> public Stream FillDataObject(object dataInputObject, byte[] arrBytesSource) { MemoryStream memoryStream = null; try { if (dataInputObject != null) { if (arrBytesSource != null && arrBytesSource.Length > 0) { #region Fix bug TFS #1971 //memoryStream = new MemoryStream(arrBytesSource, true); memoryStream = new MemoryStream(); memoryStream.Write(arrBytesSource, 0, arrBytesSource.Length); memoryStream.Position = 0; #endregion // Use OpenXML to process var dictionary = GetDictionaryFromObject(dataInputObject); using (WordprocessingDocument word = WordprocessingDocument.Open(memoryStream, true)) { var part = word.MainDocumentPart; var elements = part.Document.Descendants <SdtElement>().ToList(); foreach (SdtElement element in elements) { SdtAlias alias = element.Descendants <SdtAlias>().FirstOrDefault(); if (alias != null) { // Get title of content control var title = alias.Val.Value; if (dictionary.ContainsKey(title)) { object value = dictionary[title]; if (value != null) { if (element.ToString().Equals("DocumentFormat.OpenXml.Wordprocessing.SdtRun")) { SdtRun run = element as SdtRun; if (run != null) { if (value is bool) { SdtContentCheckBox contentCheckBox = run.Descendants <SdtContentCheckBox>().FirstOrDefault(); if (contentCheckBox != null) { if (string.Compare(value.ToString(), Boolean.TrueString, true) == 0) { contentCheckBox.Checked.Val = string.Compare(value.ToString(), Boolean.TrueString, true) == 0 ? OnOffValues.One : OnOffValues.Zero; SdtContentRun contentRun = run.Descendants <SdtContentRun>().FirstOrDefault(); if (contentRun != null) { Run xRun = contentRun.Descendants <Run>().FirstOrDefault(); if (xRun != null) { SymbolChar checkedSymbolChar = xRun.Descendants <SymbolChar>().FirstOrDefault(); if (checkedSymbolChar != null) { checkedSymbolChar.Char = new HexBinaryValue(CheckedSymbolChar); } } } } } } else { SdtContentRun contentRun = run.Descendants <SdtContentRun>().FirstOrDefault(); Run xRun = contentRun.Descendants <Run>().FirstOrDefault(); if (xRun == null) { contentRun.AppendChild(new Run()); xRun = contentRun.Descendants <Run>().FirstOrDefault(); } Text text = xRun.Descendants <Text>().FirstOrDefault(); if (text == null) { xRun.AppendChild(new Text(value.ToString())); text = xRun.Descendants <Text>().FirstOrDefault(); } text.Text = value.ToString(); } } } else if (element.ToString().Equals("DocumentFormat.OpenXml.Wordprocessing.SdtBlock")) { SdtBlock block = element as SdtBlock; if (block != null) { if (value is bool) { SdtContentCheckBox contentCheckBox = block.Descendants <SdtContentCheckBox>().FirstOrDefault(); if (contentCheckBox != null) { if (string.Compare(value.ToString(), Boolean.TrueString, true) == 0) { contentCheckBox.Checked.Val = string.Compare(value.ToString(), Boolean.TrueString, true) == 0 ? OnOffValues.One : OnOffValues.Zero; SdtContentRun contentRun = block.Descendants <SdtContentRun>().FirstOrDefault(); if (contentRun != null) { Run xRun = contentRun.Descendants <Run>().FirstOrDefault(); if (xRun != null) { SymbolChar checkedSymbolChar = xRun.Descendants <SymbolChar>().FirstOrDefault(); if (checkedSymbolChar != null) { checkedSymbolChar.Char = new HexBinaryValue(CheckedSymbolChar); } } } } } } else { SdtContentBlock contentBlock = block.Descendants <SdtContentBlock>().FirstOrDefault(); Run xRun = contentBlock.Descendants <Run>().FirstOrDefault(); if (xRun == null) { contentBlock.AppendChild(new Run()); xRun = contentBlock.Descendants <Run>().FirstOrDefault(); } Text text = xRun.Descendants <Text>().FirstOrDefault(); if (text == null) { xRun.AppendChild(new Text(value.ToString())); text = xRun.Descendants <Text>().FirstOrDefault(); } text.Text = value.ToString(); } } } } } } } part.Document.Save(); } } } } catch (Exception ex) { ULSLogging.LogError(ex); throw ex; } return(memoryStream); }