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;
}
///
///
/// 设置字体 默认宋体 10 黑色 加粗
///
/// 对齐方式
public void SetFont(Aspose.Words.ParagraphAlignment fontalignment)
{
_builder.ParagraphFormat.Alignment = fontalignment;
_builder.SetBuilderFont(_font, _fontSize, _fontcolor, _isBlod);
}
///
/// 设置字体 默认左对齐 宋体 黑色 加粗
///
/// 字体大小
public void SetFont(int fontSize)
{
_builder.ParagraphFormat.Alignment = _fontalignment;
_builder.SetBuilderFont(_font, fontSize, _fontcolor, _isBlod);
}
///
/// 设置字体 默认左对齐 宋体 10 加粗
///
/// 字体颜色
public void SetFont(Color fontcolor)
{
_builder.ParagraphFormat.Alignment = _fontalignment;
_builder.SetBuilderFont(_font, _fontSize, fontcolor, _isBlod);
}
///
/// 设置字体 默认左对齐 宋体 10 黑色
///
/// 是否加粗
public void SetFont(bool isBlod)
{
_builder.ParagraphFormat.Alignment = _fontalignment;
_builder.SetBuilderFont(_font, _fontSize, _fontcolor, isBlod);
}
///
/// 设置字体
///
/// 对齐方式
/// 字体
/// 字体大小
/// 字体颜色
/// 是否加粗
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);
}
///
/// 垂直合并单元格的方式 默认不合并
///
private Aspose.Words.Tables.CellMerge _verticalMerge = Aspose.Words.Tables.CellMerge.None;
///
/// 水平合并单元格的方式 默认不合并
///
private Aspose.Words.Tables.CellMerge _horizontalMerge = Aspose.Words.Tables.CellMerge.None;
///
/// 单元格边框格式 默认单线
///
private LineStyle _lineStyle = LineStyle.Single;
///
/// 单元格对齐方式 默认居中
///
private ParagraphAlignment _alignment = ParagraphAlignment.Center;
///
/// 单元格上边框样式
///
private LineStyle _top = LineStyle.Single;
///
/// 单元格下边框样式
///
private LineStyle _bottom = LineStyle.Single;
///
/// 单元格左边框样式
///
private LineStyle _left = LineStyle.Single;
///
/// 单元格右边框样式
///
private LineStyle _right = LineStyle.Single;
///
/// 单元格宽度 默认-1 自动计算
///
private double _width = -1;
///
/// 是否加粗 默认不加粗
///
private bool _isBold = false;
///
/// 背景色的RGB值,格式为"R,G,B"
///
private string _rgbstr = "";
///
/// 文本是否斜体 默认否
///
private bool _isItalic = false;
///
/// 字体 默认宋体
///
private string _font = "宋体";
///
/// 字体大小 默认10
///
private int _fontSize = 10;
///
/// 字体是否加粗 默认加粗
///
private bool _isBlod = false;
///
/// 字体颜色 默认黑
///
private Color _fontcolor = Color.Black;
///
/// 文字对齐方式 默认左
///
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; }
}
///
/// 构建文字
///
///
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);
}
///
/// 添加空行
///
///
public void AddBlankLine()
{
_builder.Writeln();
}
///
/// 构建文字
///
///
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);
}
///
/// 构建图片
///
/// 文字内容
/// 图片地址
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("");
}
///
/// 构建泵曲线图片
///
/// 文字内容
/// 图片地址
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("");
}
///
/// 构建图片 (同一行多个图片)
///
/// 图片地址列表
/// 图片高度
/// 图片宽度
/// 图片之间的间距
public void structureImagesInRow(List 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(""); // 在所有图片插入后添加一个换行
}
/* ///
/// 构建图片
///
/// 文字内容
/// 图片地址
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("");
}
}
}
*/ ///
/// 构建图片
///
///
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("");
}
///
/// 创建单元格(带背景颜色)
///
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();
}
}
}