done configurable table component

This commit is contained in:
ShabOl 2024-09-24 23:54:36 +04:00
parent 04a9c7bd89
commit 05fd1f373b
8 changed files with 215 additions and 18 deletions

View File

@ -40,6 +40,8 @@
PrintObjectButton = new Button();
CreateDocumentWithTables = new Button();
DocumentContextComponent = new ShabComponentsLibrary.ShabDocumentContextComponent(components);
ConfigurableTableComponent = new ShabComponentsLibrary.ShabConfigurableTableComponent(components);
CreateConfigurableTable = new Button();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
groupBox3.SuspendLayout();
@ -145,11 +147,22 @@
CreateDocumentWithTables.UseVisualStyleBackColor = true;
CreateDocumentWithTables.Click += CreateDocumentWithTables_Click;
//
// CreateConfigurableTable
//
CreateConfigurableTable.Location = new Point(213, 592);
CreateConfigurableTable.Name = "CreateConfigurableTable";
CreateConfigurableTable.Size = new Size(172, 42);
CreateConfigurableTable.TabIndex = 7;
CreateConfigurableTable.Text = "Документ с настраиваемой таблицей";
CreateConfigurableTable.UseVisualStyleBackColor = true;
CreateConfigurableTable.Click += CreateConfigurableTable_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(637, 660);
Controls.Add(CreateConfigurableTable);
Controls.Add(CreateDocumentWithTables);
Controls.Add(groupBox3);
Controls.Add(groupBox2);
@ -175,5 +188,7 @@
private Button PrintObjectButton;
private Button CreateDocumentWithTables;
private ShabComponentsLibrary.ShabDocumentContextComponent DocumentContextComponent;
private ShabComponentsLibrary.ShabConfigurableTableComponent ConfigurableTableComponent;
private Button CreateConfigurableTable;
}
}

View File

@ -100,24 +100,45 @@ namespace FormsTesting
private void CreateDocumentWithTables_Click(object sender, EventArgs e)
{
string[][] table1 = new string[][]
{
new string[] { "Стр 1 Кол 1", "Стр 1 Кол 2", "Стр 1 Кол 3" },
new string[] { "Стр 2 Кол 1", "Стр 2 Кол 2", "Стр 2 Кол 3" }
};
string[][] table1 =
[
["Стр 1 Кол 1", "Стр 1 Кол 2", "Стр 1 Кол 3"],
["Стр 2 Кол 1", "Стр 2 Кол 2", "Стр 2 Кол 3"]
];
string[][] table2 = new string[][]
{
new string[] { "Стр 1 Кол 1", "Стр 1 Кол 2", "Стр 1 Кол 3", "Стр 1 Кол 4", "Стр 1 Кол 5", "Стр 1 Кол 6" },
new string[] { "Стр 2 Кол 1", "Стр 2 Кол 2", "Стр 2 Кол 3", "Стр 2 Кол 4", "Стр 2 Кол 5", "Стр 2 Кол 6" },
new string[] { "Стр 3 Кол 1", "Стр 3 Кол 2", "Стр 3 Кол 3", "Стр 3 Кол 4", "Стр 3 Кол 5", "Стр 3 Кол 6" },
new string[] { "Стр 4 Кол 1", "Стр 4 Кол 2", "Стр 4 Кол 3", "Стр 4 Кол 4", "Стр 4 Кол 5", "Стр 4 Кол 6" },
};
string[][] table2 =
[
["Стр 1 Кол 1", "Стр 1 Кол 2", "Стр 1 Кол 3", "Стр 1 Кол 4", "Стр 1 Кол 5", "Стр 1 Кол 6"],
["Стр 2 Кол 1", "Стр 2 Кол 2", "Стр 2 Кол 3", "Стр 2 Кол 4", "Стр 2 Кол 5", "Стр 2 Кол 6"],
["Стр 3 Кол 1", "Стр 3 Кол 2", "Стр 3 Кол 3", "Стр 3 Кол 4", "Стр 3 Кол 5", "Стр 3 Кол 6"],
["Стр 4 Кол 1", "Стр 4 Кол 2", "Стр 4 Кол 3", "Стр 4 Кол 4", "Стр 4 Кол 5", "Стр 4 Кол 6"],
];
DocumentContextComponent.CreateDocument(
@"C:\Comps\Doc1.pdf",
"Sample text",
new List<string[][]> { table1, table2 });
}
private void CreateConfigurableTable_Click(object sender, EventArgs e)
{
List<TestPerson> Persons = new List<TestPerson>
{
new TestPerson(1, "Иван", "Иванов", 34),
new TestPerson(2, "Петр", "Петров", 44),
new TestPerson(3, "Сергей", "Сергеев", 55),
new TestPerson(4, "Ольга", "Иванова", 34),
new TestPerson(5, "Татьяна", "Петрова", 44),
};
ConfigurableTableComponent.CreateDocument(
@"C:\Comps\Doc2.pdf",
"Настраиваемая таблица",
new List<string> { "3cm", "2cm", "3cm", "4cm" },
new List<string> { "1cm", "0.6cm" },
new List<string> { "Фамилия", "Идент.", "Имя", "Возраст" },
new List<string> { "LastName", "Id", "FirstName", "Age" },
Persons);
}
}
}

