task 1 complete task2 task2 change test data initialization task3 task2 fix fixes add percents minor fixes
274 lines
7.8 KiB
C#
274 lines
7.8 KiB
C#
using ComponentLibrary1.office_package.HelperEnums;
|
||
using ComponentLibrary1.office_package.HelperModels;
|
||
using MigraDoc.DocumentObjectModel;
|
||
using MigraDoc.Rendering;
|
||
using MigraDoc.DocumentObjectModel.Tables;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Security.Cryptography;
|
||
using System.Reflection;
|
||
using System.Data.Common;
|
||
using MigraDoc.DocumentObjectModel.Shapes;
|
||
using MigraDoc.DocumentObjectModel.Shapes.Charts;
|
||
|
||
namespace ComponentLibrary1.office_package.Implements
|
||
{
|
||
public class SaveToPdf : ISaveToPdf
|
||
{
|
||
private Document? _document;
|
||
private Section? _section;
|
||
private Table? _table;
|
||
|
||
#region константы
|
||
private readonly Unit BORDER_WIDTH = 0.5;
|
||
private readonly Unit COLUMN_WIDTH = Unit.FromCentimeter(3.5);
|
||
private readonly Unit IMAGE_WIDTH = Unit.FromCentimeter(15);
|
||
#endregion
|
||
|
||
#region работа с файлом
|
||
public void CreatePdf(IPdfInfo info)
|
||
{
|
||
_document = new Document();
|
||
DefineStyles(_document);
|
||
_section = _document.AddSection();
|
||
}
|
||
public void SavePdf(IPdfInfo info)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(info.FileName))
|
||
{
|
||
throw new ArgumentNullException("Имя файла не задано");
|
||
}
|
||
|
||
if (_document == null || _section == null)
|
||
{
|
||
throw new ArgumentNullException("Документ не сформирован, сохранять нечего");
|
||
}
|
||
var renderer = new PdfDocumentRenderer(true)
|
||
{
|
||
Document = _document
|
||
};
|
||
renderer.RenderDocument();
|
||
renderer.PdfDocument.Save(info.FileName);
|
||
}
|
||
#endregion
|
||
|
||
#region работа с текстом
|
||
private static ParagraphAlignment GetParagraphAlignment(
|
||
PdfParagraphAlignmentType type)
|
||
{
|
||
return type switch
|
||
{
|
||
PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
|
||
PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
|
||
PdfParagraphAlignmentType.Right => ParagraphAlignment.Right,
|
||
_ => ParagraphAlignment.Justify,
|
||
};
|
||
}
|
||
|
||
private static void DefineStyles(Document document)
|
||
{
|
||
var style = document.Styles["Normal"];
|
||
style.Font.Name = "Times New Roman";
|
||
style.Font.Size = 14;
|
||
style = document.Styles.AddStyle("NormalTitle", "Normal");
|
||
style.Font.Bold = true;
|
||
}
|
||
|
||
public void CreateParagraph(PdfParagraph pdfParagraph)
|
||
{
|
||
if (_document == null)
|
||
{
|
||
return;
|
||
}
|
||
if (_section == null)
|
||
{
|
||
_section = _document.AddSection();
|
||
}
|
||
var paragraph = _section.AddParagraph(pdfParagraph.Text);
|
||
paragraph.Format.SpaceAfter = "1cm";
|
||
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
|
||
paragraph.Style = pdfParagraph.Style;
|
||
}
|
||
#endregion
|
||
|
||
#region работа с изображениями
|
||
public void CreateImage(byte[] image)
|
||
{
|
||
if (image == null || image.Length == 0)
|
||
{
|
||
throw new ArgumentNullException("Картинка не загружена");
|
||
}
|
||
if (_document == null)
|
||
{
|
||
return;
|
||
}
|
||
if (_section == null)
|
||
{
|
||
_section = _document.AddSection();
|
||
}
|
||
var imageFileName = Path.GetTempFileName();
|
||
File.WriteAllBytes(imageFileName, image);
|
||
var img = _section.AddImage(imageFileName);
|
||
img.Width = IMAGE_WIDTH;
|
||
img.LockAspectRatio = true;
|
||
_section.AddParagraph();
|
||
}
|
||
#endregion
|
||
|
||
#region работа с таблицами
|
||
public void CreateTable()
|
||
{
|
||
if (_document == null || _section == null)
|
||
{
|
||
return;
|
||
}
|
||
_table = _section.AddTable();
|
||
}
|
||
|
||
public void CreateTableWithColumns(List<string> columns)
|
||
{
|
||
if (_document == null || _section == null)
|
||
{
|
||
return;
|
||
}
|
||
_table = _section.AddTable();
|
||
foreach (var elem in columns)
|
||
{
|
||
_table.AddColumn(elem);
|
||
}
|
||
}
|
||
|
||
public void CreateTableWithColumns(int columnNumber)
|
||
{
|
||
if (_document == null || _section == null)
|
||
{
|
||
return;
|
||
}
|
||
_table = _section.AddTable();
|
||
for (int i = 0; i < columnNumber; i++)
|
||
{
|
||
_table.AddColumn(COLUMN_WIDTH);
|
||
}
|
||
}
|
||
|
||
public void CreateRow(PdfRowParameters rowParameters)
|
||
{
|
||
if (_table == null)
|
||
{
|
||
return;
|
||
}
|
||
var row = _table.AddRow();
|
||
for (int i = 0; i < rowParameters.Texts.Count; ++i)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(rowParameters.Texts[i]))
|
||
InsertTextIntoCell(row.Index, i, rowParameters, rowParameters.Texts[i]);
|
||
}
|
||
}
|
||
|
||
public void InsertTextIntoColumn(PdfColumnParameters columnParameters)
|
||
{
|
||
if (_table == null)
|
||
{
|
||
return;
|
||
}
|
||
for (int i = 0; i < columnParameters.Texts.Count; ++i)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(columnParameters.Texts[i]))
|
||
InsertTextIntoCell(i, columnParameters.ColumnIndex, columnParameters, columnParameters.Texts[i]);
|
||
}
|
||
}
|
||
|
||
public void InsertTextIntoCell(int rowIndex, int columnIndex, PdfCellParameters cellParameters)
|
||
{
|
||
InsertTextIntoCell(rowIndex, columnIndex, cellParameters, cellParameters.Text);
|
||
}
|
||
|
||
private void InsertTextIntoCell(int rowIndex, int columnIndex, ITableTextParameters parameters, string text)
|
||
{
|
||
if (_table == null)
|
||
{
|
||
return;
|
||
}
|
||
Cell cell = _table.Rows[rowIndex].Cells[columnIndex];
|
||
cell.AddParagraph(text);
|
||
if (!string.IsNullOrEmpty(parameters.Style))
|
||
{
|
||
cell.Style = parameters.Style;
|
||
}
|
||
cell.Borders.Left.Width = BORDER_WIDTH;
|
||
cell.Borders.Right.Width = BORDER_WIDTH;
|
||
cell.Borders.Top.Width = BORDER_WIDTH;
|
||
cell.Borders.Bottom.Width = BORDER_WIDTH;
|
||
cell.Format.Alignment = GetParagraphAlignment(
|
||
parameters.ParagraphAlignment);
|
||
cell.VerticalAlignment = VerticalAlignment.Center;
|
||
}
|
||
|
||
public void MergeCells(PdfCellsMergeParameters parameters)
|
||
{
|
||
if (_table == null)
|
||
{
|
||
return;
|
||
}
|
||
Cell cell = _table.Rows[parameters.RowIndex].Cells[parameters.ColumnIndex];
|
||
switch (parameters.Direction)
|
||
{
|
||
case PdfCellsMergeDirections.Down:
|
||
cell.MergeDown = parameters.CellNumber;
|
||
break;
|
||
case PdfCellsMergeDirections.Right:
|
||
cell.MergeRight = parameters.CellNumber;
|
||
break;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region работа с диаграммами
|
||
public void CreatePieChart(IPdfPieChartParameters parameters)
|
||
{
|
||
if (_document == null || _section == null)
|
||
throw new ArgumentNullException(nameof(_document), "Document is not created.");
|
||
Chart chart = new Chart(ChartType.Pie2D);
|
||
Series series = chart.SeriesCollection.AddSeries();
|
||
series.Name = parameters.Series.SeriesName;
|
||
series.Add(parameters.Series.Data.Select(x => x.Value).ToArray());
|
||
chart.XValues.AddXSeries().Add(parameters.Series.Data.Select(x => x.Key).ToArray());
|
||
ConfigChart(parameters, chart);
|
||
_section.Add(chart);
|
||
}
|
||
|
||
private void ConfigChart(IPdfPieChartParameters parameters, Chart chart)
|
||
{
|
||
chart.DataLabel.Type = DataLabelType.Percent;
|
||
chart.DataLabel.Position = DataLabelPosition.OutsideEnd;
|
||
chart.Width = Unit.FromCentimeter(16.0);
|
||
chart.Height = Unit.FromCentimeter(12.0);
|
||
chart.TopArea.AddParagraph(parameters.ChartTitle);
|
||
chart.XAxis.MajorTickMark = (TickMarkType)2;
|
||
chart.YAxis.MajorTickMark = (TickMarkType)2;
|
||
chart.YAxis.HasMajorGridlines = true;
|
||
chart.PlotArea.LineFormat.Width = new Unit(1);
|
||
chart.PlotArea.LineFormat.Visible = true;
|
||
switch (parameters.LegendLocation)
|
||
{
|
||
case PdfDiagramLegendLocation.Left:
|
||
chart.LeftArea.AddLegend();
|
||
break;
|
||
case PdfDiagramLegendLocation.Right:
|
||
chart.RightArea.AddLegend();
|
||
break;
|
||
case PdfDiagramLegendLocation.Top:
|
||
chart.TopArea.AddLegend();
|
||
break;
|
||
case PdfDiagramLegendLocation.Bottom:
|
||
chart.BottomArea.AddLegend();
|
||
break;
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|