77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using MigraDoc.DocumentObjectModel.Tables;
|
|
using MigraDoc.DocumentObjectModel;
|
|
using MigraDoc.Rendering;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Document = MigraDoc.DocumentObjectModel.Document;
|
|
using Components.SaveToPdfHelpers;
|
|
|
|
namespace Components.NonVisual
|
|
{
|
|
public partial class TablePDF : Component
|
|
{
|
|
public TablePDF()
|
|
{
|
|
InitializeComponent();
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
}
|
|
|
|
public TablePDF(IContainer container)
|
|
{
|
|
container.Add(this);
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|