KOP/Components/SaveToPdf.cs
2024-10-16 01:47:44 +04:00

285 lines
9.9 KiB
C#

using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using Components.SaveToPdfHelpers;
using System.Text;
using System.Security.Cryptography;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using Components.Nonvisual;
using System.IO;
namespace Components
{
internal class SaveToPdf
{
private Document? _document;
private Section? _section;
private Table? _table;
public void CreateTableDocumentWithContext(string title, string path, List<string[][]> tables)
{
_document = new Document();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
DefineStyles(_document);
_section = _document.AddSection();
CreateParagraph(new PdfParagraph
{
Text = title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
foreach (var table in tables)
{
if (table.Length == 0) continue;
CreateTable(Enumerable.Repeat("3cm", table[0].Length).ToList());
foreach (var row in table)
{
CreateRow(new PdfRowParameters
{
Texts = row.ToList(),
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
}
CreateParagraph(new PdfParagraph());
}
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(path);
}
public void CreateConfigurableTableDocument<T>(string path, string title,
List<(double width, string header, string propName)> columns, double headerRowHeight,
double rowHeight, List<T> data)
{
_document = new Document();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
DefineStyles(_document);
_section = _document.AddSection();
CreateParagraph(new PdfParagraph
{
Text = title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
CreateTable(columns.Select(c => c.width).ToList());
CreateRow(new PdfRowParameters
{
Texts = columns.Select(c => c.header).ToList(),
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center,
RowHeight = headerRowHeight,
});
foreach (var obj in data)
{
List<string> cells = new List<string>();
for (int i = 0; i < columns.Count; i++)
{
var propName = columns[i].propName;
var prop = typeof(T).GetProperty(propName);
if (prop is null)
{
throw new ArgumentException($"Failed to find property {prop} in provided objects class");
}
var propVal = prop.GetValue(obj)?.ToString();
if (propVal is not null)
{
cells.Add(propVal);
}
}
CreateRow(new PdfRowParameters
{
Texts = cells,
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left,
FirstCellStyle = "NormalTitle",
FirstCellParagraphAlignment = PdfParagraphAlignmentType.Center,
RowHeight = rowHeight,
});
}
CreateParagraph(new PdfParagraph());
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(path);
}
public void CreateHist(string filePath, string title, string histTitle,
LegendAlignment alignment, Dictionary<string, int[]> data)
{
_document = new Document();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
DefineStyles(_document);
_section = _document.AddSection();
CreateParagraph(new PdfParagraph
{
Text = title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
CreateChart(new PdfChartParameters
{
Data = data,
Title = histTitle,
LegendAlignment = alignment,
});
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
renderer.RenderDocument();
renderer.PdfDocument.Save(filePath);
}
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;
}
protected void CreateParagraph(PdfParagraph pdfParagraph)
{
if (_section == null)
{
return;
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Style = pdfParagraph.Style;
}
protected void CreateTable(List<string> columns)
{
if (_document == null)
{
return;
}
_table = _document.LastSection.AddTable();
foreach (var elem in columns)
{
_table.AddColumn(elem);
}
}
protected void CreateTable(List<double> columnWidths)
{
if (_document == null)
{
return;
}
_table = _document.LastSection.AddTable();
foreach (var elem in columnWidths)
{
_table.AddColumn(Unit.FromCentimeter(elem));
}
}
protected void CreateRow(PdfRowParameters rowParameters)
{
if (_table == null)
{
return;
}
var row = _table.AddRow();
for (int i = 0; i < rowParameters.Texts.Count; ++i)
{
row.Cells[i].AddParagraph(rowParameters.Texts[i]);
if (i == 0 && !string.IsNullOrEmpty(rowParameters.FirstCellStyle))
{
row.Cells[i].Style = rowParameters.FirstCellStyle;
}
else if (!string.IsNullOrEmpty(rowParameters.Style))
{
row.Cells[i].Style = rowParameters.Style;
}
if (i == 0 && rowParameters.FirstCellParagraphAlignment != null)
{
row.Cells[i].Format.Alignment = GetParagraphAlignment((PdfParagraphAlignmentType)rowParameters.FirstCellParagraphAlignment);
}
else
{
row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment);
}
if (rowParameters.RowHeight is not null)
{
row.Height = Unit.FromCentimeter((double)rowParameters.RowHeight);
}
Unit borderWidth = 0.5;
row.Cells[i].Borders.Left.Width = borderWidth;
row.Cells[i].Borders.Right.Width = borderWidth;
row.Cells[i].Borders.Top.Width = borderWidth;
row.Cells[i].Borders.Bottom.Width = borderWidth;
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
protected void CreateChart(PdfChartParameters chartParams)
{
if (_section == null)
return;
Chart chart = new Chart(ChartType.Bar2D);
chart.Width = "15cm";
chart.Height = "8cm";
foreach (var dataSeries in chartParams.Data)
{
Series series = chart.SeriesCollection.AddSeries();
series.Name = dataSeries.Key;
series.Add(dataSeries.Value.Select(x => (double)x).ToArray());
}
chart.TopArea.AddParagraph(chartParams.Title);
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
_ = chartParams.LegendAlignment switch
{
LegendAlignment.Top => chart.TopArea.AddLegend(),
LegendAlignment.Right => chart.RightArea.AddLegend(),
LegendAlignment.Bottom => chart.BottomArea.AddLegend(),
LegendAlignment.Left => chart.LeftArea.AddLegend(),
_ => chart.BottomArea.AddLegend(),
};
chart.PlotArea.LineFormat.Width = 1;
chart.PlotArea.LineFormat.Visible = true;
_section.Add(chart);
}
}
}