public DocStructure(BlockType blockType, decimal structureID, DocMaster docMaster) { //根据structureID从数据库取出相关数据 //BlockType blockType, string docName, decimal fileID, string json this.BlockType = blockType; this.structureID = structureID; //this.DocName = docName; //this.FileID = fileID; //this.Json = json; this.docMaster = docMaster; string error = ""; try { var structure = BuildWordInstance.GetStructure((int)structureID); if (structure != null) { error = "构件'" + structure.STRUCTURE_NAME + "'文件不存在"; this.DocName = structure.STRUCTURE_NAME; this.FileID = structure.FILE_ID.Value; this.Json = structure.SET_CONTENT; this.NewSection = (int)structure.IS_NEW_SECTION == 1; this.buildWord = new BuildWord(FileServerHelper.GetFileStream(this.FileID)); //this.InitLabel(null); } } catch { throw new Exception(error); } /* * if (isInitLabel && this.LabelList != null && this.LabelList.Count > 0) * this.InitLabel(new Dictionary<string, string>()); */ }
/// <summary> /// 获取图像流 /// </summary> /// <returns>图像流</returns> public Stream GetStream() { decimal fileID = this.GetFileID(); return(FileServerHelper.GetFileStream(fileID)); }
/// <summary> /// 初始化数据 /// </summary> /// <param name="jsonStructure">构件结构</param> /// <param name="inputParams">输入参数</param> private void InitData(string jsonStructure, Dictionary<string, string> inputParams) { try { MotherSetDTO motherSet = BuildWordInstance.GetMotherSet((int)this.masterID); if (motherSet != null) { Dictionary<BlockType, List<Structure>> structureCofing; this.FileID = motherSet.FILE_ID.Value; this.DocTemplateType = new DocTemplateType(motherSet.TEMPLATE_TYPE.Value, this.InstanceID, inputParams); var fileStream = FileServerHelper.GetFileStream(motherSet.FILE_ID.Value); if (fileStream != null) this.buildWord = new BuildWord(fileStream); if (jsonStructure == null) { structureCofing = this.GetStructureDictionary(motherSet.SET_CONTENT); } else { structureCofing = this.GetStructureDictionary(jsonStructure); } //获取构建信息 this.StructureInfoList = this.GetStructureInfoList(structureCofing); } //处理返回结果 if (!string.IsNullOrEmpty(this.resultJson)) { JArray ary = JArray.Parse(this.resultJson); decimal id; StructureType type; foreach (var v in ary) { id = v["ID"].Value<decimal>(); type = (StructureType)Enum.Parse(typeof(StructureType), v["StructureType"].Value<string>()); if (!this.InputValue.ContainsKey(v["LabelName"].Value<string>())) { this.InputValue.Add(v["LabelName"].Value<string>(), v["Value"].Value<string>()); } } } // 应用替换值 if (this.InputValue != null && this.InputValue.Count > 0) { this.LabelList.ForEach(label => { if (label is TextLabel) { var textLabel = label as TextLabel; var input = this.InputValue.FirstOrDefault(t => t.Key == label.LabelName); if (!string.IsNullOrEmpty(input.Key)) { textLabel.IsInput = true; textLabel.Value = input.Value; } } }); } //处理条件标签 // 1.这种判断有误,当条件标签的条件没有@标签的时候,内容不会被替换 // 2.条件标签应该都算outside modify by huzy 2016.4.5 var inside = this.LabelList.Where(t => !t.RelateValue.Contains('@')).ToList(); var outside = this.LabelList.Where(t => t.RelateValue.Contains('@')).ToList(); var conditionS = this.LabelList.Where(t => t is ConditionLabel).ToList(); foreach (var c in conditionS) { inside.Remove(c); if (!outside.Contains(c)) outside.Add(c); } var tmpList = new List<BaseLabel>(); while (true) { bool isBreak = true; foreach (var oItem in outside) { foreach (var iItem in inside) { if (oItem.RelateValue.IndexOf(iItem.LabelName) > 0) { if (iItem is TextLabel) { var textLabel = iItem as TextLabel; //var value = string.IsNullOrEmpty(textLabel.RelateValue) ? textLabel.GetValue() : textLabel.RelateValue; var value = textLabel.GetValue(); if (!textLabel.IsAfterCompute) value = textLabel.InnerValue; bool pass = oItem.Replace(iItem.LabelName, value); if (!tmpList.Contains(oItem) && pass) tmpList.Add(oItem); if (isBreak && pass) isBreak = false; } else if (iItem is ConditionLabel) //条件引用条件标签 { var conditionLabel = iItem as ConditionLabel; BaseLabel baseLabel = conditionLabel.ConditionJudgment(); if (baseLabel is TextLabel) { var textLabel = baseLabel as TextLabel; var value = textLabel.GetValue(); bool pass = oItem.Replace(iItem.LabelName, value); if (!tmpList.Contains(oItem) && pass) tmpList.Add(oItem); if (isBreak && pass) isBreak = false; } } } } } foreach (var item in tmpList) { inside.Add(item); outside.Remove(item); } tmpList.Clear(); if (isBreak) break; } //处理构建里无匹配的标签 匹配常量中的书名号《》 this.LabelList.ForEach(label => { if (label is ConditionLabel) { var cl = label as ConditionLabel; cl.LabelList.ForEach(l => { string key = DocHelper.PatternString(l.Condition); var findLable = inside.FirstOrDefault(i => i.LabelName == key); if (findLable != null && findLable is TextLabel) { try { var textLabel = findLable as TextLabel; var value = string.IsNullOrEmpty(textLabel.RelateValue) ? textLabel.GetValue() : textLabel.RelateValue; l.Condition = l.Condition.Replace("@" + key, value); } catch { } } if (DocHelper.CalcByJs(l.Condition) && l.BaseLabel is TextLabel) { var tl = l.BaseLabel as TextLabel; tl.ReplaceWithConst(inside); } }); } if (label is TextLabel) { var tl = label as TextLabel; tl.ReplaceWithConst(inside); } }); } catch (Exception ex) { throw ex; } }
/// <summary> /// 插入图像 /// </summary> /// <param name="bookmark">书签名称</param> /// <param name="fileIds">文件服务器文件ID数组</param> public void InsertImage(string bookmark, string[,] fileIds) { if (this.doc == null || fileIds.GetLength(0) == 0) { return; } Bookmark mark = this.doc.Range.Bookmarks[bookmark]; decimal fileId = 0; if (mark != null) { mark.Text = string.Empty; var builder = new DocumentBuilder(this.doc); builder.MoveTo(mark.BookmarkStart); var tableNode = this.FindParentNode(builder.CurrentNode, NodeType.Table) as Table; var cellNode = this.FindParentNode(builder.CurrentNode, NodeType.Cell) as Cell; double fixedWidth = 420, rowHeight = 0; int startCellIndex = 0; int tableIndex = -1; int rowCount = 0, colCount = 0; if (tableNode != null && cellNode != null) { var table = tableNode as Table; tableIndex = this.GetTableIndex(table); rowCount = table.Rows.Count; colCount = table.FirstRow.Cells.Count; bool isBreak = false; // 查找书签所在单元格的序号 for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { for (int colIndex = 0; colIndex < colCount; colIndex++) { var cell = table.Rows[rowIndex].Cells[colIndex]; if (cellNode.Equals(cell)) { isBreak = true; break; } startCellIndex++; } if (isBreak) { break; } } } for (int i = 0; i < fileIds.GetLength(0); i++) { if (decimal.TryParse(fileIds[i, 0], out fileId)) { if (tableIndex != -1 && rowCount > 0 && colCount > 0) { if (i + 1 + startCellIndex > rowCount * colCount) { break; } int rowIndex = (i + startCellIndex) / colCount; int colIndex = (i + startCellIndex) % colCount; builder.MoveToCell(tableIndex, rowIndex, colIndex, 0); var cell = this.FindParentNode(builder.CurrentParagraph, NodeType.Cell) as Cell; fixedWidth = cell.CellFormat.Width; rowHeight = cell.ParentRow.RowFormat.Height; } var imageStream = FileServerHelper.GetFileStream(fileId); if (imageStream.Length > 0) { this.InsertImage(builder, imageStream, fixedWidth, rowHeight); } } } } }