diff --git a/Components/Components.csproj b/Components/Components.csproj index 3d78086..3b33f9a 100644 --- a/Components/Components.csproj +++ b/Components/Components.csproj @@ -7,4 +7,9 @@ enable + + + + + diff --git a/Components/UncheckedNullException.cs b/Components/Exceptions/UncheckedNullException.cs similarity index 90% rename from Components/UncheckedNullException.cs rename to Components/Exceptions/UncheckedNullException.cs index 73b6e8c..e66fdae 100644 --- a/Components/UncheckedNullException.cs +++ b/Components/Exceptions/UncheckedNullException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Exceptions { public class UncheckedNullException : Exception { diff --git a/Components/UnexpectedTypeException.cs b/Components/Exceptions/UnexpectedTypeException.cs similarity index 90% rename from Components/UnexpectedTypeException.cs rename to Components/Exceptions/UnexpectedTypeException.cs index 19eace9..e742c0a 100644 --- a/Components/UnexpectedTypeException.cs +++ b/Components/Exceptions/UnexpectedTypeException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Exceptions { public class UnexpectedTypeException : Exception { diff --git a/Components/Nonvisual/UserControlTableDocument.Designer.cs b/Components/Nonvisual/UserControlTableDocument.Designer.cs new file mode 100644 index 0000000..525ee88 --- /dev/null +++ b/Components/Nonvisual/UserControlTableDocument.Designer.cs @@ -0,0 +1,36 @@ +namespace Components +{ + partial class UserControlTableDocument + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/Components/Nonvisual/UserControlTableDocument.cs b/Components/Nonvisual/UserControlTableDocument.cs new file mode 100644 index 0000000..a1ce594 --- /dev/null +++ b/Components/Nonvisual/UserControlTableDocument.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Components +{ + public partial class UserControlTableDocument : Component + { + public UserControlTableDocument() + { + InitializeComponent(); + } + + public UserControlTableDocument(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void SaveToDocument(string filePath, string header, List tables) + { + if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException("Empty string instead of path to file"); + if (string.IsNullOrEmpty(header)) throw new ArgumentNullException("Header string is empty"); + if (tables.Count == 0) throw new ArgumentNullException("Table list is empty"); + + SaveToPdf saver = new SaveToPdf(); + + saver.CreateDoc(filePath, header, tables); + } + } +} diff --git a/Components/SaveToPdf.cs b/Components/SaveToPdf.cs new file mode 100644 index 0000000..7ac3f44 --- /dev/null +++ b/Components/SaveToPdf.cs @@ -0,0 +1,117 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using Components.SaveToPdfHelpers; + +namespace Components +{ + internal class SaveToPdf + { + private Document? _document; + private Section? _section; + private Table? _table; + public void CreateDoc(string title, string path, List tables) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + + CreateParagraph(new PdfParagraph + { + Text = title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + + foreach (var table in tables) + { + if (table.Length == 0) continue; + + CreateTable(Enumerable.Repeat("3cm", table[0].Length).ToList()); + + foreach (var row in table) + { + CreateRow(new PdfRowParameters + { + Texts = row.ToList(), + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + + CreateParagraph(new PdfParagraph()); + } + + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(path); + } + private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Right => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + protected void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + protected void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + protected void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + 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].VerticalAlignment = VerticalAlignment.Center; + } + } + } +} diff --git a/Components/SaveToPdfHelpers/PdfParagraph.cs b/Components/SaveToPdfHelpers/PdfParagraph.cs new file mode 100644 index 0000000..ac1f2ab --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfParagraph.cs @@ -0,0 +1,9 @@ +namespace Components.SaveToPdfHelpers +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs b/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..bde3c13 --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs @@ -0,0 +1,9 @@ +namespace Components.SaveToPdfHelpers +{ + public enum PdfParagraphAlignmentType + { + Center, + Left, + Right + } +} diff --git a/Components/SaveToPdfHelpers/PdfRowParameters.cs b/Components/SaveToPdfHelpers/PdfRowParameters.cs new file mode 100644 index 0000000..76435a6 --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfRowParameters.cs @@ -0,0 +1,10 @@ + +namespace Components.SaveToPdfHelpers +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/Components/User.cs b/Components/User.cs deleted file mode 100644 index 8a521b8..0000000 --- a/Components/User.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WinFormsLibrary1 -{ - public class User - { - public string username { get; private set; } - public string email { get; private set; } - public DateTime birthday { get; private set; } - - public User(string username, string email, DateTime birthday) - { - this.username = username; - this.email = email; - this.birthday = birthday; - } - - } -} diff --git a/Components/ColumnInfo.cs b/Components/Visual/ColumnInfo.cs similarity index 89% rename from Components/ColumnInfo.cs rename to Components/Visual/ColumnInfo.cs index f504008..553b5dd 100644 --- a/Components/ColumnInfo.cs +++ b/Components/Visual/ColumnInfo.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Visual { public class ColumnInfo { public string HeaderText { get; set; } = string.Empty; - public int Width { get; set; } + public int Width { get; set; } public bool Visible { get; set; } public string Name { get; set; } = string.Empty; public ColumnInfo(string headerText, string name, int width, bool visible) diff --git a/Components/UserControlIntegerInput.Designer.cs b/Components/Visual/UserControlIntegerInput.Designer.cs similarity index 98% rename from Components/UserControlIntegerInput.Designer.cs rename to Components/Visual/UserControlIntegerInput.Designer.cs index e978ffc..5a4661d 100644 --- a/Components/UserControlIntegerInput.Designer.cs +++ b/Components/Visual/UserControlIntegerInput.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlIntegerInput { diff --git a/Components/UserControlIntegerInput.cs b/Components/Visual/UserControlIntegerInput.cs similarity index 98% rename from Components/UserControlIntegerInput.cs rename to Components/Visual/UserControlIntegerInput.cs index 3413573..1b0d204 100644 --- a/Components/UserControlIntegerInput.cs +++ b/Components/Visual/UserControlIntegerInput.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlIntegerInput : UserControl { diff --git a/Components/UserControlIntegerInput.resx b/Components/Visual/UserControlIntegerInput.resx similarity index 100% rename from Components/UserControlIntegerInput.resx rename to Components/Visual/UserControlIntegerInput.resx diff --git a/Components/UserControlStringsListBox.Designer.cs b/Components/Visual/UserControlStringsListBox.Designer.cs similarity index 98% rename from Components/UserControlStringsListBox.Designer.cs rename to Components/Visual/UserControlStringsListBox.Designer.cs index 6b42aee..382b913 100644 --- a/Components/UserControlStringsListBox.Designer.cs +++ b/Components/Visual/UserControlStringsListBox.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlStringsListBox { diff --git a/Components/UserControlStringsListBox.cs b/Components/Visual/UserControlStringsListBox.cs similarity index 98% rename from Components/UserControlStringsListBox.cs rename to Components/Visual/UserControlStringsListBox.cs index 0cf9580..8693f27 100644 --- a/Components/UserControlStringsListBox.cs +++ b/Components/Visual/UserControlStringsListBox.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlStringsListBox : UserControl { diff --git a/Components/UserControlStringsListBox.resx b/Components/Visual/UserControlStringsListBox.resx similarity index 100% rename from Components/UserControlStringsListBox.resx rename to Components/Visual/UserControlStringsListBox.resx diff --git a/Components/UserControlTable.Designer.cs b/Components/Visual/UserControlTable.Designer.cs similarity index 98% rename from Components/UserControlTable.Designer.cs rename to Components/Visual/UserControlTable.Designer.cs index 9db1b39..01b9469 100644 --- a/Components/UserControlTable.Designer.cs +++ b/Components/Visual/UserControlTable.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlTable { diff --git a/Components/UserControlTable.cs b/Components/Visual/UserControlTable.cs similarity index 98% rename from Components/UserControlTable.cs rename to Components/Visual/UserControlTable.cs index f341e31..b5e7041 100644 --- a/Components/UserControlTable.cs +++ b/Components/Visual/UserControlTable.cs @@ -8,8 +8,9 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Components.Visual; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlTable : UserControl { diff --git a/Components/UserControlTable.resx b/Components/Visual/UserControlTable.resx similarity index 100% rename from Components/UserControlTable.resx rename to Components/Visual/UserControlTable.resx