done tables component

This commit is contained in:
ShabOl 2024-09-24 19:20:38 +04:00
parent 1ca6980b3a
commit 04a9c7bd89
10 changed files with 280 additions and 1 deletions

View File

@ -28,6 +28,7 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container();
SelectComponent = new ShabComponentsLibrary.ShabSelectComponent(); SelectComponent = new ShabComponentsLibrary.ShabSelectComponent();
InputComponent = new ShabComponentsLibrary.ShabInputComponent(); InputComponent = new ShabComponentsLibrary.ShabInputComponent();
GridComponent = new ShabComponentsLibrary.ShabListOutputComponent(); GridComponent = new ShabComponentsLibrary.ShabListOutputComponent();
@ -37,6 +38,8 @@
ShowIntButton = new Button(); ShowIntButton = new Button();
groupBox3 = new GroupBox(); groupBox3 = new GroupBox();
PrintObjectButton = new Button(); PrintObjectButton = new Button();
CreateDocumentWithTables = new Button();
DocumentContextComponent = new ShabComponentsLibrary.ShabDocumentContextComponent(components);
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
groupBox2.SuspendLayout(); groupBox2.SuspendLayout();
groupBox3.SuspendLayout(); groupBox3.SuspendLayout();
@ -132,11 +135,22 @@
PrintObjectButton.UseVisualStyleBackColor = true; PrintObjectButton.UseVisualStyleBackColor = true;
PrintObjectButton.Click += PrintObjectButton_Click; 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 // Form1
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(637, 581); ClientSize = new Size(637, 660);
Controls.Add(CreateDocumentWithTables);
Controls.Add(groupBox3); Controls.Add(groupBox3);
Controls.Add(groupBox2); Controls.Add(groupBox2);
Controls.Add(groupBox1); Controls.Add(groupBox1);
@ -159,5 +173,7 @@
private Button ShowIntButton; private Button ShowIntButton;
private GroupBox groupBox3; private GroupBox groupBox3;
private Button PrintObjectButton; private Button PrintObjectButton;
private Button CreateDocumentWithTables;
private ShabComponentsLibrary.ShabDocumentContextComponent DocumentContextComponent;
} }
} }

View File

@ -97,5 +97,27 @@ namespace FormsTesting
var Test = GridComponent.GetSelectedObject<TestPerson>(); var Test = GridComponent.GetSelectedObject<TestPerson>();
MessageBox.Show(Test.ToString()); 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<string[][]> { table1, table2 });
}
} }
} }

View File

@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="DocumentContextComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@ -0,0 +1,9 @@
namespace ShabComponentsLibrary.OfficePackage.HelperEnums
{
internal enum PdfParagraphAlignmentType
{
Center,
Left,
Rigth
}
}

View File

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

View File

@ -0,0 +1,13 @@
using ShabComponentsLibrary.OfficePackage.HelperEnums;
namespace ShabComponentsLibrary.OfficePackage.HelperModels
{
internal class PdfRowParameters
{
public List<string> Texts { get; set; } = new();
public string Style { get; set; } = string.Empty;
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
}
}

View File

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

View File

@ -7,4 +7,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,36 @@
namespace ShabComponentsLibrary
{
partial class ShabDocumentContextComponent
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -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
{
/// <summary>
/// Невизуальный компонент для создания документа с таблицами
/// </summary>
public partial class ShabDocumentContextComponent : Component
{
public ShabDocumentContextComponent()
{
InitializeComponent();
}
public ShabDocumentContextComponent(IContainer Container)
{
Container.Add(this);
InitializeComponent();
}
public void CreateDocument(string Filename, string Title, List<string[][]> 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);
}
}
}