105 lines
2.7 KiB
C#
105 lines
2.7 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|