PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairBusinessLogic/OfficePackage/Implements/SaveToWord.cs

136 lines
4.5 KiB
C#
Raw Normal View History

2024-04-10 10:37:22 +04:00
using DocumentFormat.OpenXml;
2024-03-27 11:04:28 +04:00
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
2024-04-10 10:37:22 +04:00
using PlumbingRepairBusinessLogic.OfficePackage.HelperEnum;
using PlumbingRepairBusinessLogic.OfficePackage.HelperModels;
using PlumbingRepairBusinessLogic.OfficePackage;
2024-03-27 11:04:28 +04:00
namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
{
public class SaveToWord : AbstractSaveToWord
{
private WordprocessingDocument? _wordDocument;
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
private Body? _docBody;
2024-04-10 10:37:22 +04:00
/// <summary>
/// Получение типа выравнивания
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
2024-03-27 11:04:28 +04:00
private static JustificationValues GetJustificationValues(WordJustificationType type)
{
return type switch
{
WordJustificationType.Both => JustificationValues.Both,
WordJustificationType.Center => JustificationValues.Center,
_ => JustificationValues.Left,
};
}
2024-04-10 10:37:22 +04:00
/// <summary>
/// Настройки страницы
/// </summary>
/// <returns></returns>
2024-03-27 11:04:28 +04:00
private static SectionProperties CreateSectionProperties()
{
var properties = new SectionProperties();
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
var pageSize = new PageSize
{
Orient = PageOrientationValues.Portrait
};
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
properties.AppendChild(pageSize);
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
return properties;
}
2024-04-10 10:37:22 +04:00
/// <summary>
/// Задание форматирования для абзаца
/// </summary>
/// <param name="paragraphProperties"></param>
/// <returns></returns>
2024-03-27 11:04:28 +04:00
private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties)
{
if (paragraphProperties == null)
{
return null;
}
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
var properties = new ParagraphProperties();
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
properties.AppendChild(new Justification()
{
2024-04-10 10:37:22 +04:00
Val = GetJustificationValues(paragraphProperties.JustificationType)
2024-03-27 11:04:28 +04:00
});
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
properties.AppendChild(new SpacingBetweenLines
{
LineRule = LineSpacingRuleValues.Auto
});
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
properties.AppendChild(new Indentation());
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
var paragraphMarkRunProperties = new ParagraphMarkRunProperties();
if (!string.IsNullOrEmpty(paragraphProperties.Size))
{
2024-04-10 10:37:22 +04:00
paragraphMarkRunProperties.AppendChild(new FontSize { Val = paragraphProperties.Size });
2024-03-27 11:04:28 +04:00
}
properties.AppendChild(paragraphMarkRunProperties);
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
return properties;
}
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
protected override void CreateWord(WordInfo info)
{
2024-04-10 10:37:22 +04:00
_wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document);
2024-03-27 11:04:28 +04:00
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
_docBody = mainPart.Document.AppendChild(new Body());
}
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
protected override void CreateParagraph(WordParagraph paragraph)
{
if (_docBody == null || paragraph == null)
{
return;
}
var docParagraph = new Paragraph();
docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
foreach (var run in paragraph.Texts)
{
var docRun = new Run();
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
var properties = new RunProperties();
properties.AppendChild(new FontSize { Val = run.Item2.Size });
if (run.Item2.Bold)
{
properties.AppendChild(new Bold());
}
docRun.AppendChild(properties);
2024-04-10 10:37:22 +04:00
docRun.AppendChild(new Text { Text = run.Item1, Space = SpaceProcessingModeValues.Preserve });
2024-03-27 11:04:28 +04:00
docParagraph.AppendChild(docRun);
}
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
_docBody.AppendChild(docParagraph);
}
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
protected override void SaveWord(WordInfo info)
{
if (_docBody == null || _wordDocument == null)
{
return;
}
_docBody.AppendChild(CreateSectionProperties());
2024-04-10 10:37:22 +04:00
2024-03-27 11:04:28 +04:00
_wordDocument.MainDocumentPart!.Document.Save();
2024-04-10 10:37:22 +04:00
2024-04-05 13:25:11 +04:00
_wordDocument.Dispose();
2024-03-27 11:04:28 +04:00
}
}
2024-04-10 10:37:22 +04:00
}