private WordTemplateModel GenerateDataMappingOxml(string TemplateFileLocation, WordTemplateModel model) { try { using (WordprocessingDocument document = WordprocessingDocument.Open(TemplateFileLocation, true)) { // Change the document type to Document document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document); // Get the MainPart of the document MainDocumentPart mainPart = document.MainDocumentPart; // Get the Document Settings Part DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart; OpenXmlElement[] Enumerate = mainPart.ContentControls().ToArray(); List <ModelField> dataMapping = new List <ModelField>(); for (int i = 0; i < Enumerate.Count(); i++) { OpenXmlElement cc = Enumerate[i]; SdtProperties props = cc.Elements <SdtProperties>().FirstOrDefault(); Tag tag = props.Elements <Tag>().FirstOrDefault(); SdtAlias alias = props.Elements <SdtAlias>().FirstOrDefault(); string title = ((DocumentFormat.OpenXml.Wordprocessing.StringType)(alias)).Val; string tagName = tag.Val; dataMapping.Add(new ModelField() { Key = title }); } model.DataMapping = dataMapping; } } catch (Exception ex) { throw; } return(model); }
public void wordDocOpenXml(string TemplateFileLocation, string GeneratedFileNameLocation, List <ModelField> dataMap) { if (System.IO.File.Exists(GeneratedFileNameLocation)) { System.IO.File.Delete(GeneratedFileNameLocation); } System.IO.File.Copy(TemplateFileLocation, GeneratedFileNameLocation); using (WordprocessingDocument document = WordprocessingDocument.Open(GeneratedFileNameLocation, true)) { // Change the document type to Document document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document); // Get the MainPart of the document MainDocumentPart mainPart = document.MainDocumentPart; // Get the Document Settings Part DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart; OpenXmlElement[] Enumerate = mainPart.ContentControls().ToArray(); for (int i = 0; i < Enumerate.Count(); i++) { OpenXmlElement cc = Enumerate[i]; SdtProperties props = cc.Elements <SdtProperties>().FirstOrDefault(); Tag tag = props.Elements <Tag>().FirstOrDefault(); //Console.WriteLine(tag.Val); SdtAlias alias = props.Elements <SdtAlias>().FirstOrDefault(); string title = ((DocumentFormat.OpenXml.Wordprocessing.StringType)(alias)).Val; string tagName = tag.Val; //if (dataMap.Any(f => string.Format("{0}Tag", f.Key) == tagName)) if (dataMap.Any(f => f.Key == title)) { var valkey = dataMap.FirstOrDefault(f => f.Key == title).Value; object val = POCUtil.DictionaryMappedDocPOC[valkey]; OpenXmlElement parentElement = cc.Parent; DocumentFormat.OpenXml.Wordprocessing.Paragraph pg = cc.Descendants <DocumentFormat.OpenXml.Wordprocessing.Paragraph>().FirstOrDefault(); if (pg != null || true) { //ParagraphProperties paragraphProperties = (ParagraphProperties)cc.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().FirstOrDefault().ParagraphProperties.Clone(); Run r1 = null; DocumentFormat.OpenXml.Wordprocessing.Paragraph p1 = null; if (cc.Parent.GetType() != typeof(DocumentFormat.OpenXml.Wordprocessing.Paragraph)) { p1 = parentElement.InsertAfter(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(), cc); r1 = p1.AppendChild(new Run()); } else { r1 = parentElement.InsertAfter(new Run(), cc); } r1.RunProperties = new RunProperties(); cc.Descendants <DocumentFormat.OpenXml.Wordprocessing.RunProperties>().ToList().ForEach( runProperty => runProperty.ToList().ForEach( property => { if (!r1.RunProperties.ChildElements.ToList().Exists(propertyToAdd => propertyToAdd.GetType() == property.GetType())) { r1.RunProperties.AppendChild((OpenXmlElement)property.CloneNode(true)); } } ) ); if (cc.Descendants <DocumentFormat.OpenXml.Office2010.Word.SdtContentCheckBox>().Count() > 0) { //☒ //☐ //☐ var t2 = r1.AppendChild(new Text("☒")); //DocumentFormat.OpenXml.Wordprocessing.CheckBox c1 = r1.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.CheckBox()); //c1.AppendChild(new Checked()); } else { var t1 = r1.AppendChild(new Text(val.ToString())); } } } } while (mainPart.ContentControls().Count() > 0) { mainPart.ContentControls().FirstOrDefault().Remove(); } // Save the document mainPart.Document.Save(); document.Close(); } }