View File

@ -120,4 +120,7 @@
<metadata name="DocumentContextComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="ConfigurableTableComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>233, 17</value>
</metadata>
</root>

View File

@ -8,6 +8,12 @@ namespace ShabComponentsLibrary.OfficePackage.HelperModels
public string Style { get; set; } = string.Empty;
public string? FirstCellStyle { get; set; }
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
public PdfParagraphAlignmentType? FirstCellParagraphAlignment { get; set; }
public string? RowHeight { get; set; }
}
}

View File

@ -75,17 +75,36 @@ namespace ShabComponentsLibrary.OfficePackage
for (int i = 0; i < RowParameters.Texts.Count; ++i)
{
Row.Cells[i].AddParagraph(RowParameters.Texts[i]);
if (!string.IsNullOrEmpty(RowParameters.Style))
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 = 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].Format.Alignment = GetParagraphAlignment(RowParameters.ParagraphAlignment);
// Row.Cells[i].Format.Alignment = GetParagraphAlignment(RowParameters.ParagraphAlignment);
Row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}

View File

@ -0,0 +1,36 @@
namespace ShabComponentsLibrary
{
partial class ShabConfigurableTableComponent
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,98 @@
using ShabComponentsLibrary.OfficePackage;
using ShabComponentsLibrary.OfficePackage.HelperEnums;
using ShabComponentsLibrary.OfficePackage.HelperModels;
using System.ComponentModel;
namespace ShabComponentsLibrary
{
/// <summary>
/// Невизуальный компонент для создания документа с таблицей,
/// у которой шапкой является первая строка и первый столбец
/// </summary>
public partial class ShabConfigurableTableComponent : Component
{
public ShabConfigurableTableComponent()
{
InitializeComponent();
}
public ShabConfigurableTableComponent(IContainer Container)
{
Container.Add(this);
InitializeComponent();
}
public void CreateDocument<T>(string Filename, string Title, List<string> ColumnWidths, List<string> RowHeights,
List<string> TableHeaders, List<string> HeaderPropertyNames, List<T> Data)
{
if (string.IsNullOrEmpty(Filename))
throw new ArgumentException("Filename cannot be empty");
if (string.IsNullOrEmpty(Title))
throw new ArgumentException("Title cannot be empty");
if (TableHeaders.Count == 0)
throw new ArgumentException("Headers cannot be empty");
if (Data.Count == 0)
throw new ArgumentException("Data cannot be empty");
if (ColumnWidths.Count != TableHeaders.Count)
throw new ArgumentException("Column widths count must match headers count");
if (RowHeights.Count != 2)
throw new ArgumentException("Row heights should be specified for header row and other rows");
if (HeaderPropertyNames.Count != TableHeaders.Count)
throw new ArgumentException("Each column must have a corresponding property mapping");
SaveToPdf Pdf = new SaveToPdf();
Pdf.CreatePdf();
Pdf.CreateParagraph(new PdfParagraph
{
Text = Title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
Pdf.CreateTable(ColumnWidths);
// Шапка
Pdf.CreateRow(new PdfRowParameters
{
Texts = TableHeaders,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center,
RowHeight = RowHeights[0]
});
// Данные
foreach (var Object in Data)
{
List<string> Cells = new List<string>();
for (int ColumnIndex = 0; ColumnIndex < TableHeaders.Count; ++ColumnIndex)
{
string PropertyName = HeaderPropertyNames[ColumnIndex];
var Property = typeof(T).GetProperty(PropertyName);
if (Property is null)
{
throw new ArgumentException($"Failed to find property {PropertyName} in provided objects class");
}
var PropertyValue = Property.GetValue(Object)?.ToString();
if (PropertyValue is not null)
{
Cells.Add(PropertyValue);
}
}
Pdf.CreateRow(new PdfRowParameters
{
Texts = Cells,
Style = "Normal",
FirstCellStyle = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Left,
FirstCellParagraphAlignment = PdfParagraphAlignmentType.Center,
RowHeight = RowHeights[1]
});
}
Pdf.SavePdf(Filename);
}
}
}

View File

@ -1,8 +1,7 @@
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;
using ShabComponentsLibrary.OfficePackage;
using ShabComponentsLibrary.OfficePackage;
using ShabComponentsLibrary.OfficePackage.HelperEnums;
using ShabComponentsLibrary.OfficePackage.HelperModels;
using System.ComponentModel;
namespace ShabComponentsLibrary
{
@ -47,7 +46,7 @@ namespace ShabComponentsLibrary
{
Texts = Row.ToList(),
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Center
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
}
Pdf.CreateParagraph(new PdfParagraph());