public void UncheckCheckbox(string filePath, string locatorText) { using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true)) { var textNode = doc.MainDocumentPart.RootElement.Descendants <Text>().Where(x => x.Text.Equals(locatorText)).FirstOrDefault(); var paragraph = textNode.Parent.Parent; // First we set the checked value to 0 SdtRun checkerElement = paragraph.Descendants <SdtRun>().FirstOrDefault(); SdtProperties properties = checkerElement.Descendants <SdtProperties>().FirstOrDefault(); SdtContentCheckBox checkbox = (SdtContentCheckBox)properties.ChildElements[2]; checkbox.Checked.Val = OnOffValues.Zero; // Next we change the symbol shown var content = properties.NextSibling().NextSibling(); SymbolChar symbol = content.Descendants <SymbolChar>().FirstOrDefault(); symbol.Char = "F072"; symbol.Font = "Wingdings"; } }
/// <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); }