duheng
2024-12-24 058adb7d84fa39cf4fc5ff1ab6ea337db6c21423
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using Aspose.Words;
using SkiaSharp;
using System.Drawing;
using Yw.DiagramFile;
using Yw.DiagramFile.Image;
 
namespace HStation.ReportFile
{
    public class SimulationWordReportHelper
    {
        private Aspose.Words.DocumentBuilder _builder = null;
 
        public SimulationWordReportHelper(Aspose.Words.DocumentBuilder builder)
        {
            this._builder = builder;
        }
 
        /// <summary>
        ///
        /// 设置字体 默认宋体 10 黑色 加粗
        /// </summary>
        /// <param name="fontalignment">对齐方式</param>
        public void SetFont(Aspose.Words.ParagraphAlignment fontalignment)
        {
            _builder.ParagraphFormat.Alignment = fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
        }
 
        /// <summary>
        /// 设置字体 默认左对齐 宋体 黑色 加粗
        /// </summary>
        /// <param name="fontSize">字体大小</param>
        public void SetFont(int fontSize)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, fontSize, _fontcolor, _isBlod);
        }
 
        /// <summary>
        /// 设置字体 默认左对齐 宋体 10 加粗
        /// </summary>
        /// <param name="fontcolor">字体颜色</param>
        public void SetFont(Color fontcolor)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, fontcolor, _isBlod);
        }
 
        /// <summary>
        /// 设置字体 默认左对齐 宋体 10 黑色
        /// </summary>
        /// <param name="isBlod">是否加粗</param>
        public void SetFont(bool isBlod)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, isBlod);
        }
 
        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="fontalignment">对齐方式</param>
        /// <param name="font">字体</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="isBlod">是否加粗</param>
        public void SetFont(Aspose.Words.ParagraphAlignment fontalignment, string font, int fontSize, Color fontcolor, bool isBlod)
        {
            _builder.ParagraphFormat.Alignment = fontalignment;
            _builder.SetBuilderFont(font, fontSize, fontcolor, isBlod);
        }
 
        /// <summary>
        /// 垂直合并单元格的方式 默认不合并
        /// </summary>
        private Aspose.Words.Tables.CellMerge _verticalMerge = Aspose.Words.Tables.CellMerge.None;
 
        /// <summary>
        /// 水平合并单元格的方式 默认不合并
        /// </summary>
        private Aspose.Words.Tables.CellMerge _horizontalMerge = Aspose.Words.Tables.CellMerge.None;
 
        /// <summary>
        /// 单元格边框格式 默认单线
        /// </summary>
        private LineStyle _lineStyle = LineStyle.Single;
 
        /// <summary>
        /// 单元格对齐方式 默认居中
        /// </summary>
        private ParagraphAlignment _alignment = ParagraphAlignment.Center;
 
        /// <summary>
        /// 单元格上边框样式
        /// </summary>
        private LineStyle _top = LineStyle.Single;
 
        /// <summary>
        /// 单元格下边框样式
        /// </summary>
        private LineStyle _bottom = LineStyle.Single;
 
        /// <summary>
        /// 单元格左边框样式
        /// </summary>
        private LineStyle _left = LineStyle.Single;
 
        /// <summary>
        /// 单元格右边框样式
        /// </summary>
        private LineStyle _right = LineStyle.Single;
 
        /// <summary>
        /// 单元格宽度 默认-1 自动计算
        /// </summary>
        private double _width = -1;
 
        /// <summary>
        /// 是否加粗 默认不加粗
        /// </summary>
        private bool _isBold = false;
 
        /// <summary>
        /// 背景色的RGB值,格式为"R,G,B"
        /// </summary>
        private string _rgbstr = "";
 
        /// <summary>
        /// 文本是否斜体 默认否
        /// </summary>
        private bool _isItalic = false;
 
        /// <summary>
        /// 字体 默认宋体
        /// </summary>
        private string _font = "宋体";
 
        /// <summary>
        /// 字体大小 默认10
        /// </summary>
        private int _fontSize = 10;
 
        /// <summary>
        /// 字体是否加粗 默认加粗
        /// </summary>
        private bool _isBlod = false;
 
        /// <summary>
        /// 字体颜色 默认黑
        /// </summary>
        private Color _fontcolor = Color.Black;
 
        /// <summary>
        /// 文字对齐方式 默认左
        /// </summary>
        private Aspose.Words.ParagraphAlignment _fontalignment = ParagraphAlignment.Left;
 
        public Aspose.Words.ParagraphAlignment fontalignment
        {
            get { return _fontalignment; }
            set { _fontalignment = value; }
        }
 
        public Color fontcolor
        {
            get { return _fontcolor; }
            set { _fontcolor = value; }
        }
 
        public bool isblod
        {
            get { return _isBlod; }
            set { _isBlod = value; }
        }
 
        public int fontsize
        {
            get { return _fontSize; }
            set { _fontSize = value; }
        }
 
        public string font
        {
            get { return _font; }
            set { _font = value; }
        }
 
        public Aspose.Words.Tables.CellMerge verticalMerge
        {
            get { return _verticalMerge; }
            set { _verticalMerge = value; }
        }
 
        public Aspose.Words.Tables.CellMerge horizontalMerge
        {
            get { return _horizontalMerge; }
            set { _horizontalMerge = value; }
        }
 
        public LineStyle lineStyle
        {
            get { return _lineStyle; }
            set { _lineStyle = value; }
        }
 
        public ParagraphAlignment alignment
        {
            get { return _alignment; }
            set { _alignment = value; }
        }
 
        public LineStyle top
        {
            get { return _top; }
            set { _top = value; }
        }
 
        public LineStyle bottom
        {
            get { return _bottom; }
            set { _bottom = value; }
        }
 
        public LineStyle left
        {
            get { return _left; }
            set { _left = value; }
        }
 
        public LineStyle right
        {
            get { return _right; }
            set { _right = value; }
        }
 
        public double width
        {
            get { return _width; }
            set { _width = value; }
        }
 
        public bool isBold
        {
            get { return _isBold; }
            set { _isBold = value; }
        }
 
        public string rgbstr
        {
            get { return _rgbstr; }
            set { _rgbstr = value; }
        }
 
        public bool isItalic
        {
            get { return _isItalic; }
            set { _isItalic = value; }
        }
 
        /// <summary>
        /// 构建文字
        /// </summary>
        /// <param name="content"></param>
        public void structureLeft20Text(string content)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
            _builder.ParagraphFormat.SpaceBefore = 5;
            _builder.ParagraphFormat.FirstLineIndent = 30;
            _builder.Writeln(content);
        }
 
        /// <summary>
        /// 添加空行
        /// </summary>
        /// <param name="content"></param>
        public void AddBlankLine()
        {
            _builder.Writeln();
        }
 
        /// <summary>
        /// 构建文字
        /// </summary>
        /// <param name="content"></param>
        public void structureText(string content)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
            _builder.ParagraphFormat.SpaceBefore = 5;
            _builder.ParagraphFormat.LeftIndent = 0;
            _builder.Writeln(content);
        }
 
        /// <summary>
        /// 构建图片
        /// </summary>
        /// <param name="content">文字内容</param>
        /// <param name="address">图片地址</param>
        public void structureTextAndImage(string address, int height, int wide)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
            if (!System.IO.File.Exists(address))
            {
                return;
            }
            _builder.InsertImage(address, wide, height);
            _builder.Writeln("");
        }
 
        /// <summary>
        /// 构建泵曲线图片
        /// </summary>
        /// <param name="content">文字内容</param>
        /// <param name="address">图片地址</param>
        public void BuildingPumpCurvePicture(PumpChartViewModel pumpChartViewModel, double wide, double height)
        {
            if (pumpChartViewModel == null)
                return;
            var pumpDiagram = new PumpChart();
            var bitmap = pumpDiagram.Create(pumpChartViewModel);
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
            if (bitmap == null)
            {
                return;
            }
            _builder.InsertImage(bitmap, wide, height);
            _builder.Writeln("");
        }
 
        /// <summary>
        /// 构建图片  (同一行多个图片)
        /// </summary>
        /// <param name="addresses">图片地址列表</param>
        /// <param name="height">图片高度</param>
        /// <param name="width">图片宽度</param>
        /// <param name="spacing">图片之间的间距</param>
        public void structureImagesInRow(List<string> addresses, int height, int width)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
 
            foreach (var address in addresses)
            {
                if (!System.IO.File.Exists(address))
                {
                    continue; // 如果文件不存在,则跳过该图片
                }
 
                _builder.InsertImage(address, width, height);
            }
 
            _builder.Writeln(""); // 在所有图片插入后添加一个换行
        }
 
        /*        /// <summary>
                /// 构建图片
                /// </summary>
                /// <param name="content">文字内容</param>
                /// <param name="BitMap">图片地址</param>
                public void structureTextAndImage(Bitmap BitMap, int height, int wide)
                {
                    using (var stream = new MemoryStream())
                    {
                        // 将System.Drawing.Bitmap保存到内存流中
                        BitMap.Save(stream, ImageFormat.Png);
                        stream.Position = 0;
                        // 使用SKManagedStream从内存流中解码出SKBitmap
                        using (var skManagedStream = new SKManagedStream(stream))
                        {
                            var skBitmap = SKBitmap.Decode(skManagedStream);
                            _builder.ParagraphFormat.Alignment = _fontalignment;
                            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
                            _builder.InsertImage(skBitmap, wide, height);
                            _builder.Writeln("");
                        }
                    }
                }
 
        */        /// <summary>
 
                  /// 构建图片
                  /// </summary>
                  /// <param name="address"></param>
        public void structureTextAndImage(string address)
        {
            _builder.ParagraphFormat.Alignment = _fontalignment;
            _builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
            if (!System.IO.File.Exists(address))
            {
                return;
            }
            _builder.InsertImage(address);
            _builder.Writeln("");
        }
 
        /// <summary>
        /// 创建单元格(带背景颜色)
        /// </summary>
        public void structureCell(string content)
        {
            _builder.ParagraphFormat.FirstLineIndent = 0;
            _builder.ParagraphFormat.LeftIndent = 2;
            var cell = _builder.InsertCell();
            //_builder.InsertParagraph(); // 添加一个新段落
            //_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // 设置段落垂直居中
            _builder.CellFormat.VerticalMerge = _verticalMerge;
            _builder.CellFormat.HorizontalMerge = _horizontalMerge;
            //builder.CellFormat.Borders.LineStyle = lineStyle;
            _builder.CellFormat.Borders[BorderType.Bottom].LineStyle = _bottom;
            _builder.CellFormat.TopPadding = 1;
            _builder.CellFormat.Borders[BorderType.Top].LineStyle = _top;
            _builder.CellFormat.Borders[BorderType.Left].LineStyle = _left;
            _builder.CellFormat.Borders[BorderType.Right].LineStyle = _right;
            _builder.ParagraphFormat.Alignment = _alignment;
            _builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
            _builder.CellFormat.Width = _width;
            //builder.CellFormat.Borders.LineStyle = lineStyle;
            _builder.Bold = _isBold;
            _builder.Italic = _isItalic;
            //builder.CellFormat.Shading.ForegroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);//设置单元格背景色
            if (!string.IsNullOrEmpty(_rgbstr))
            {
                var rgblist = _rgbstr.Split(',').ToList();
                if (rgblist.Count == 3)
                    _builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(Convert.ToInt32(rgblist[0]), Convert.ToInt32(rgblist[1]), Convert.ToInt32(rgblist[2]));//设置单元格背景色
            }
 
            //builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(red, green, blue);//设置单元格背景色
            //builder.Underline = isUnderline;
            if (content == null)
            {
                _builder.Write(""); // 在新段落中写入内容
                return;
            }
 
            _builder.Write(content); // 在新段落中写入内容
            //cell.FirstParagraph.Remove();
        }
    }
}