From 05fd1f373b337d6a3dceaf18522d1cb634d0db37 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 24 Sep 2024 23:54:36 +0400 Subject: [PATCH] done configurable table component --- FormsTesting/Form1.Designer.cs | 15 +++ FormsTesting/Form1.cs | 45 ++++++--- FormsTesting/Form1.resx | 3 + .../HelperModels/PdfRowParameters.cs | 6 ++ .../OfficePackage/SaveToPdf.cs | 23 ++++- ...ShabConfigurableTableComponent.Designer.cs | 36 +++++++ .../ShabConfigurableTableComponent.cs | 98 +++++++++++++++++++ .../ShabDocumentContextComponent.cs | 7 +- 8 files changed, 215 insertions(+), 18 deletions(-) create mode 100644 ShabComponentsLibrary/ShabConfigurableTableComponent.Designer.cs create mode 100644 ShabComponentsLibrary/ShabConfigurableTableComponent.cs diff --git a/FormsTesting/Form1.Designer.cs b/FormsTesting/Form1.Designer.cs index 109a5c9..1c2bbaf 100644 --- a/FormsTesting/Form1.Designer.cs +++ b/FormsTesting/Form1.Designer.cs @@ -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; } } diff --git a/FormsTesting/Form1.cs b/FormsTesting/Form1.cs index 9404fae..d890b0d 100644 --- a/FormsTesting/Form1.cs +++ b/FormsTesting/Form1.cs @@ -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 { table1, table2 }); } + + private void CreateConfigurableTable_Click(object sender, EventArgs e) + { + List Persons = new List + { + 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 { "3cm", "2cm", "3cm", "4cm" }, + new List { "1cm", "0.6cm" }, + new List { "", ".", "", "" }, + new List { "LastName", "Id", "FirstName", "Age" }, + Persons); + } } } diff --git a/FormsTesting/Form1.resx b/FormsTesting/Form1.resx index 9613a61..25bd66e 100644 --- a/FormsTesting/Form1.resx +++ b/FormsTesting/Form1.resx @@ -120,4 +120,7 @@ 17, 17 + + 233, 17 + \ No newline at end of file diff --git a/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs index 8dab937..79fead7 100644 --- a/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs +++ b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs @@ -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; } } } diff --git a/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs index 9ca2763..83dea25 100644 --- a/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs +++ b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs @@ -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; } } diff --git a/ShabComponentsLibrary/ShabConfigurableTableComponent.Designer.cs b/ShabComponentsLibrary/ShabConfigurableTableComponent.Designer.cs new file mode 100644 index 0000000..42134e6 --- /dev/null +++ b/ShabComponentsLibrary/ShabConfigurableTableComponent.Designer.cs @@ -0,0 +1,36 @@ +namespace ShabComponentsLibrary +{ + partial class ShabConfigurableTableComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/ShabComponentsLibrary/ShabConfigurableTableComponent.cs b/ShabComponentsLibrary/ShabConfigurableTableComponent.cs new file mode 100644 index 0000000..9988c00 --- /dev/null +++ b/ShabComponentsLibrary/ShabConfigurableTableComponent.cs @@ -0,0 +1,98 @@ +using ShabComponentsLibrary.OfficePackage; +using ShabComponentsLibrary.OfficePackage.HelperEnums; +using ShabComponentsLibrary.OfficePackage.HelperModels; +using System.ComponentModel; + +namespace ShabComponentsLibrary +{ + /// + /// Невизуальный компонент для создания документа с таблицей, + /// у которой шапкой является первая строка и первый столбец + /// + public partial class ShabConfigurableTableComponent : Component + { + public ShabConfigurableTableComponent() + { + InitializeComponent(); + } + + public ShabConfigurableTableComponent(IContainer Container) + { + Container.Add(this); + InitializeComponent(); + } + + public void CreateDocument(string Filename, string Title, List ColumnWidths, List RowHeights, + List TableHeaders, List HeaderPropertyNames, List 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 Cells = new List(); + + 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); + } + } +} diff --git a/ShabComponentsLibrary/ShabDocumentContextComponent.cs b/ShabComponentsLibrary/ShabDocumentContextComponent.cs index 6e6dc86..eae8b6c 100644 --- a/ShabComponentsLibrary/ShabDocumentContextComponent.cs +++ b/ShabComponentsLibrary/ShabDocumentContextComponent.cs @@ -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());