второй компонент тоже вроде как работает

This commit is contained in:
Kirill 2024-11-17 17:35:47 +04:00
parent c281e4e07b
commit 1cbc55bbe9
7 changed files with 230 additions and 3 deletions

View File

@ -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<T>(PdfTableInfo<T> 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<string> properties = new List<string>();
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<string> cells = new List<string>();
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);
}
}
}

View File

@ -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<T> : IPdfInfo
{
public string FileName { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public List<TreeNode> Headers { get; set; } = new();
public List<T> 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));
}
}
}

Binary file not shown.

View File

@ -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;
}
}

View File

@ -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<TreeNode> headers = new List<TreeNode>
{
new TreeNode
{
Text = "Name",
Tag = "Name",
},
new TreeNode("Info", new TreeNode[]
{
new TreeNode
{
Text = "Category",
Tag = "Category"
},
new TreeNode
{
Text = "SubCategory",
Tag = "SubCategory"
}
})
};
List<SampleClass> data = new List<SampleClass>
{
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<SampleClass>
{
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);
}
}
}
}

View File

@ -123,4 +123,7 @@
<metadata name="pdfImage.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>183, 17</value>
</metadata>
<metadata name="pdfTable.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>304, 17</value>
</metadata>
</root>

View File

@ -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;
}
}
}