private void InsertSummaryAndPictureTable(string BookmarkStartName, int CompressImageFlag, List <DamageSummary> listDamageSummary, double ImageWidth, double ImageHeight, bool CommentColumnInsertTable) { var builder = new DocumentBuilder(_doc); var fieldStyleRefBuilder = new FieldBuilder(FieldType.FieldStyleRef); fieldStyleRefBuilder.AddArgument(1); fieldStyleRefBuilder.AddSwitch(@"\s"); var pictureFieldSequenceBuilder = new FieldBuilder(FieldType.FieldSequence); pictureFieldSequenceBuilder.AddArgument("图"); pictureFieldSequenceBuilder.AddSwitch(@"\*", "ARABIC"); pictureFieldSequenceBuilder.AddSwitch(@"\s", "1"); var tableFieldSequenceBuilder = new FieldBuilder(FieldType.FieldSequence); tableFieldSequenceBuilder.AddArgument("表"); tableFieldSequenceBuilder.AddSwitch(@"\*", "ARABIC"); tableFieldSequenceBuilder.AddSwitch(@"\s", "1"); //_Refxx的书签不会在word的“插入”=>“书签”中显示 FieldRef pictureRefField; //模板在书签位置格式调整 //1、单倍行距 //2、首行不缩进 var bookmark = _doc.Range.Bookmarks[BookmarkStartName]; builder.MoveTo(bookmark.BookmarkStart); builder.ParagraphFormat.Style = _doc.Styles[_generateReportSettings.ComboBoxReportTemplates.DocStyleOfMainText];//_doc.Styles[App.DocStyleOfMainText]; //TODO:考虑一下具体的缩进值 //builder.ParagraphFormat.FirstLineIndent = 8; TableCellWidth tableCellWidth; //要求:至少要有2张照片 if (BookmarkStartName == BridgeDeckBookmarkStartName) { tableCellWidth = _generateReportSettings.BridgeDeckTableCellWidth; builder.Write($"桥面系{_generateReportSettings.InspectionString}结果详见"); } else if (BookmarkStartName == SuperSpaceBookmarkStartName) { tableCellWidth = _generateReportSettings.SuperSpaceTableCellWidth; builder.Write($"上部结构{_generateReportSettings.InspectionString}结果详见"); } else { tableCellWidth = _generateReportSettings.SubSpaceTableCellWidth; builder.Write($"下部结构{_generateReportSettings.InspectionString}结果详见"); } int firstIndex = 0; int lastIndex = listDamageSummary.Count - 1; //查找第一张 while (listDamageSummary[firstIndex].PictureCounts == 0 && firstIndex < listDamageSummary.Count - 1) { firstIndex++; } //查找最后一张 while (listDamageSummary[lastIndex].PictureCounts == 0 && lastIndex > 0) { lastIndex--; } //TODO:考虑表格第1行和最后1行可能没有照片 pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[0].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("~"); pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary.Last().FirstPictureBookmarkIndex + listDamageSummary.Last().PictureCounts - 1}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("。"); builder.Writeln(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Write("表 "); var r1 = new Run(_doc, ""); builder.InsertNode(r1); fieldStyleRefBuilder.BuildAndInsert(r1); builder.Write("-"); var r2 = new Run(_doc, ""); builder.InsertNode(r2); tableFieldSequenceBuilder.BuildAndInsert(r2); builder.Write(" "); //写入表头 if (BookmarkStartName == BridgeDeckBookmarkStartName) { builder.Write($"桥面系{_generateReportSettings.InspectionString}结果汇总表"); } else if (BookmarkStartName == SuperSpaceBookmarkStartName) { builder.Write($"上部结构{_generateReportSettings.InspectionString}结果汇总表"); } else { builder.Write($"下部结构{_generateReportSettings.InspectionString}结果汇总表"); } builder.ParagraphFormat.Style = _doc.Styles[_generateReportSettings.ComboBoxReportTemplates.DocStyleOfTable]; builder.Writeln(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //病害汇总表格 var summaryTable = builder.StartTable(); builder.InsertCell(); CellFormat cellFormat = builder.CellFormat; if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.No; } builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; builder.Font.Bold = true; builder.Write("序号"); builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.Position; } builder.Write("位置"); builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.Component; } BridgePart bridgePart; if (BookmarkStartName == BridgeDeckBookmarkStartName) { builder.Write("要素"); bridgePart = BridgePart.BridgeDeck; } else if (BookmarkStartName == SuperSpaceBookmarkStartName) { builder.Write("构件类型"); bridgePart = BridgePart.SuperSpace; } else { builder.Write("构件类型"); bridgePart = BridgePart.SubSpace; } builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.Damage; } builder.Write("缺损类型"); builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.DamageDescription; } builder.Write("缺损描述"); builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.PictureNo; } builder.Write("图示编号"); if (CommentColumnInsertTable) { builder.InsertCell(); if (_generateReportSettings.CustomTableCellWidth) { cellFormat.Width = tableCellWidth.Comment; } builder.Write("备注"); } builder.Font.Bold = false; builder.EndRow(); int sn = 1; //序号 for (int i = 0; i < listDamageSummary.Count; i++) { //如果完整结构不插入汇总表并且部件名称为"/" if (_generateReportSettings.IntactStructNoInsertSummaryTable && listDamageSummary[i].GetDamageName(bridgePart).Contains("/")) { continue; } builder.InsertCell(); builder.Write($"{sn}"); sn++; cellFormat.Width = tableCellWidth.No; builder.InsertCell(); builder.Write($"{listDamageSummary[i].Position}"); cellFormat.Width = tableCellWidth.Position; builder.InsertCell(); builder.Write($"{listDamageSummary[i].GetComponentName(bridgePart)}"); cellFormat.Width = tableCellWidth.Component; builder.InsertCell(); builder.Write($"{listDamageSummary[i].GetDamageName(bridgePart).Replace("m2", "m\u00B2").Replace("m3", "m\u00B3")}"); //\u00B2是2的上标,\u00B3是3的上标 cellFormat.Width = tableCellWidth.Damage; builder.InsertCell(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; builder.Write($"{listDamageSummary[i].DamageDescription.Replace("m2", "m\u00B2").Replace("m3", "m\u00B3")}"); cellFormat.Width = tableCellWidth.DamageDescription; builder.InsertCell(); cellFormat.Width = tableCellWidth.PictureNo; builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; if (listDamageSummary[i].PictureCounts == 0) { if (string.IsNullOrWhiteSpace(listDamageSummary[i].CustomPictureNo)) { builder.Write("/"); } else { builder.Write(listDamageSummary[i].CustomPictureNo); } } else if (listDamageSummary[i].PictureCounts == 1) { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; } else if (listDamageSummary[i].PictureCounts == 2) { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("\r\n"); pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + 1}", "", ""); pictureRefField.InsertHyperlink = true; } else //图片大于2张 { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("\r\n~\r\n"); pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + listDamageSummary[i].PictureCounts - 1}", "", ""); pictureRefField.InsertHyperlink = true; } if (CommentColumnInsertTable) { builder.InsertCell(); builder.Write($"{listDamageSummary[i].Comment}"); cellFormat.Width = tableCellWidth.Comment; } builder.EndRow(); } builder.EndTable(); if (_generateReportSettings.CustomTableCellWidth) { summaryTable.AutoFit(AutoFitBehavior.FixedColumnWidths); } //TODO:用建造者模式重构 MergeDamageColumn(listDamageSummary, summaryTable); MergeComponentColumn(listDamageSummary, summaryTable); MergeTheSameColumn(listDamageSummary, summaryTable, 1); // Set a green border around the table but not inside. summaryTable.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Black, true); if (BookmarkStartName == BridgeDeckBookmarkStartName && _generateReportSettings.DeletePositionInBridgeDeckCheckBox == true) { Column column = Column.FromIndex(summaryTable, 1); column.Remove(); } //根据内容自动调整表格 //summaryTable.AutoFit(AutoFitBehavior.AutoFitToContents); builder.ParagraphFormat.Style = _doc.Styles[_generateReportSettings.ComboBoxReportTemplates.DocStyleOfPicture]; //注意:图片段落格式设置采用单倍行距 builder.Writeln(); //病害图片插入表格 //Reference: //https://github.com/aspose-words/Aspose.Words-for-.NET/blob/f84af3bfbf2a1f818551064a0912b106e848b2ad/Examples/CSharp/Programming-Documents/Bookmarks/BookmarkTable.cs var pictureTable = builder.StartTable(); //病害详细图片 //计算总的图片数量 int totalPictureCounts = 0; for (int i = 0; i < listDamageSummary.Count; i++) { totalPictureCounts += listDamageSummary[i].PictureCounts; } int tableTotalRows = 2 * ((totalPictureCounts + 1) / 2); //表格总行数 int tableTotalCols = 2; for (int i = 0; i < tableTotalRows; i++) { for (int j = 0; j < tableTotalCols; j++) { builder.InsertCell(); cellFormat.Width = App.TablePictureWidth; } builder.EndRow(); } builder.EndTable(); //builder.MoveTo(table.Rows[1 + 1].Cells[0].FirstParagraph); int curr = 0; //当前已插入图片数 string[] dirs; string pictureFileName; for (int i = 0; i < listDamageSummary.Count; i++) { if (listDamageSummary[i].PictureCounts > 0) //有图片则插入 { string[] p = listDamageSummary[i].PictureNo.Split(App.PictureNoSplitSymbol); for (int j = 0; j < p.Length; j++) { builder.MoveTo(pictureTable.Rows[2 * (int)(curr / 2)].Cells[(curr) % 2].FirstParagraph); //var dirs = Directory.GetFiles(@"Pictures/", $"*{p[j]}*"); //结果含有路径 if (Directory.GetFiles($@"{App.PicturesFolder}/", $"*{ p[j]}.*").Length != 0) { pictureFileName = FileService.GetFileName(@"Pictures", p[j]); } else { pictureFileName = FileService.GetFileName($"{App.PicturesOutFolder}", p[j]); } //TODO:检测文件是否重复,若重复不需要再压缩(MD5校验) //(暂时用文件名校验) if (!File.Exists($"PicturesOut/{Path.GetFileName(pictureFileName)}")) { _ = ImageServices.CompressImage($"{pictureFileName}", $"PicturesOut/{Path.GetFileName(pictureFileName)}", CompressImageFlag, _generateReportSettings.ImageSettings.MaxCompressSize); //只取查找到的第1个文件,TODO:UI提示 } _ = builder.InsertImage($"PicturesOut/{Path.GetFileName(pictureFileName)}", RelativeHorizontalPosition.Margin, 0, RelativeVerticalPosition.Margin, 0, ImageWidth, ImageHeight, WrapType.Inline); builder.MoveTo(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[(curr) % 2].FirstParagraph); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.StartBookmark($"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + j}"); builder.Write("图 "); _ = fieldStyleRefBuilder.BuildAndInsert(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[curr % 2].Paragraphs[0]); builder.Write("-"); _ = pictureFieldSequenceBuilder.BuildAndInsert(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[curr % 2].Paragraphs[0]); builder.EndBookmark($"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + j}"); if (listDamageSummary[i].PictureCounts > 1) { if (!listDamageSummary[i].DamageDescriptionInPicture.Contains("$")) { builder.Write($" {listDamageSummary[i].DamageDescriptionInPicture}-{j + 1}"); } else { var damageDescriptionInPictureArray = listDamageSummary[i].DamageDescriptionInPicture.Split('$'); builder.Write($" {damageDescriptionInPictureArray[j]}"); } } else { builder.Write($" {listDamageSummary[i].DamageDescriptionInPicture}"); } curr++; } } } pictureTable.ClearBorders(); }
private void InsertSummaryAndPictureTable(string BookmarkStartName, int CompressImageFlag, List <DamageSummary> listDamageSummary, double ImageWidth, double ImageHeight, bool CommentColumnInsertTable) { var builder = new DocumentBuilder(_doc); var fieldStyleRefBuilder = new FieldBuilder(FieldType.FieldStyleRef); fieldStyleRefBuilder.AddArgument(1); fieldStyleRefBuilder.AddSwitch(@"\s"); var pictureFieldSequenceBuilder = new FieldBuilder(FieldType.FieldSequence); pictureFieldSequenceBuilder.AddArgument("图"); pictureFieldSequenceBuilder.AddSwitch(@"\*", "ARABIC"); pictureFieldSequenceBuilder.AddSwitch(@"\s", "1"); var tableFieldSequenceBuilder = new FieldBuilder(FieldType.FieldSequence); tableFieldSequenceBuilder.AddArgument("表"); tableFieldSequenceBuilder.AddSwitch(@"\*", "ARABIC"); tableFieldSequenceBuilder.AddSwitch(@"\s", "1"); //_Refxx的书签不会在word的“插入”=>“书签”中显示 FieldRef pictureRefField; //模板在书签位置格式调整 //1、单倍行距 //2、首行不缩进 var bookmark = _doc.Range.Bookmarks[BookmarkStartName]; builder.MoveTo(bookmark.BookmarkStart); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Write("表 "); var r1 = new Run(_doc, ""); builder.InsertNode(r1); fieldStyleRefBuilder.BuildAndInsert(r1); builder.Write("-"); var r2 = new Run(_doc, ""); builder.InsertNode(r2); tableFieldSequenceBuilder.BuildAndInsert(r2); builder.Write(" "); //写入表头 if (BookmarkStartName == BridgeDeckBookmarkStartName) { builder.Write("桥面系检查结果汇总表"); } else if (BookmarkStartName == SuperSpaceBookmarkStartName) { builder.Write("上部结构检查结果汇总表"); } else { builder.Write("下部结构检查结果汇总表"); } builder.Writeln(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //病害汇总表格 var summaryTable = builder.StartTable(); builder.InsertCell(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Font.Bold = true; builder.Write("序号"); builder.InsertCell(); builder.Write("位置"); builder.InsertCell(); BridgePart bridgePart; if (BookmarkStartName == BridgeDeckBookmarkStartName) { builder.Write("要素"); bridgePart = BridgePart.BridgeDeck; } else if (BookmarkStartName == SuperSpaceBookmarkStartName) { builder.Write("构件类型"); bridgePart = BridgePart.SuperSpace; } else { builder.Write("构件类型"); bridgePart = BridgePart.SubSpace; } builder.InsertCell(); builder.Write("缺损类型"); builder.InsertCell(); builder.Write("缺损描述"); builder.InsertCell(); builder.Write("图示编号"); if (CommentColumnInsertTable) { builder.InsertCell(); builder.Write("备注"); } builder.Font.Bold = false; builder.EndRow(); for (int i = 0; i < listDamageSummary.Count; i++) { builder.InsertCell(); builder.Write($"{i + 1}"); builder.InsertCell(); builder.Write($"{listDamageSummary[i].Position}"); builder.InsertCell(); builder.Write($"{listDamageSummary[i].GetComponentName(bridgePart)}"); builder.InsertCell(); builder.Write($"{listDamageSummary[i].GetDamageName(bridgePart).Replace("m2", "m\u00B2").Replace("m3", "m\u00B3")}"); //\u00B2是2的上标,\u00B3是3的上标 builder.InsertCell(); builder.Write($"{listDamageSummary[i].DamageDescription.Replace("m2", "m\u00B2").Replace("m3", "m\u00B3")}"); builder.InsertCell(); if (listDamageSummary[i].PictureCounts == 0) { builder.Write("/"); } else if (listDamageSummary[i].PictureCounts == 1) { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; } else if (listDamageSummary[i].PictureCounts == 2) { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("\r\n"); pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + 1}", "", ""); pictureRefField.InsertHyperlink = true; } else //图片大于2张 { pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex}", "", ""); pictureRefField.InsertHyperlink = true; builder.Write("\r\n~\r\n"); pictureRefField = InsertFieldRef(builder, $"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + listDamageSummary[i].PictureCounts - 1}", "", ""); pictureRefField.InsertHyperlink = true; } if (CommentColumnInsertTable) { builder.InsertCell(); builder.Write($"{listDamageSummary[i].Comment}"); } builder.EndRow(); } builder.EndTable(); //TODO:用建造者模式重构 MergeDamageColumn(listDamageSummary, summaryTable); MergeComponentColumn(listDamageSummary, summaryTable); MergeTheSameColumn(listDamageSummary, summaryTable, 1); // Set a green border around the table but not inside. summaryTable.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Black, true); summaryTable.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Black, true); builder.Writeln(); //病害内容插入表格 //Reference: //https://github.com/aspose-words/Aspose.Words-for-.NET/blob/f84af3bfbf2a1f818551064a0912b106e848b2ad/Examples/CSharp/Programming-Documents/Bookmarks/BookmarkTable.cs var pictureTable = builder.StartTable(); //病害详细图片 //计算总的图片数量 int totalPictureCounts = 0; for (int i = 0; i < listDamageSummary.Count; i++) { totalPictureCounts += listDamageSummary[i].PictureCounts; } int tableTotalRows = 2 * ((totalPictureCounts + 1) / 2); //表格总行数 int tableTotalCols = 2; for (int i = 0; i < tableTotalRows; i++) { for (int j = 0; j < tableTotalCols; j++) { builder.InsertCell(); } builder.EndRow(); } builder.EndTable(); //builder.MoveTo(table.Rows[1 + 1].Cells[0].FirstParagraph); int curr = 0; //当前已插入图片数 for (int i = 0; i < listDamageSummary.Count; i++) { if (listDamageSummary[i].PictureCounts > 0) //有图片则插入 { var p = listDamageSummary[i].PictureNo.Split(','); for (int j = 0; j < p.Length; j++) { builder.MoveTo(pictureTable.Rows[2 * (int)(curr / 2)].Cells[(curr) % 2].FirstParagraph); var dirs = Directory.GetFiles(@"Pictures/", $"*{p[j]}*"); //结果含有路径 //TODO:检测文件是否重复,若重复不需要再压缩(MD5校验) //(暂时用文件名校验) if (!File.Exists($"PicturesOut/{Path.GetFileName(dirs[0])}")) { ImageServices.CompressImage($"{dirs[0]}", $"PicturesOut/{Path.GetFileName(dirs[0])}", CompressImageFlag); //只取查找到的第1个文件,TODO:UI提示 } builder.InsertImage($"PicturesOut/{Path.GetFileName(dirs[0])}", RelativeHorizontalPosition.Margin, 0, RelativeVerticalPosition.Margin, 0, ImageWidth, ImageHeight, WrapType.Inline); builder.MoveTo(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[(curr) % 2].FirstParagraph); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.StartBookmark($"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + j}"); builder.Write("图 "); fieldStyleRefBuilder.BuildAndInsert(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[(curr) % 2].Paragraphs[0]); builder.Write("-"); pictureFieldSequenceBuilder.BuildAndInsert(pictureTable.Rows[2 * (int)(curr / 2) + 1].Cells[(curr) % 2].Paragraphs[0]); builder.EndBookmark($"_Ref{listDamageSummary[i].FirstPictureBookmarkIndex + j}"); if (listDamageSummary[i].PictureCounts > 1) { builder.Write($" {listDamageSummary[i].DamageDescriptionInPicture}-{j + 1}"); } else { builder.Write($" {listDamageSummary[i].DamageDescriptionInPicture}"); } curr++; } } } pictureTable.ClearBorders(); }