Готово
This commit is contained in:
parent
4d673aa1dc
commit
2faa7bf7f9
@ -8,6 +8,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="OxyPlot.Core" Version="2.2.0" />
|
||||
<PackageReference Include="OxyPlot.WindowsForms" Version="2.2.0" />
|
||||
<PackageReference Include="OxyPlot.Wpf" Version="2.2.0" />
|
||||
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -24,6 +24,7 @@ namespace Components.NonVisual
|
||||
public HeaderedTablePDF(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
@ -33,10 +34,14 @@ namespace Components.NonVisual
|
||||
if (settings == null ||
|
||||
string.IsNullOrEmpty(settings.FilePath) ||
|
||||
string.IsNullOrEmpty(settings.DocumentTitle) ||
|
||||
settings.Columns == null || settings.Columns.Count == 0 ||
|
||||
settings.DataList == null)
|
||||
settings.HeaderTitles == null || settings.HeaderTitles.Count == 0 ||
|
||||
settings.ColumnWidths == null || settings.DataList == null ||
|
||||
settings.ColumnPropertyMappings == 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");
|
||||
@ -45,9 +50,9 @@ namespace Components.NonVisual
|
||||
table.Borders.Width = 0.75;
|
||||
|
||||
// столбцы
|
||||
foreach (var (_, width, _, _) in settings.Columns)
|
||||
for (int i = 0; i < settings.ColumnWidths.Count; i++)
|
||||
{
|
||||
Column column = table.AddColumn(Unit.FromCentimeter(width));
|
||||
Column column = table.AddColumn(Unit.FromCentimeter(settings.ColumnWidths[i]));
|
||||
column.Format.Alignment = ParagraphAlignment.Center;
|
||||
}
|
||||
|
||||
@ -55,12 +60,11 @@ namespace Components.NonVisual
|
||||
Row headerRow = table.AddRow();
|
||||
headerRow.Height = Unit.FromCentimeter(settings.HeaderRowHeight);
|
||||
|
||||
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
||||
for (int i = 0; i < settings.HeaderTitles.Count; i++)
|
||||
{
|
||||
var (headerTitle, _, _, _) = settings.Columns[columnIndex];
|
||||
headerRow.Cells[columnIndex].AddParagraph(headerTitle);
|
||||
headerRow.Cells[columnIndex].Format.Font.Bold = true;
|
||||
headerRow.Cells[columnIndex].Format.Alignment = ParagraphAlignment.Center;
|
||||
headerRow.Cells[i].AddParagraph(settings.HeaderTitles[i]);
|
||||
headerRow.Cells[i].Format.Font.Bold = true;
|
||||
headerRow.Cells[i].Format.Alignment = ParagraphAlignment.Center;
|
||||
}
|
||||
|
||||
// данные
|
||||
@ -69,20 +73,15 @@ namespace Components.NonVisual
|
||||
Row row = table.AddRow();
|
||||
row.Height = Unit.FromCentimeter(settings.DataRowHeight);
|
||||
|
||||
for (int columnIndex = 0; columnIndex < settings.Columns.Count; columnIndex++)
|
||||
foreach (var columnMapping in settings.ColumnPropertyMappings)
|
||||
{
|
||||
var (_, _, propertyName, _) = settings.Columns[columnIndex];
|
||||
PropertyInfo propertyInfo = typeof(T).GetProperty(columnMapping.Value);
|
||||
|
||||
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
|
||||
if (propertyInfo == null)
|
||||
throw new ArgumentException($"Свойство {propertyName} не найдено в классе {typeof(T).Name}.");
|
||||
throw new ArgumentException($"Свойство {columnMapping.Value} не найдено в классе {typeof(T).Name}.");
|
||||
|
||||
object value = propertyInfo.GetValue(dataItem);
|
||||
if (columnIndex == 0)
|
||||
{
|
||||
row.Cells[columnIndex].Format.Font.Bold = true;
|
||||
}
|
||||
row.Cells[columnIndex].AddParagraph(value != null ? value.ToString() : "");
|
||||
row.Cells[columnMapping.Key].AddParagraph(value != null ? value.ToString() : "");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,17 @@
|
||||
using System;
|
||||
using Components.SaveToPdfHelpers;
|
||||
using MigraDoc.DocumentObjectModel;
|
||||
using MigraDoc.Rendering;
|
||||
using OxyPlot.Series;
|
||||
using OxyPlot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OxyPlot.WindowsForms;
|
||||
using OxyPlot.Legends;
|
||||
|
||||
namespace Components.NonVisual
|
||||
{
|
||||
@ -13,13 +20,100 @@ namespace Components.NonVisual
|
||||
public HistogramPDF()
|
||||
{
|
||||
InitializeComponent();
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
}
|
||||
|
||||
public HistogramPDF(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void CreateHistogramPdf(HistogramData histogramData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(histogramData.filePath))
|
||||
throw new ArgumentException("Путь к файлу не может быть пустым.");
|
||||
if (string.IsNullOrEmpty(histogramData.documentTitle))
|
||||
throw new ArgumentException("Название документа не может быть пустым.");
|
||||
if (string.IsNullOrEmpty(histogramData.chartTitle))
|
||||
throw new ArgumentException("Заголовок диаграммы не может быть пустым.");
|
||||
if (histogramData.chartData == null || histogramData.chartData.Count == 0)
|
||||
throw new ArgumentException("Набор данных не может быть пустым.");
|
||||
|
||||
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 };
|
||||
|
||||
foreach (var data in histogramData.chartData)
|
||||
{
|
||||
var barSeries = new BarSeries { Title = data.SeriesName };
|
||||
foreach (var item in data.Data)
|
||||
{
|
||||
barSeries.Items.Add(new BarItem(item.Value));
|
||||
}
|
||||
plotModel.Series.Add(barSeries);
|
||||
}
|
||||
|
||||
// Добавление легенды
|
||||
AddLegend(plotModel, histogramData.legendPosition);
|
||||
|
||||
// сохранение графика в изображение
|
||||
var pngExporter = new PngExporter { Width = 600, Height = 400 };
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
pngExporter.Export(plotModel, stream);
|
||||
File.WriteAllBytes("chart.png", stream.ToArray());
|
||||
}
|
||||
|
||||
// создание документа
|
||||
Document document = new Document();
|
||||
document.Info.Title = histogramData.documentTitle;
|
||||
document.Info.Subject = "Гистограмма";
|
||||
|
||||
Section section = document.AddSection();
|
||||
section.AddParagraph(histogramData.chartTitle, "Heading1");
|
||||
|
||||
// вставка изображения в PDF
|
||||
var image = section.AddImage("chart.png");
|
||||
image.Width = Unit.FromCentimeter(15);
|
||||
|
||||
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true) { Document = document };
|
||||
renderer.RenderDocument();
|
||||
renderer.PdfDocument.Save(histogramData.filePath);
|
||||
|
||||
File.Delete("chart.png");
|
||||
}
|
||||
|
||||
//добавление легенды
|
||||
private void AddLegend(PlotModel plotModel, LegendPositions legendPosition)
|
||||
{
|
||||
// Создание легенды
|
||||
var legend = new OxyPlot.Legends.Legend
|
||||
{
|
||||
LegendPlacement = LegendPlacement.Outside,
|
||||
LegendPosition = ConvertLegendPosition(legendPosition),
|
||||
LegendOrientation = LegendOrientation.Vertical
|
||||
};
|
||||
|
||||
plotModel.Legends.Add(legend);
|
||||
}
|
||||
|
||||
// конвертация позиции легенды
|
||||
private OxyPlot.Legends.LegendPosition ConvertLegendPosition(LegendPositions legendPosition)
|
||||
{
|
||||
return legendPosition switch
|
||||
{
|
||||
LegendPositions.Top => OxyPlot.Legends.LegendPosition.TopCenter,
|
||||
LegendPositions.Bottom => OxyPlot.Legends.LegendPosition.BottomCenter,
|
||||
LegendPositions.Left => OxyPlot.Legends.LegendPosition.LeftTop,
|
||||
LegendPositions.Right => OxyPlot.Legends.LegendPosition.RightTop,
|
||||
_ => OxyPlot.Legends.LegendPosition.TopCenter, // Положение по умолчанию
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ namespace Components.NonVisual
|
||||
public TablePDF(IContainer container)
|
||||
{
|
||||
container.Add(this);
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
17
Components/SaveToPdfHelpers/HistogramData.cs
Normal file
17
Components/SaveToPdfHelpers/HistogramData.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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>? chartData { get; set; }
|
||||
}
|
||||
}
|
@ -10,9 +10,11 @@ namespace Components.SaveToPdfHelpers
|
||||
{
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public string DocumentTitle { get; set; } = string.Empty;
|
||||
public List<(string HeaderTitle, float Width, string PropertyName, int ColumnIndex)> Columns { get; set; }
|
||||
public List<string> HeaderTitles { get; set; }
|
||||
public List<float> ColumnWidths { get; set; }
|
||||
public float HeaderRowHeight { get; set; }
|
||||
public float DataRowHeight { get; set; }
|
||||
public List<T>? DataList { get; set; }
|
||||
public Dictionary<int, string> ColumnPropertyMappings { get; set; }
|
||||
}
|
||||
}
|
||||
|
49
WinFormsTest/Form1.Designer.cs
generated
49
WinFormsTest/Form1.Designer.cs
generated
@ -28,9 +28,16 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
customListBox = new Components.CustomListBox();
|
||||
customTextBox = new Components.CustomTextBox();
|
||||
customDataGridView1 = new Components.CustomDataGridView();
|
||||
buttonCreateTable = new Button();
|
||||
buttonCreateHeaderedTable = new Button();
|
||||
buttonCreateHistogram = new Button();
|
||||
tablepdf1 = new Components.NonVisual.TablePDF(components);
|
||||
histogrampdf1 = new Components.NonVisual.HistogramPDF(components);
|
||||
headeredTablepdf1 = new Components.NonVisual.HeaderedTablePDF(components);
|
||||
SuspendLayout();
|
||||
//
|
||||
// customListBox
|
||||
@ -55,14 +62,47 @@
|
||||
customDataGridView1.Size = new Size(449, 150);
|
||||
customDataGridView1.TabIndex = 2;
|
||||
//
|
||||
// buttonCreateTable
|
||||
//
|
||||
buttonCreateTable.Location = new Point(41, 235);
|
||||
buttonCreateTable.Name = "buttonCreateTable";
|
||||
buttonCreateTable.Size = new Size(139, 44);
|
||||
buttonCreateTable.TabIndex = 11;
|
||||
buttonCreateTable.Text = "Документ с контекстом";
|
||||
buttonCreateTable.UseVisualStyleBackColor = true;
|
||||
buttonCreateTable.Click += GeneratePdfButton_Click;
|
||||
//
|
||||
// buttonCreateHeaderedTable
|
||||
//
|
||||
buttonCreateHeaderedTable.Location = new Point(204, 231);
|
||||
buttonCreateHeaderedTable.Name = "buttonCreateHeaderedTable";
|
||||
buttonCreateHeaderedTable.Size = new Size(139, 53);
|
||||
buttonCreateHeaderedTable.TabIndex = 12;
|
||||
buttonCreateHeaderedTable.Text = "Документ с настраиваемой таблицей";
|
||||
buttonCreateHeaderedTable.UseVisualStyleBackColor = true;
|
||||
buttonCreateHeaderedTable.Click += btnGeneratePDF_Click;
|
||||
//
|
||||
// buttonCreateHistogram
|
||||
//
|
||||
buttonCreateHistogram.Location = new Point(362, 235);
|
||||
buttonCreateHistogram.Name = "buttonCreateHistogram";
|
||||
buttonCreateHistogram.Size = new Size(139, 44);
|
||||
buttonCreateHistogram.TabIndex = 13;
|
||||
buttonCreateHistogram.Text = "Документ с диаграммой";
|
||||
buttonCreateHistogram.UseVisualStyleBackColor = true;
|
||||
buttonCreateHistogram.Click += btnGenerateHistogrammPdf_Click;
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(1003, 257);
|
||||
ClientSize = new Size(934, 296);
|
||||
Controls.Add(customDataGridView1);
|
||||
Controls.Add(customTextBox);
|
||||
Controls.Add(customListBox);
|
||||
Controls.Add(buttonCreateTable);
|
||||
Controls.Add(buttonCreateHeaderedTable);
|
||||
Controls.Add(buttonCreateHistogram);
|
||||
Name = "Form1";
|
||||
Text = "Form1";
|
||||
ResumeLayout(false);
|
||||
@ -73,5 +113,12 @@
|
||||
private Components.CustomListBox customListBox;
|
||||
private Components.CustomTextBox customTextBox;
|
||||
private Components.CustomDataGridView customDataGridView1;
|
||||
|
||||
private Button buttonCreateTable;
|
||||
private Button buttonCreateHeaderedTable;
|
||||
private Button buttonCreateHistogram;
|
||||
private Components.NonVisual.TablePDF tablepdf1;
|
||||
private Components.NonVisual.HistogramPDF histogrampdf1;
|
||||
private Components.NonVisual.HeaderedTablePDF headeredTablepdf1;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
using Components;
|
||||
using Components.NonVisual;
|
||||
using Components.Object;
|
||||
using Components.SaveToPdfHelpers;
|
||||
using OxyPlot.Legends;
|
||||
|
||||
namespace WinFormsTest
|
||||
{
|
||||
@ -42,5 +45,95 @@ namespace WinFormsTest
|
||||
};
|
||||
customDataGridView1.FillData(data);
|
||||
}
|
||||
|
||||
private void GeneratePdfButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
var settings = new PdfDocumentData(
|
||||
"F:\\Îò÷åò1.pdf",
|
||||
"Íàçâàíèå äîêóìåíòà",
|
||||
new List<string[,]>
|
||||
{
|
||||
new string[,]
|
||||
{
|
||||
{ "ß÷åéêà 1.1", "ß÷åéêà 2.1", "ß÷åéêà 3.1" },
|
||||
{ "ß÷åéêà 2.1", "ß÷åéêà 2.2", "ß÷åéêà 3.2" }
|
||||
},
|
||||
new string[,]
|
||||
{
|
||||
{ "Íå ÿ÷åéêà 1", "Íå ÿ÷åéêà 2" },
|
||||
{ "ß÷Àéêà 1", "ß÷Àéêà 2" }
|
||||
}
|
||||
});
|
||||
try
|
||||
{
|
||||
tablepdf1.GeneratePdf(settings);
|
||||
MessageBox.Show("PDF-äîêóìåíò óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Ïðîèçîøëà îøèáêà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
private void btnGeneratePDF_Click(object sender, EventArgs e)
|
||||
{
|
||||
var settings = new PDFTableSettings<Person>
|
||||
{
|
||||
FilePath = "F:\\Îò÷åò2.pdf",
|
||||
DocumentTitle = "Îò÷åò ïî ñòóäåíòàì",
|
||||
HeaderTitles = new List<string> { "ÔÈÎ", "Âîçðàñò", "Ïî÷òà" },
|
||||
ColumnWidths = new List<float> { 6.0f, 2.0f, 6.0f },
|
||||
HeaderRowHeight = 1.0f,
|
||||
DataRowHeight = 1.0f,
|
||||
DataList = new List<Person>
|
||||
{
|
||||
new Person ( "Àëåêñàíä Áóáûë¸â", 30, "Alex@mail.ru" ),
|
||||
new Person ( "Øåðëîê Õîëìñ", 25, "221B_Baker_Street@mail.com" ),
|
||||
new Person ("Ñòàñ Àñàôüåâ", 41, "Stas@gmail.com")
|
||||
},
|
||||
ColumnPropertyMappings = new Dictionary<int, string>
|
||||
{
|
||||
{ 0, nameof(Person.Name) },
|
||||
{ 1, nameof(Person.Age) },
|
||||
{ 2, nameof(Person.Email) }
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
headeredTablepdf1.GeneratePDFWithHead(settings);
|
||||
MessageBox.Show("PDF-äîêóìåíò óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Îøèáêà: {ex.Message}");
|
||||
}
|
||||
}
|
||||
private void btnGenerateHistogrammPdf_Click(object sender, EventArgs e)
|
||||
{
|
||||
var setting = new HistogramData
|
||||
{
|
||||
filePath = "F:\\Ãèñòîãðàììà.pdf",
|
||||
documentTitle = "Ãèñòîãðàììà",
|
||||
chartTitle = "Ïðèìåð ãèñòîãðàììû",
|
||||
legendPosition = LegendPositions.Bottom,
|
||||
|
||||
chartData = new List<ChartData>
|
||||
{
|
||||
new ChartData { SeriesName = "Ñåðèÿ 1", Data = new Dictionary<string, double> { { "Êàòåãîðèÿ 1", 5 }, { "Êàòåãîðèÿ 2", 10 } } },
|
||||
new ChartData { SeriesName = "Ñåðèÿ 2", Data = new Dictionary<string, double> { { "Êàòåãîðèÿ 1", 3 }, { "Êàòåãîðèÿ 2", 8 } } },
|
||||
new ChartData { SeriesName = "Ñåðèÿ 3", Data = new Dictionary<string, double> { { "Êàòåãîðèÿ 1", 3 }, { "Êàòåãîðèÿ 2", 8 } } }
|
||||
}
|
||||
};
|
||||
try
|
||||
{
|
||||
|
||||
histogrampdf1.CreateHistogramPdf(setting);
|
||||
MessageBox.Show("PDF óñïåøíî ñãåíåðèðîâàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Îøèáêà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,4 +117,13 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="tablepdf1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="histogrampdf1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>121, 17</value>
|
||||
</metadata>
|
||||
<metadata name="headeredTablepdf1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>253, 17</value>
|
||||
</metadata>
|
||||
</root>
|
Loading…
Reference in New Issue
Block a user