From dc7bee82a036b0e1a482908a3a4cbe28f6098945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=94?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=BE=D0=B2?= Date: Sat, 2 Nov 2024 18:51:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BD=D1=8F=D1=82=D0=B0?= =?UTF-8?q?=D1=8F=202=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Components/NonVisual/HeaderedTablePDF.cs | 33 ++++----- Components/NonVisual/HistogramPDF.cs | 22 +++--- Components/SaveToPdfHelpers/HistogramData.cs | 10 +-- .../SaveToPdfHelpers/PDFTableSettings.cs | 4 +- WinFormsTest/Form1.cs | 67 +++++++++++++++---- 5 files changed, 88 insertions(+), 48 deletions(-) diff --git a/Components/NonVisual/HeaderedTablePDF.cs b/Components/NonVisual/HeaderedTablePDF.cs index 696ae6a..aebf931 100644 --- a/Components/NonVisual/HeaderedTablePDF.cs +++ b/Components/NonVisual/HeaderedTablePDF.cs @@ -34,25 +34,22 @@ namespace Components.NonVisual if (settings == null || string.IsNullOrEmpty(settings.FilePath) || string.IsNullOrEmpty(settings.DocumentTitle) || - settings.HeaderTitles == null || settings.HeaderTitles.Count == 0 || - settings.ColumnWidths == null || settings.DataList == null || - settings.ColumnPropertyMappings == null) + settings.Columns == null || settings.Columns.Count == 0 || + settings.DataList == null) throw new ArgumentException("Заполнены не все необходимые данные для генерации документа."); - if (settings.HeaderTitles.Count != settings.ColumnWidths.Count) - throw new ArgumentException("Количество заголовков должно совпадать с количеством ширин столбцов."); - Document document = new Document(); Section section = document.AddSection(); - section.AddParagraph(settings.DocumentTitle, "Heading1"); + Paragraph titleParagraph = section.AddParagraph(settings.DocumentTitle, "Heading1"); + titleParagraph.Format.Font.Bold = true; Table table = new Table(); table.Borders.Width = 0.75; // столбцы - for (int i = 0; i < settings.ColumnWidths.Count; i++) + foreach (var (_, width, _, _) in settings.Columns) { - Column column = table.AddColumn(Unit.FromCentimeter(settings.ColumnWidths[i])); + Column column = table.AddColumn(Unit.FromCentimeter(width)); column.Format.Alignment = ParagraphAlignment.Center; } @@ -60,9 +57,10 @@ namespace Components.NonVisual Row headerRow = table.AddRow(); headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight); - for (int i = 0; i < settings.HeaderTitles.Count; i++) + for (int i = 0; i < settings.Columns.Count; i++) { - headerRow.Cells[i].AddParagraph(settings.HeaderTitles[i]); + var (headerTitle, _, _, _) = settings.Columns[i]; + headerRow.Cells[i].AddParagraph(headerTitle); headerRow.Cells[i].Format.Font.Bold = true; headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center; } @@ -73,15 +71,20 @@ namespace Components.NonVisual Row row = table.AddRow(); row.Height = Unit.FromCentimeter(settings.DataRowHeight); - foreach (var columnMapping in settings.ColumnPropertyMappings) + for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++) { - PropertyInfo propertyInfo = typeof(T).GetProperty(columnMapping.Value); + var (_, _, propertyName, _) = settings.Columns[columnIndex]; + PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName); if (propertyInfo == null) - throw new ArgumentException($"Свойство {columnMapping.Value} не найдено в классе {typeof(T).Name}."); + throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}."); object value = propertyInfo.GetValue(dataItem); - row.Cells[columnMapping.Key].AddParagraph(value != null ? value.ToString() : ""); + if (columnIndex == 0) + { + row.Cells[columnIndex].Format.Font.Bold = true; + } + row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : ""); } } diff --git a/Components/NonVisual/HistogramPDF.cs b/Components/NonVisual/HistogramPDF.cs index 7e76193..1ff5508 100644 --- a/Components/NonVisual/HistogramPDF.cs +++ b/Components/NonVisual/HistogramPDF.cs @@ -31,25 +31,25 @@ namespace Components.NonVisual public void CreateHistogramPdf(HistogramData histogramData) { - if (string.IsNullOrEmpty(histogramData.filePath)) + if (string.IsNullOrEmpty(histogramData.FilePath)) throw new ArgumentException("Путь к файлу не может быть пустым."); - if (string.IsNullOrEmpty(histogramData.documentTitle)) + if (string.IsNullOrEmpty(histogramData.DocumentTitle)) throw new ArgumentException("Название документа не может быть пустым."); - if (string.IsNullOrEmpty(histogramData.chartTitle)) + if (string.IsNullOrEmpty(histogramData.ChartTitle)) throw new ArgumentException("Заголовок диаграммы не может быть пустым."); - if (histogramData.chartData == null || histogramData.chartData.Count == 0) + if (histogramData.ChartData == null || histogramData.ChartData.Count == 0) throw new ArgumentException("Набор данных не может быть пустым."); - foreach (var data in histogramData.chartData) + foreach (var data in histogramData.ChartData) { if (string.IsNullOrEmpty(data.SeriesName) || data.Data == null || data.Data.Count == 0) throw new ArgumentException($"Набор данных для серии '{data.SeriesName}' некорректен."); } // создание графика - var plotModel = new PlotModel { Title = histogramData.chartTitle }; + var plotModel = new PlotModel { Title = histogramData.ChartTitle }; - foreach (var data in histogramData.chartData) + foreach (var data in histogramData.ChartData) { var barSeries = new BarSeries { Title = data.SeriesName }; foreach (var item in data.Data) @@ -60,7 +60,7 @@ namespace Components.NonVisual } // Добавление легенды - AddLegend(plotModel, histogramData.legendPosition); + AddLegend(plotModel, histogramData.LegendPosition); // сохранение графика в изображение var pngExporter = new PngExporter { Width = 600, Height = 400 }; @@ -72,11 +72,11 @@ namespace Components.NonVisual // создание документа Document document = new Document(); - document.Info.Title = histogramData.documentTitle; + document.Info.Title = histogramData.DocumentTitle; document.Info.Subject = "Гистограмма"; Section section = document.AddSection(); - section.AddParagraph(histogramData.chartTitle, "Heading1"); + section.AddParagraph(histogramData.ChartTitle, "Heading1"); // вставка изображения в PDF var image = section.AddImage("chart.png"); @@ -84,7 +84,7 @@ namespace Components.NonVisual PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document }; renderer.RenderDocument(); - renderer.PdfDocument.Save(histogramData.filePath); + renderer.PdfDocument.Save(histogramData.FilePath); File.Delete("chart.png"); } diff --git a/Components/SaveToPdfHelpers/HistogramData.cs b/Components/SaveToPdfHelpers/HistogramData.cs index 958a7b6..551e65a 100644 --- a/Components/SaveToPdfHelpers/HistogramData.cs +++ b/Components/SaveToPdfHelpers/HistogramData.cs @@ -8,10 +8,10 @@ namespace Components.SaveToPdfHelpers { public class HistogramData { - public string filePath { get; set; } = string.Empty; - public string documentTitle { get; set; } = string.Empty; - public string chartTitle { get; set; } = string.Empty; - public LegendPositions legendPosition { get; set; } - public List? chartData { get; set; } + public string FilePath { get; set; } = string.Empty; + public string DocumentTitle { get; set; } = string.Empty; + public string ChartTitle { get; set; } = string.Empty; + public LegendPositions LegendPosition { get; set; } + public List? ChartData { get; set; } } } diff --git a/Components/SaveToPdfHelpers/PDFTableSettings.cs b/Components/SaveToPdfHelpers/PDFTableSettings.cs index 7f97c72..b7e4759 100644 --- a/Components/SaveToPdfHelpers/PDFTableSettings.cs +++ b/Components/SaveToPdfHelpers/PDFTableSettings.cs @@ -10,11 +10,9 @@ namespace Components.SaveToPdfHelpers { public string FilePath { get; set; } = string.Empty; public string DocumentTitle { get; set; } = string.Empty; - public List HeaderTitles { get; set; } - public List ColumnWidths { get; set; } + public List<(string HeaderTitles, float Widht, string PropertyName, int ColumnIndex)> Columns { get; set; } public float HeaderRowHeight { get; set; } public float DataRowHeight { get; set; } public List? DataList { get; set; } - public Dictionary ColumnPropertyMappings { get; set; } } } diff --git a/WinFormsTest/Form1.cs b/WinFormsTest/Form1.cs index 0673f55..0ec80b6 100644 --- a/WinFormsTest/Form1.cs +++ b/WinFormsTest/Form1.cs @@ -46,10 +46,35 @@ namespace WinFormsTest customDataGridView1.FillData(data); } + private string FilePath() + { + using (SaveFileDialog saveFileDialog = new SaveFileDialog()) + { + saveFileDialog.InitialDirectory = "d:\\tmp"; + saveFileDialog.Filter = "Excel files (*.pdf)|*.pdf|All files (*.*)|*.*"; + saveFileDialog.FilterIndex = 1; + saveFileDialog.RestoreDirectory = true; + + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + return saveFileDialog.FileName; + } + } + return String.Empty; + } + private void GeneratePdfButton_Click(object sender, EventArgs e) { + string filePath = FilePath(); + + if (filePath == String.Empty) + { + MessageBox.Show(" : "); + return; + } + var settings = new PdfDocumentData( - "F:\\1.pdf", + filePath, " ", new List { @@ -76,12 +101,18 @@ namespace WinFormsTest } private void btnGeneratePDF_Click(object sender, EventArgs e) { + string filePath = FilePath(); + + if (filePath == String.Empty) + { + MessageBox.Show(" : "); + return; + } + var settings = new PDFTableSettings { - FilePath = "F:\\2.pdf", - DocumentTitle = " ", - HeaderTitles = new List { "", "", "" }, - ColumnWidths = new List { 6.0f, 2.0f, 6.0f }, + FilePath = filePath, + DocumentTitle = "", HeaderRowHeight = 1.0f, DataRowHeight = 1.0f, DataList = new List @@ -90,11 +121,11 @@ namespace WinFormsTest new Person ( " ", 25, "221B_Baker_Street@mail.com" ), new Person (" ", 41, "Stas@gmail.com") }, - ColumnPropertyMappings = new Dictionary + Columns = new List<(string, float, string, int)> { - { 0, nameof(Person.Name) }, - { 1, nameof(Person.Age) }, - { 2, nameof(Person.Email) } + ("", 6.0f, nameof(Person.Name), 0), + ("", 2.0f, nameof(Person.Age), 1), + ("", 6.0f, nameof(Person.Email), 2) } }; @@ -110,14 +141,22 @@ namespace WinFormsTest } private void btnGenerateHistogrammPdf_Click(object sender, EventArgs e) { + string filePath = FilePath(); + + if (filePath == String.Empty) + { + MessageBox.Show(" : "); + return; + } + var setting = new HistogramData { - filePath = "F:\\.pdf", - documentTitle = "", - chartTitle = " ", - legendPosition = LegendPositions.Bottom, + FilePath = filePath, + DocumentTitle = "", + ChartTitle = " ", + LegendPosition = LegendPositions.Bottom, - chartData = new List + ChartData = new List { new ChartData { SeriesName = " 1", Data = new Dictionary { { " 1", 5 }, { " 2", 10 } } }, new ChartData { SeriesName = " 2", Data = new Dictionary { { " 1", 3 }, { " 2", 8 } } },