public static byte[] GenerateDocument(byte[] templateBuffer, DCTWordDataObject dataobject) { byte[] buffer = templateBuffer; using (MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true)) { foreach (DCTDataProperty property in dataobject.PropertyCollection) { GeneralDataProcessor gdp = GeneralDataProcessor.CreateProcessor(wordDoc, property); gdp.Process(); } DeleteComments(wordDoc); if (wordDoc.MainDocumentPart.WordprocessingCommentsPart != null) { wordDoc.MainDocumentPart.WordprocessingCommentsPart.Comments.Save(); } wordDoc.MainDocumentPart.Document.Save(); } byte[] resultBuffer = new byte[(int)ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(resultBuffer, 0, (int)ms.Length); return(resultBuffer); } }
public static byte[] GenerateDocument(byte[] templateBuffer, DCTWordDataObject dataobject) { byte[] buffer = templateBuffer;//File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templatePath)); using (MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true)) { //var permList = wordDoc.MainDocumentPart.Document.Body.Descendants<PermStart>().ToList(); foreach (DCTDataProperty property in dataobject.PropertyCollection) { GeneralDataProcessor gdp = GeneralDataProcessor.CreateProcessor(wordDoc, property); gdp.Process(); } DeleteComments(wordDoc); if (wordDoc.MainDocumentPart.WordprocessingCommentsPart != null) { wordDoc.MainDocumentPart.WordprocessingCommentsPart.Comments.Save(); } wordDoc.MainDocumentPart.Document.Save(); } byte[] resultBuffer = new byte[(int)ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(resultBuffer, 0, (int)ms.Length); return resultBuffer; } }
public static byte[] GenerateDocument(byte[] templateBuffer, DCTWordDataObject dataobject) { byte[] buffer = templateBuffer; //File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templatePath)); using (MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true)) { //var permList = wordDoc.MainDocumentPart.Document.Body.Descendants<PermStart>().ToList(); foreach (DCTDataProperty property in dataobject.PropertyCollection) { GeneralDataProcessor gdp = GeneralDataProcessor.CreateProcessor(wordDoc, property); gdp.Process(); } DeleteComments(wordDoc); if (wordDoc.MainDocumentPart.WordprocessingCommentsPart != null) { wordDoc.MainDocumentPart.WordprocessingCommentsPart.Comments.Save(); } wordDoc.MainDocumentPart.Document.Save(); } byte[] resultBuffer = new byte[(int)ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(resultBuffer, 0, (int)ms.Length); return(resultBuffer); } }
private static void processDataControl(WordprocessingDocument document, DCTWordDataObject wdo, List <int> ignoreControls) { var AllSdtElements = document.MainDocumentPart.Document.Body.Descendants <SdtElement>().ToList(); foreach (SdtElement se in AllSdtElements) { var curAlias = se.SdtProperties.Descendants <SdtAlias>().ToList(); if (curAlias.Count == 0) { continue; } var curId = se.SdtProperties.Descendants <SdtId>().ToList(); if (curId.Count == 0) { continue; } //如果控件在黑名单中,忽略 if (curAlias[0].Val == null || ignoreControls.Contains(curId[0].Val.Value)) { continue; } //生成简单类型 DCTSimpleProperty sp = new DCTSimpleProperty() { TagID = curAlias[0].Val.Value, Value = se.InnerText }; wdo.PropertyCollection.Add(sp); } }
public byte[] DCMBuildDocument(string templateUri, DCTWordDataObject wordData) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { byte[] templateBinary = context.OpenBinary(templateUri); return WordEntry.GenerateDocument(templateBinary, wordData); } }
public byte[] DCMBuildDocument(string templateUri, DCTWordDataObject wordData) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { byte[] templateBinary = context.OpenBinary(templateUri); return(WordEntry.GenerateDocument(templateBinary, wordData)); } }
public DCTWordDataObject AnalyzeDocument(byte[] wordFileContent) { DCTWordDataObject result = null; ServiceProxy.SingleCall <IDCSDocumentAnalyzeService>(binding, documentAnalyzeEndpointAddress, userBehavior, action => { result = action.DCMAnalyze(wordFileContent); }); return(result); }
private static void processDataAreas(WordprocessingDocument document, DCTWordDataObject wdo, List <int> ignoreControls) { var titleRows = document.MainDocumentPart.Document.Body .Descendants <TableRow>().Where(o => o.Descendants <BookmarkStart>().Any()); foreach (TableRow titleRow in titleRows) { var bookmarkStart = titleRow.Descendants <BookmarkStart>().FirstOrDefault(); if (bookmarkStart == null) { continue; } if (bookmarkStart.ColumnFirst == null) { continue; } //针对每一个区域,先读取表头的控件,同时将控件加入黑名单 DCTDataColumnCollection collection = new DCTDataColumnCollection(titleRow); ignoreControls.AddRange(collection.CoveredControlIds); //遍历区域中的每一行,生成复杂属性 var curRow = titleRow.NextSibling <TableRow>(); DCTComplexProperty cp = new DCTComplexProperty(); cp.TagID = bookmarkStart.Name; while (null != curRow && (!curRow.Descendants <BookmarkEnd>().Any(o => o.Id == bookmarkStart.Id))) { DCTWordDataObject childWdo = new DCTWordDataObject(); DCTDataRow newDataRow = DCTDataRow.FromTableRow(curRow, collection); if (!newDataRow.IsEmpty) { childWdo.PropertyCollection.AddRange <DCTSimpleProperty>(newDataRow.ToSimpleProperties()); } curRow = curRow.NextSibling <TableRow>(); cp.DataObjects.Add(childWdo); } wdo.PropertyCollection.Add(cp); } }
public static DCTWordDataObject Build(object source,string tagID) { ExceptionHelper.TrueThrow(source == null, "源对象不能为空"); DCTWordDataObject wordObj = new DCTWordDataObject(); Type sourceType = source.GetType(); PropertyInfo[] fieldInfos = sourceType.GetProperties(); foreach (PropertyInfo propInfo in fieldInfos) { if (propInfo.PropertyType.Name != typeof(object).Name) { DCTSimpleProperty simpleProp = ConvertToSimpleProperty(propInfo, source,""); if (simpleProp != null) wordObj.PropertyCollection.Add(simpleProp); } } return wordObj; }
private static void processDataControl(WordprocessingDocument document, DCTWordDataObject wdo, List<int> ignoreControls) { var AllSdtElements = document.MainDocumentPart.Document.Body.Descendants<SdtElement>().ToList(); foreach (SdtElement se in AllSdtElements) { var curAlias = se.SdtProperties.Descendants<SdtAlias>().ToList(); if (curAlias.Count == 0) continue; var curId = se.SdtProperties.Descendants<SdtId>().ToList(); if (curId.Count == 0) continue; //如果控件在黑名单中,忽略 if (curAlias[0].Val == null || ignoreControls.Contains(curId[0].Val.Value)) continue; //生成简单类型 DCTSimpleProperty sp = new DCTSimpleProperty() { TagID = curAlias[0].Val.Value, Value = se.InnerText }; wdo.PropertyCollection.Add(sp); } }
public static DCTWordDataObject CollectData(byte[] data) { using (MemoryStream ms = new MemoryStream()) { ms.Write(data, 0, data.Length); using (WordprocessingDocument document = WordprocessingDocument.Open(ms, true)) { DCTWordDataObject wdo = new DCTWordDataObject(); wdo.PropertyCollection = new DCTDataPropertyCollection(); List <int> ignoreControls = new List <int>(); //读取所有标签标记的区域(bookmarks) processDataAreas(document, wdo, ignoreControls); //遍历所有控件 processDataControl(document, wdo, ignoreControls); return(wdo); } } }
public static DCTWordDataObject Build(object source, string tagID) { ExceptionHelper.TrueThrow(source == null, "源对象不能为空"); DCTWordDataObject wordObj = new DCTWordDataObject(); Type sourceType = source.GetType(); PropertyInfo[] fieldInfos = sourceType.GetProperties(); foreach (PropertyInfo propInfo in fieldInfos) { if (propInfo.PropertyType.Name != typeof(object).Name) { DCTSimpleProperty simpleProp = ConvertToSimpleProperty(propInfo, source, ""); if (simpleProp != null) { wordObj.PropertyCollection.Add(simpleProp); } } } return(wordObj); }
public static DCTWordDataObject CollectData(byte[] data) { using (MemoryStream ms = new MemoryStream()) { ms.Write(data, 0, data.Length); using (WordprocessingDocument document = WordprocessingDocument.Open(ms, true)) { DCTWordDataObject wdo = new DCTWordDataObject(); wdo.PropertyCollection = new DCTDataPropertyCollection(); List<int> ignoreControls = new List<int>(); //读取所有标签标记的区域(bookmarks) processDataAreas(document, wdo, ignoreControls); //遍历所有控件 processDataControl(document, wdo, ignoreControls); return wdo; } } }
public WordTemplate() { wordDataObject = new DCTWordDataObject(); }
private static void processDataAreas(WordprocessingDocument document, DCTWordDataObject wdo, List<int> ignoreControls) { var titleRows = document.MainDocumentPart.Document.Body.Descendants<TableRow>().Where(o => o.Descendants<BookmarkStart>().Any()); foreach (TableRow titleRow in titleRows) { var bookmarkStart = titleRow.Descendants<BookmarkStart>().FirstOrDefault(); if (bookmarkStart == null) continue; if (bookmarkStart.ColumnFirst == null) continue; //针对每一个区域,先读取表头的控件,同时将控件加入黑名单 DCTDataColumnCollection collection = new DCTDataColumnCollection(titleRow); ignoreControls.AddRange(collection.CoveredControlIds); //遍历区域中的每一行,生成复杂属性 var curRow = titleRow.NextSibling<TableRow>(); DCTComplexProperty cp = new DCTComplexProperty(); cp.TagID = bookmarkStart.Name; while (null != curRow && (!curRow.Descendants<BookmarkEnd>().Any(o => o.Id == bookmarkStart.Id))) { DCTWordDataObject childWdo = new DCTWordDataObject(); DCTDataRow newDataRow = DCTDataRow.FromTableRow(curRow, collection); if (!newDataRow.IsEmpty) childWdo.PropertyCollection.AddRange<DCTSimpleProperty>(newDataRow.ToSimpleProperties()); curRow = curRow.NextSibling<TableRow>(); cp.DataObjects.Add(childWdo); } wdo.PropertyCollection.Add(cp); } }
public void FillDocTest() { DCTWordDataObject wdo = new DCTWordDataObject(); wdo.PropertyCollection.Add(new DCTSimpleProperty() { TagID = "姓名", Value = "张三", FormatString = "", IsReadOnly = true }); wdo.PropertyCollection.Add(new DCTSimpleProperty() { TagID = "年龄", Value = 1234, FormatString = "###,###" }); wdo.PropertyCollection.Add(new DCTComplexProperty() { TagID = "学历信息", DataObjects = new DCTWordDataObjectCollection() { new DCTWordDataObject() { PropertyCollection = new DCTDataPropertyCollection() { new DCTSimpleProperty() { TagID = "毕业日期", Value = DateTime.Now, FormatString = "yyyy-MM-dd", Type = typeof(DateTime).FullName }, new DCTSimpleProperty() { TagID = "毕业院校", Value = "XX1学校", FormatString = "" }, new DCTSimpleProperty() { TagID = "专业", Value = "XX1专业", FormatString = "" } } }, new DCTWordDataObject() { PropertyCollection = new DCTDataPropertyCollection() { new DCTSimpleProperty() { TagID = "毕业日期", Value = DateTime.Now, FormatString = "yyyy-MM-dd" }, new DCTSimpleProperty() { TagID = "毕业院校", Value = "XX2学校", FormatString = "" }, new DCTSimpleProperty() { TagID = "专业", Value = "XX2专业", FormatString = "" } } }, new DCTWordDataObject() { PropertyCollection = new DCTDataPropertyCollection() { new DCTSimpleProperty() { TagID = "毕业日期", Value = DateTime.Now, FormatString = "yyyy-MM-dd" }, new DCTSimpleProperty() { TagID = "毕业院校", Value = "XX3学校", FormatString = "" }, new DCTSimpleProperty() { TagID = "专业", Value = "XX3专业", FormatString = "" } } } } }); string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); DirectoryInfo rootDirectory = new DirectoryInfo(path); byte[] templateBinary = File.ReadAllBytes(Path.Combine(rootDirectory.Parent.Parent.FullName, "Table.docx")); File.WriteAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FilleTemp.docx"), WordEntry.GenerateDocument(templateBinary, wdo)); }