73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using FormLibrary.HelperClasses;
|
|
using MigraDoc.DocumentObjectModel;
|
|
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();
|
|
}
|
|
|
|
public void GeneratePdf(PdfDocumentData pdfData)
|
|
{
|
|
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("Необходимо передать хотя бы одну таблицу.");
|
|
|
|
Document document = new Document();
|
|
Section section = document.AddSection();
|
|
|
|
Paragraph title = section.AddParagraph();
|
|
title.AddFormattedText(pdfData.DocumentTitle, TextFormat.Bold);
|
|
title.Format.Alignment = ParagraphAlignment.Center;
|
|
section.AddParagraph();
|
|
|
|
foreach (var tableData in pdfData.Tables)
|
|
{
|
|
Table table = section.AddTable();
|
|
int columnsCount = tableData.GetLength(1);
|
|
|
|
for (int i = 0; i < columnsCount; i++)
|
|
{
|
|
Column column = table.AddColumn(Unit.FromCentimeter(3));
|
|
}
|
|
|
|
table.Borders.Width = 0.75;
|
|
table.Borders.Color = Colors.Black;
|
|
|
|
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]);
|
|
}
|
|
}
|
|
|
|
section.AddParagraph();
|
|
}
|
|
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
|
|
pdfRenderer.Document = document;
|
|
pdfRenderer.RenderDocument();
|
|
pdfRenderer.PdfDocument.Save(pdfData.FileName);
|
|
}
|
|
|
|
}
|
|
}
|