diff --git a/CheckedListBoxLibrary/Components/PdfTable/PdfTable.cs b/CheckedListBoxLibrary/Components/PdfTable/PdfTable.cs index 66e832f..9d75c9b 100644 --- a/CheckedListBoxLibrary/Components/PdfTable/PdfTable.cs +++ b/CheckedListBoxLibrary/Components/PdfTable/PdfTable.cs @@ -1,4 +1,10 @@ -using System; +using MyUserControls.Components.office_package.HelperEnums; +using MyUserControls.Components.office_package.HelperModels; +using MyUserControls.Components.office_package.Implements; +using MyUserControls.Components.office_package; +using MigraDoc.DocumentObjectModel; +using MigraDoc.Rendering; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -10,6 +16,8 @@ namespace MyUserControls.Components.PdfTable { public partial class PdfTable : Component { + ISaveToPdf SaveToPdf = new SaveToPdf(); + public PdfTable() { InitializeComponent(); @@ -21,5 +29,124 @@ namespace MyUserControls.Components.PdfTable InitializeComponent(); } + public void CreatePdf(PdfTableInfo info) + { + info.CheckFields(); + SaveToPdf.CreatePdf(info); + SaveToPdf.CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + SaveToPdf.CreateTableWithColumns(info.Data.Count + 2); + SaveToPdf.CreateRow(new PdfRowParameters()); + int rowIndex = 0; + int columnIndex = 0; + List properties = new List(); + string? header; + foreach (var node in info.Headers) + { + if (string.IsNullOrWhiteSpace(node.Text)) + { + throw new Exception("Cannot read header"); + } + SaveToPdf.CreateRow(new PdfRowParameters()); + SaveToPdf.InsertTextIntoCell( + rowIndex, + columnIndex, + new PdfCellParameters + { + Text = node.Text, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + if (node.Nodes.Count == 0) + { + SaveToPdf.MergeCells(new PdfCellsMergeParameters + { + RowIndex = rowIndex, + ColumnIndex = columnIndex, + Direction = PdfCellsMergeDirections.Right + }); + header = node.Tag as string; + if (header == null) + { + throw new Exception("Cannot read property name"); + } + properties.Add(header); + rowIndex++; + } + else + { + int mergeRowIndex = rowIndex; + int mergeColumnIndex = columnIndex; + foreach (TreeNode child in node.Nodes) + { + if (string.IsNullOrWhiteSpace(child.Text)) + { + throw new Exception("Cannot read header"); + } + SaveToPdf.CreateRow(new PdfRowParameters()); + SaveToPdf.InsertTextIntoCell( + rowIndex, + columnIndex + 1, + new PdfCellParameters + { + Text = child.Text, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + header = child.Tag as string; + if (header == null) + { + throw new Exception("Cannot read property name"); + } + properties.Add(header); + rowIndex++; + } + SaveToPdf.MergeCells(new PdfCellsMergeParameters + { + RowIndex = mergeRowIndex, + ColumnIndex = mergeColumnIndex, + Direction = PdfCellsMergeDirections.Down, + CellNumber = node.Nodes.Count - 1 + }); + } + } + columnIndex = 2; + foreach (var obj in info.Data) + { + if (obj == null) + { + throw new Exception("Table Data element cannot be null"); + } + List cells = new List(); + for (int i = 0; i < properties.Count; i++) + { + string propertyName = properties[i]; + var property = obj.GetType().GetProperty(propertyName); + if (property is null) + { + throw new ArgumentException($"Failed to find property {propertyName} in provided objects class"); + } + var value = property.GetValue(obj)?.ToString(); + if (value == null) + { + throw new ArgumentException($"Failed to find property {propertyName} in provided objects class"); + } + cells.Add(value); + } + SaveToPdf.InsertTextIntoColumn(new PdfColumnParameters + { + Texts = cells, + ColumnIndex = columnIndex, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + columnIndex++; + } + SaveToPdf.SavePdf(info); + } } } diff --git a/CheckedListBoxLibrary/Components/PdfTable/PdfTableInfo.cs b/CheckedListBoxLibrary/Components/PdfTable/PdfTableInfo.cs index e03e737..a9264be 100644 --- a/CheckedListBoxLibrary/Components/PdfTable/PdfTableInfo.cs +++ b/CheckedListBoxLibrary/Components/PdfTable/PdfTableInfo.cs @@ -1,4 +1,5 @@ -using System; +using MyUserControls.Components.office_package.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,23 @@ using System.Threading.Tasks; namespace MyUserControls.Components.PdfTable { - internal class PdfTableInfo + public class PdfTableInfo : IPdfInfo { + public string FileName { get; set; } = string.Empty; + public string Title { get; set; } = string.Empty; + public List Headers { get; set; } = new(); + public List Data { get; set; } = new(); + + public void CheckFields() + { + if (string.IsNullOrWhiteSpace(FileName)) + throw new ArgumentException("Имя файла не может быть пустым.", nameof(FileName)); + if (string.IsNullOrWhiteSpace(Title)) + throw new ArgumentException("Название документа не может быть пустым.", nameof(Title)); + if (Headers == null || Headers.Count == 0) + throw new ArgumentException("Заголовки для шапки не могут быть пустыми.", nameof(Headers)); + if (Data == null || Data.Count == 0) + throw new ArgumentException("Данные для таблицы не могут быть пустыми.", nameof(Data)); + } } } diff --git a/WinFormsApp1/Documents/PdfWithTable.pdf b/WinFormsApp1/Documents/PdfWithTable.pdf new file mode 100644 index 0000000..e3c4359 Binary files /dev/null and b/WinFormsApp1/Documents/PdfWithTable.pdf differ diff --git a/WinFormsApp1/Form1.Designer.cs b/WinFormsApp1/Form1.Designer.cs index 21cdb4c..bf8e714 100644 --- a/WinFormsApp1/Form1.Designer.cs +++ b/WinFormsApp1/Form1.Designer.cs @@ -39,6 +39,8 @@ listBoxImages = new ListBox(); chooseImage = new Button(); createPdfImages = new Button(); + pdfTable = new MyUserControls.Components.PdfTable.PdfTable(components); + createTable = new Button(); SuspendLayout(); // // smartCheckedListBox1 @@ -112,11 +114,22 @@ createPdfImages.UseVisualStyleBackColor = true; createPdfImages.Click += createPdfImages_Click; // + // createTable + // + createTable.Location = new Point(330, 316); + createTable.Name = "createTable"; + createTable.Size = new Size(184, 31); + createTable.TabIndex = 9; + createTable.Text = "создать таблицу пдф"; + createTable.UseVisualStyleBackColor = true; + createTable.Click += createTable_Click; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1120, 474); + Controls.Add(createTable); Controls.Add(createPdfImages); Controls.Add(chooseImage); Controls.Add(listBoxImages); @@ -142,5 +155,7 @@ private ListBox listBoxImages; private Button chooseImage; private Button createPdfImages; + private MyUserControls.Components.PdfTable.PdfTable pdfTable; + private Button createTable; } } diff --git a/WinFormsApp1/Form1.cs b/WinFormsApp1/Form1.cs index 6535228..af090f0 100644 --- a/WinFormsApp1/Form1.cs +++ b/WinFormsApp1/Form1.cs @@ -1,6 +1,7 @@ using MyUserControls; using MyUserControls.Components.office_package.Implements; using MyUserControls.Components.PdfImage; +using MyUserControls.Components.PdfTable; using System.ComponentModel; @@ -131,5 +132,56 @@ namespace WinFormsApp1 } } + private void createTable_Click(object sender, EventArgs e) + { + try + { + List headers = new List + { + new TreeNode + { + Text = "Name", + Tag = "Name", + }, + new TreeNode("Info", new TreeNode[] + { + new TreeNode + { + Text = "Category", + Tag = "Category" + }, + new TreeNode + { + Text = "SubCategory", + Tag = "SubCategory" + } + }) + }; + List data = new List + { + new SampleClass("a1", "d1", "f1"), + new SampleClass("a2", "d2", "f2"), + new SampleClass("a3", "d3", "f3") + }; + string currentDirectory = Directory.GetCurrentDirectory(); + + string filePath = Path.Combine(currentDirectory, @"..\..\..\Documents\PdfWithTable.pdf"); + + var info = new PdfTableInfo + { + FileName = filePath, + Title = "Title", + Headers = headers, + Data = data + }; + pdfTable.CreatePdf(info); + MessageBox.Show("PDF !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + } } diff --git a/WinFormsApp1/Form1.resx b/WinFormsApp1/Form1.resx index e213689..68b4e87 100644 --- a/WinFormsApp1/Form1.resx +++ b/WinFormsApp1/Form1.resx @@ -123,4 +123,7 @@ 183, 17 + + 304, 17 + \ No newline at end of file diff --git a/WinFormsApp1/SampleClass.cs b/WinFormsApp1/SampleClass.cs index e5b18e2..59b269b 100644 --- a/WinFormsApp1/SampleClass.cs +++ b/WinFormsApp1/SampleClass.cs @@ -16,5 +16,18 @@ namespace WinFormsApp1 { return Name + " " + Category + " " + SubCategory; } + + public SampleClass() { + Name = string.Empty; + Category = string.Empty; + SubCategory = string.Empty; + } + + public SampleClass(string name, string category, string subCategory) + { + Name = name; + Category = category; + SubCategory = subCategory; + } } }