251 lines
8.5 KiB
C#
251 lines
8.5 KiB
C#
using AircraftPlantBusinessLogic.OfficePackage.HelperEnums;
|
||
using AircraftPlantBusinessLogic.OfficePackage.HelperModels;
|
||
using DocumentFormat.OpenXml;
|
||
using DocumentFormat.OpenXml.Packaging;
|
||
using DocumentFormat.OpenXml.Wordprocessing;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AircraftPlantBusinessLogic.OfficePackage.Implements
|
||
{
|
||
/// <summary>
|
||
/// Реализация абстрактного класса для сохранения Word-файла
|
||
/// </summary>
|
||
public class SaveToWord : AbstractSaveToWord
|
||
{
|
||
/// <summary>
|
||
/// Документ
|
||
/// </summary>
|
||
private WordprocessingDocument? _wordDocument;
|
||
|
||
/// <summary>
|
||
/// Тело документа
|
||
/// </summary>
|
||
private Body? _docBody;
|
||
|
||
/// <summary>
|
||
/// Таблица
|
||
/// </summary>
|
||
private Table? _table;
|
||
|
||
/// <summary>
|
||
/// Получение типа выравнивания текста
|
||
/// </summary>
|
||
/// <param name="type"></param>
|
||
/// <returns></returns>
|
||
private static JustificationValues GetJustificationValues(WordJustificationType type)
|
||
{
|
||
return type switch
|
||
{
|
||
WordJustificationType.Both => JustificationValues.Both,
|
||
WordJustificationType.Center => JustificationValues.Center,
|
||
_ => JustificationValues.Left,
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Настройки станицы
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private static SectionProperties CreateSectionProperties()
|
||
{
|
||
var properties = new SectionProperties();
|
||
|
||
var pageSize = new PageSize
|
||
{
|
||
Orient = PageOrientationValues.Portrait
|
||
};
|
||
|
||
properties.AppendChild(pageSize);
|
||
|
||
return properties;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Задание форматирования для абзаца
|
||
/// </summary>
|
||
/// <param name="paragraphProperties"></param>
|
||
/// <returns></returns>
|
||
private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties)
|
||
{
|
||
if (paragraphProperties == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var properties = new ParagraphProperties();
|
||
|
||
properties.AppendChild(new Justification()
|
||
{
|
||
Val = GetJustificationValues(paragraphProperties.JustificationType)
|
||
});
|
||
|
||
properties.AppendChild(new SpacingBetweenLines
|
||
{
|
||
LineRule = LineSpacingRuleValues.Auto
|
||
});
|
||
|
||
properties.AppendChild(new Indentation());
|
||
|
||
var paragraphMarkRunProperties = new ParagraphMarkRunProperties();
|
||
if (!string.IsNullOrEmpty(paragraphProperties.Size))
|
||
{
|
||
paragraphMarkRunProperties.AppendChild(new FontSize { Val = paragraphProperties.Size });
|
||
}
|
||
properties.AppendChild(paragraphMarkRunProperties);
|
||
|
||
return properties;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание doc-файла
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
protected override void CreateWord(WordInfo info)
|
||
{
|
||
_wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document);
|
||
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
|
||
mainPart.Document = new Document();
|
||
_docBody = mainPart.Document.AppendChild(new Body());
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание абзаца с текстом
|
||
/// </summary>
|
||
/// <param name="paragraph"></param>
|
||
protected override void CreateParagraph(WordParagraph paragraph)
|
||
{
|
||
if (_docBody == null || paragraph == null)
|
||
{
|
||
return;
|
||
}
|
||
var docParagraph = new Paragraph();
|
||
|
||
docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));
|
||
|
||
foreach (var run in paragraph.Texts)
|
||
{
|
||
var docRun = new Run();
|
||
|
||
var properties = new RunProperties();
|
||
properties.AppendChild(new FontSize { Val = run.Item2.Size });
|
||
if (run.Item2.Bold)
|
||
{
|
||
properties.AppendChild(new Bold());
|
||
}
|
||
docRun.AppendChild(properties);
|
||
|
||
docRun.AppendChild(new Text { Text = run.Item1, Space = SpaceProcessingModeValues.Preserve });
|
||
|
||
docParagraph.AppendChild(docRun);
|
||
}
|
||
|
||
_docBody.AppendChild(docParagraph);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание таблицы
|
||
/// </summary>
|
||
/// <param name="columns"></param>
|
||
protected override void CreateTable(List<string> columns)
|
||
{
|
||
if (_docBody == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_table = new Table();
|
||
|
||
var tableProperties = new TableProperties();
|
||
tableProperties.AppendChild(new TableLayout { Type = TableLayoutValues.Fixed });
|
||
tableProperties.AppendChild(new TableBorders(
|
||
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },
|
||
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },
|
||
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },
|
||
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },
|
||
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 },
|
||
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 4 }
|
||
));
|
||
tableProperties.AppendChild(new TableWidth { Type = TableWidthUnitValues.Auto });
|
||
_table.AppendChild(tableProperties);
|
||
|
||
TableGrid tableGrid = new TableGrid();
|
||
foreach (var column in columns)
|
||
{
|
||
tableGrid.AppendChild(new GridColumn() { Width = column });
|
||
}
|
||
_table.AppendChild(tableGrid);
|
||
|
||
_docBody.AppendChild(_table);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание строки
|
||
/// </summary>
|
||
/// <param name="row"></param>
|
||
protected override void CreateRow(WordRow row)
|
||
{
|
||
if (_docBody == null || _table == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
TableRow docRow = new TableRow();
|
||
foreach (var column in row.Texts)
|
||
{
|
||
var docParagraph = new Paragraph();
|
||
WordParagraph paragraph = new WordParagraph
|
||
{
|
||
Texts = new List<(string, WordTextProperties)> { (column, row.TextProperties) },
|
||
TextProperties = row.TextProperties
|
||
};
|
||
|
||
docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));
|
||
|
||
foreach (var run in paragraph.Texts)
|
||
{
|
||
var docRun = new Run();
|
||
|
||
var properties = new RunProperties();
|
||
properties.AppendChild(new FontSize { Val = run.Item2.Size });
|
||
if (run.Item2.Bold)
|
||
{
|
||
properties.AppendChild(new Bold());
|
||
}
|
||
docRun.AppendChild(properties);
|
||
|
||
docRun.AppendChild(new Text { Text = run.Item1, Space = SpaceProcessingModeValues.Preserve });
|
||
|
||
docParagraph.AppendChild(docRun);
|
||
}
|
||
|
||
TableCell docCell = new TableCell();
|
||
docCell.AppendChild(docParagraph);
|
||
docRow.AppendChild(docCell);
|
||
}
|
||
|
||
_table.AppendChild(docRow);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Сохранение файла
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
protected override void SaveWord(WordInfo info)
|
||
{
|
||
if (_docBody == null || _wordDocument == null)
|
||
{
|
||
return;
|
||
}
|
||
_docBody.AppendChild(CreateSectionProperties());
|
||
|
||
_wordDocument.MainDocumentPart!.Document.Save();
|
||
_wordDocument.Dispose();
|
||
}
|
||
}
|
||
}
|