add packages, 1 nonvisual comp of 3 is done
This commit is contained in:
parent
637271039c
commit
9cfa4fdf2a
@ -7,4 +7,9 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="SaveToPdfHelpers\" />
|
||||
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components.Exceptions
|
||||
{
|
||||
public class UncheckedNullException : Exception
|
||||
{
|
@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components.Exceptions
|
||||
{
|
||||
public class UnexpectedTypeException : Exception
|
||||
{
|
36
Components/Nonvisual/UserControlTableDocument.Designer.cs
generated
Normal file
36
Components/Nonvisual/UserControlTableDocument.Designer.cs
generated
Normal file
@ -0,0 +1,36 @@
|
||||
namespace Components
|
||||
{
|
||||
partial class UserControlTableDocument
|
||||
{
|
||||
/// <summary>
|
||||
/// Обязательная переменная конструктора.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Освободить все используемые ресурсы.
|
||||
/// </summary>
|
||||
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Код, автоматически созданный конструктором компонентов
|
||||
|
||||
/// <summary>
|
||||
/// Требуемый метод для поддержки конструктора — не изменяйте
|
||||
/// содержимое этого метода с помощью редактора кода.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
36
Components/Nonvisual/UserControlTableDocument.cs
Normal file
36
Components/Nonvisual/UserControlTableDocument.cs
Normal file
@ -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<string[][]> 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);
|
||||
}
|
||||
}
|
||||
}
|
117
Components/SaveToPdf.cs
Normal file
117
Components/SaveToPdf.cs
Normal file
@ -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<string[][]> 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<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Components/SaveToPdfHelpers/PdfParagraph.cs
Normal file
9
Components/SaveToPdfHelpers/PdfParagraph.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
9
Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs
Normal file
9
Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Components.SaveToPdfHelpers
|
||||
{
|
||||
public enum PdfParagraphAlignmentType
|
||||
{
|
||||
Center,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
}
|
10
Components/SaveToPdfHelpers/PdfRowParameters.cs
Normal file
10
Components/SaveToPdfHelpers/PdfRowParameters.cs
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
namespace Components.SaveToPdfHelpers
|
||||
{
|
||||
public class PdfRowParameters
|
||||
{
|
||||
public List<string> Texts { get; set; } = new();
|
||||
public string Style { get; set; } = string.Empty;
|
||||
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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)
|
@ -1,4 +1,4 @@
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components
|
||||
{
|
||||
partial class UserControlIntegerInput
|
||||
{
|
@ -8,7 +8,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components
|
||||
{
|
||||
public partial class UserControlIntegerInput : UserControl
|
||||
{
|
@ -1,4 +1,4 @@
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components
|
||||
{
|
||||
partial class UserControlStringsListBox
|
||||
{
|
@ -8,7 +8,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components
|
||||
{
|
||||
public partial class UserControlStringsListBox : UserControl
|
||||
{
|
@ -1,4 +1,4 @@
|
||||
namespace WinFormsLibrary1
|
||||
namespace Components
|
||||
{
|
||||
partial class UserControlTable
|
||||
{
|
@ -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
|
||||
{
|
Loading…
Reference in New Issue
Block a user