PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopBusinessLogic/OfficePackage/Implements/SaveToWord.cs

132 lines
4.4 KiB
C#
Raw Normal View History

2024-04-07 10:17:23 +04:00
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using GiftShopBusinessLogic.OfficePackage.HelperEnums;
using GiftShopBusinessLogic.OfficePackage.HelperModels;
namespace GiftShopBusinessLogic.OfficePackage.Implements
{
public class SaveToWord : AbstractSaveToWord
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
2024-04-09 17:02:29 +04:00
/// <summary>
2024-04-07 10:17:23 +04:00
/// Получение типа выравнивания
2024-04-09 17:02:29 +04:00
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
2024-04-07 10:17:23 +04:00
private static JustificationValues GetJustificationValues(WordJustificationType type)
{
return type switch
{
WordJustificationType.Both => JustificationValues.Both,
WordJustificationType.Center => JustificationValues.Center,
_ => JustificationValues.Left,
};
}
2024-04-09 17:02:29 +04:00
/// <summary>
2024-04-07 10:17:23 +04:00
/// Настройки страницы
2024-04-09 17:02:29 +04:00
/// </summary>
/// <returns></returns>
2024-04-07 10:17:23 +04:00
private static SectionProperties CreateSectionProperties()
{
var properties = new SectionProperties();
var pageSize = new PageSize
{
Orient = PageOrientationValues.Portrait
};
properties.AppendChild(pageSize);
return properties;
}
2024-04-09 17:02:29 +04:00
/// <summary>
2024-04-07 10:17:23 +04:00
/// Задание форматирования для абзаца
2024-04-09 17:02:29 +04:00
/// </summary>
/// <param name="paragraphProperties"></param>
/// <returns></returns>
2024-04-07 10:17:23 +04:00
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;
}
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());
}
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);
}
protected override void SaveWord(WordInfo info)
{
if (_docBody == null || _wordDocument == null)
{
return;
}
_docBody.AppendChild(CreateSectionProperties());
_wordDocument.MainDocumentPart!.Document.Save();
_wordDocument.Close();
}
}
}