2024-10-02 18:26:36 +04:00
|
|
|
|
using FormLibrary.HelperClasses;
|
|
|
|
|
using MigraDoc.DocumentObjectModel;
|
2024-09-18 20:23:53 +04:00
|
|
|
|
using MigraDoc.DocumentObjectModel.Tables;
|
|
|
|
|
using MigraDoc.Rendering;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Document = MigraDoc.DocumentObjectModel.Document;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace FormLibrary
|
|
|
|
|
{
|
|
|
|
|
public partial class PDFTable : Component
|
|
|
|
|
{
|
|
|
|
|
public PDFTable()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PDFTable(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-10-02 18:26:36 +04:00
|
|
|
|
|
|
|
|
|
public void GeneratePdf(PdfDocumentData pdfData)
|
2024-09-18 20:23:53 +04:00
|
|
|
|
{
|
2024-10-02 18:26:36 +04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(pdfData.FileName)) throw new ArgumentException("Имя файла не может быть пустым.");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(pdfData.DocumentTitle)) throw new ArgumentException("Название документа не может быть пустым.");
|
|
|
|
|
if (pdfData.Tables == null || pdfData.Tables.Count == 0) throw new ArgumentException("Необходимо передать хотя бы одну таблицу.");
|
2024-09-18 20:23:53 +04:00
|
|
|
|
|
|
|
|
|
Document document = new Document();
|
|
|
|
|
Section section = document.AddSection();
|
|
|
|
|
|
|
|
|
|
Paragraph title = section.AddParagraph();
|
2024-10-02 18:26:36 +04:00
|
|
|
|
title.AddFormattedText(pdfData.DocumentTitle, TextFormat.Bold);
|
2024-09-18 20:23:53 +04:00
|
|
|
|
title.Format.Alignment = ParagraphAlignment.Center;
|
2024-10-02 18:26:36 +04:00
|
|
|
|
section.AddParagraph();
|
2024-09-18 20:23:53 +04:00
|
|
|
|
|
2024-10-02 18:26:36 +04:00
|
|
|
|
foreach (var tableData in pdfData.Tables)
|
2024-09-18 20:23:53 +04:00
|
|
|
|
{
|
|
|
|
|
Table table = section.AddTable();
|
|
|
|
|
int columnsCount = tableData.GetLength(1);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < columnsCount; i++)
|
|
|
|
|
{
|
2024-10-02 18:26:36 +04:00
|
|
|
|
Column column = table.AddColumn(Unit.FromCentimeter(3));
|
2024-09-18 20:23:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 18:26:36 +04:00
|
|
|
|
table.Borders.Width = 0.75;
|
|
|
|
|
table.Borders.Color = Colors.Black;
|
|
|
|
|
|
2024-09-18 20:23:53 +04:00
|
|
|
|
for (int i = 0; i < tableData.GetLength(0); i++)
|
|
|
|
|
{
|
|
|
|
|
Row row = table.AddRow();
|
|
|
|
|
for (int j = 0; j < tableData.GetLength(1); j++)
|
|
|
|
|
{
|
|
|
|
|
row.Cells[j].AddParagraph(tableData[i, j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 18:26:36 +04:00
|
|
|
|
section.AddParagraph();
|
2024-09-18 20:23:53 +04:00
|
|
|
|
}
|
|
|
|
|
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
|
|
|
|
|
pdfRenderer.Document = document;
|
|
|
|
|
pdfRenderer.RenderDocument();
|
2024-10-02 18:26:36 +04:00
|
|
|
|
pdfRenderer.PdfDocument.Save(pdfData.FileName);
|
2024-09-18 20:23:53 +04:00
|
|
|
|
}
|
2024-10-02 18:26:36 +04:00
|
|
|
|
|
2024-09-18 20:23:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|