diff --git a/FormsTesting/Form1.Designer.cs b/FormsTesting/Form1.Designer.cs index e541983..109a5c9 100644 --- a/FormsTesting/Form1.Designer.cs +++ b/FormsTesting/Form1.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + components = new System.ComponentModel.Container(); SelectComponent = new ShabComponentsLibrary.ShabSelectComponent(); InputComponent = new ShabComponentsLibrary.ShabInputComponent(); GridComponent = new ShabComponentsLibrary.ShabListOutputComponent(); @@ -37,6 +38,8 @@ ShowIntButton = new Button(); groupBox3 = new GroupBox(); PrintObjectButton = new Button(); + CreateDocumentWithTables = new Button(); + DocumentContextComponent = new ShabComponentsLibrary.ShabDocumentContextComponent(components); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); groupBox3.SuspendLayout(); @@ -132,11 +135,22 @@ PrintObjectButton.UseVisualStyleBackColor = true; PrintObjectButton.Click += PrintObjectButton_Click; // + // CreateDocumentWithTables + // + CreateDocumentWithTables.Location = new Point(18, 592); + CreateDocumentWithTables.Name = "CreateDocumentWithTables"; + CreateDocumentWithTables.Size = new Size(172, 42); + CreateDocumentWithTables.TabIndex = 6; + CreateDocumentWithTables.Text = "Документ с таблицами"; + CreateDocumentWithTables.UseVisualStyleBackColor = true; + CreateDocumentWithTables.Click += CreateDocumentWithTables_Click; + // // Form1 // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(637, 581); + ClientSize = new Size(637, 660); + Controls.Add(CreateDocumentWithTables); Controls.Add(groupBox3); Controls.Add(groupBox2); Controls.Add(groupBox1); @@ -159,5 +173,7 @@ private Button ShowIntButton; private GroupBox groupBox3; private Button PrintObjectButton; + private Button CreateDocumentWithTables; + private ShabComponentsLibrary.ShabDocumentContextComponent DocumentContextComponent; } } diff --git a/FormsTesting/Form1.cs b/FormsTesting/Form1.cs index 59a7880..9404fae 100644 --- a/FormsTesting/Form1.cs +++ b/FormsTesting/Form1.cs @@ -97,5 +97,27 @@ namespace FormsTesting var Test = GridComponent.GetSelectedObject(); MessageBox.Show(Test.ToString()); } + + 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[][] 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" }, + }; + + DocumentContextComponent.CreateDocument( + @"C:\Comps\Doc1.pdf", + "Sample text", + new List { table1, table2 }); + } } } diff --git a/FormsTesting/Form1.resx b/FormsTesting/Form1.resx index af32865..9613a61 100644 --- a/FormsTesting/Form1.resx +++ b/FormsTesting/Form1.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file diff --git a/ShabComponentsLibrary/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs b/ShabComponentsLibrary/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..c3aa009 --- /dev/null +++ b/ShabComponentsLibrary/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs @@ -0,0 +1,9 @@ +namespace ShabComponentsLibrary.OfficePackage.HelperEnums +{ + internal enum PdfParagraphAlignmentType + { + Center, + Left, + Rigth + } +} diff --git a/ShabComponentsLibrary/OfficePackage/HelperModels/PdfParagraph.cs b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..1c55f70 --- /dev/null +++ b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,13 @@ +using ShabComponentsLibrary.OfficePackage.HelperEnums; + +namespace ShabComponentsLibrary.OfficePackage.HelperModels +{ + internal class PdfParagraph + { + public string Text { get; set; } = string.Empty; + + public string Style { get; set; } = string.Empty; + + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..8dab937 --- /dev/null +++ b/ShabComponentsLibrary/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,13 @@ +using ShabComponentsLibrary.OfficePackage.HelperEnums; + +namespace ShabComponentsLibrary.OfficePackage.HelperModels +{ + internal class PdfRowParameters + { + public List Texts { get; set; } = new(); + + public string Style { get; set; } = string.Empty; + + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs new file mode 100644 index 0000000..9ca2763 --- /dev/null +++ b/ShabComponentsLibrary/OfficePackage/SaveToPdf.cs @@ -0,0 +1,104 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using ShabComponentsLibrary.OfficePackage.HelperEnums; +using ShabComponentsLibrary.OfficePackage.HelperModels; + +namespace ShabComponentsLibrary.OfficePackage +{ + internal class SaveToPdf + { + private Document? _document; + private Section? _section; + private Table? _table; + + private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType Type) + { + return Type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Rigth => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + + private static void DefineStyles(Document Document) + { + var Style = Document.Styles["Normal"]; + Style.Font.Name = "Times New Roman"; + Style.Font.Size = 12; + + Style = Document.Styles.AddStyle("NormalTitle", "Normal"); + Style.Font.Bold = true; + } + + public void CreatePdf() + { + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + _document = new Document(); + DefineStyles(_document); + + _section = _document.AddSection(); + } + + public void CreateParagraph(PdfParagraph PdfParagraph) + { + if (_section == null) + return; + + var Paragraph = _section.AddParagraph(PdfParagraph.Text); + + Paragraph.Format.SpaceAfter = "0.1cm"; + Paragraph.Format.Alignment = GetParagraphAlignment(PdfParagraph.ParagraphAlignment); + Paragraph.Style = PdfParagraph.Style; + } + + public void CreateTable(List Columns) + { + if (_document == null) + return; + + _table = _document.LastSection.AddTable(); + foreach (var Column in Columns) + { + _table.AddColumn(Column); + } + } + + public 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; + } + } + + public void SavePdf(string Filename) + { + var Renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + + Renderer.RenderDocument(); + Renderer.PdfDocument.Save(Filename); + } + } +} diff --git a/ShabComponentsLibrary/ShabComponentsLibrary.csproj b/ShabComponentsLibrary/ShabComponentsLibrary.csproj index 3e210aa..7fbd9ef 100644 --- a/ShabComponentsLibrary/ShabComponentsLibrary.csproj +++ b/ShabComponentsLibrary/ShabComponentsLibrary.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/ShabComponentsLibrary/ShabDocumentContextComponent.Designer.cs b/ShabComponentsLibrary/ShabDocumentContextComponent.Designer.cs new file mode 100644 index 0000000..00ee807 --- /dev/null +++ b/ShabComponentsLibrary/ShabDocumentContextComponent.Designer.cs @@ -0,0 +1,36 @@ +namespace ShabComponentsLibrary +{ + partial class ShabDocumentContextComponent + { + /// + /// 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/ShabDocumentContextComponent.cs b/ShabComponentsLibrary/ShabDocumentContextComponent.cs new file mode 100644 index 0000000..6e6dc86 --- /dev/null +++ b/ShabComponentsLibrary/ShabDocumentContextComponent.cs @@ -0,0 +1,59 @@ +using System.ComponentModel; +using System.Windows.Forms.VisualStyles; +using ShabComponentsLibrary.OfficePackage; +using ShabComponentsLibrary.OfficePackage.HelperEnums; +using ShabComponentsLibrary.OfficePackage.HelperModels; + +namespace ShabComponentsLibrary +{ + /// + /// Невизуальный компонент для создания документа с таблицами + /// + public partial class ShabDocumentContextComponent : Component + { + public ShabDocumentContextComponent() + { + InitializeComponent(); + } + + public ShabDocumentContextComponent(IContainer Container) + { + Container.Add(this); + InitializeComponent(); + } + + public void CreateDocument(string Filename, string Title, List Tables) + { + SaveToPdf Pdf = new SaveToPdf(); + Pdf.CreatePdf(); + Pdf.CreateParagraph(new PdfParagraph + { + Text = Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + + foreach (string[][] Table in Tables) + { + if (Table.Length == 0) + { + continue; + } + + Pdf.CreateTable(Enumerable.Repeat("3cm", Table[0].Length).ToList()); + foreach (string[] Row in Table) + { + Pdf.CreateRow(new PdfRowParameters + { + Texts = Row.ToList(), + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + } + Pdf.CreateParagraph(new PdfParagraph()); + } + + Pdf.SavePdf(Filename); + } + } +}