2024-10-28 20:23:38 +04:00
|
|
|
|
using ComponentsLibrary.entities;
|
|
|
|
|
using DocumentFormat.OpenXml;
|
2024-09-30 22:43:40 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
2024-09-30 23:54:03 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
2024-09-30 22:43:40 +04:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ComponentsLibrary
|
|
|
|
|
{
|
2024-10-28 20:23:38 +04:00
|
|
|
|
public partial class ComponentBigText : Component
|
2024-09-30 22:43:40 +04:00
|
|
|
|
{
|
|
|
|
|
public ComponentBigText()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentBigText(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 20:23:38 +04:00
|
|
|
|
public void CreateWordText(DocumentSymple docInfo)
|
2024-09-30 22:43:40 +04:00
|
|
|
|
{
|
2024-10-28 20:23:38 +04:00
|
|
|
|
if (string.IsNullOrEmpty(docInfo.FileUrl) || string.IsNullOrEmpty(docInfo.Title) || !CheckData(docInfo.Text))
|
2024-09-30 23:54:03 +04:00
|
|
|
|
{
|
2024-10-28 20:23:38 +04:00
|
|
|
|
throw new Exception("Не все данные заполнены");
|
2024-09-30 23:54:03 +04:00
|
|
|
|
}
|
2024-09-30 22:43:40 +04:00
|
|
|
|
|
2024-10-28 20:23:38 +04:00
|
|
|
|
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(docInfo.FileUrl, WordprocessingDocumentType.Document))
|
2024-09-30 22:43:40 +04:00
|
|
|
|
{
|
2024-10-28 20:23:38 +04:00
|
|
|
|
// Добавляем главную часть документа
|
2024-09-30 22:43:40 +04:00
|
|
|
|
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
|
|
|
|
|
mainPart.Document = new Document();
|
2024-10-28 20:23:38 +04:00
|
|
|
|
Body body = mainPart.Document.AppendChild(new Body());
|
|
|
|
|
|
|
|
|
|
// Создаем параграф для заголовка
|
|
|
|
|
Paragraph titleParagraph = new Paragraph();
|
|
|
|
|
|
|
|
|
|
// Задаем свойства параграфа (центровка и отступ)
|
|
|
|
|
ParagraphProperties paragraphProperties = new ParagraphProperties();
|
|
|
|
|
paragraphProperties.AppendChild(new Justification() { Val = JustificationValues.Center });
|
|
|
|
|
|
|
|
|
|
// Применяем свойства параграфа к заголовку
|
|
|
|
|
titleParagraph.AppendChild(paragraphProperties);
|
|
|
|
|
|
|
|
|
|
// Создаем "бегунок" текста
|
|
|
|
|
Run titleRun = new Run();
|
|
|
|
|
|
|
|
|
|
// Устанавливаем свойства "бегунка" (шрифт, размер, жирный шрифт)
|
|
|
|
|
RunProperties runProperties = new RunProperties();
|
|
|
|
|
runProperties.AppendChild(new Bold());
|
|
|
|
|
runProperties.AppendChild(new FontSize() { Val = "48" }); // Размер шрифта, 24pt
|
|
|
|
|
|
|
|
|
|
// Применяем свойства к тексту
|
|
|
|
|
titleRun.AppendChild(runProperties);
|
|
|
|
|
|
|
|
|
|
// Добавляем текст заголовка
|
|
|
|
|
titleRun.AppendChild(new Text(docInfo.Title));
|
2024-09-30 22:43:40 +04:00
|
|
|
|
|
2024-10-28 20:23:38 +04:00
|
|
|
|
// Добавляем "бегунок" с текстом в параграф
|
|
|
|
|
titleParagraph.AppendChild(titleRun);
|
2024-09-30 23:54:03 +04:00
|
|
|
|
|
2024-10-28 20:23:38 +04:00
|
|
|
|
// Добавляем параграф в тело документа
|
|
|
|
|
body.AppendChild(titleParagraph);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var line in docInfo.Text)
|
2024-09-30 22:43:40 +04:00
|
|
|
|
{
|
|
|
|
|
Paragraph paragraph = new Paragraph(new Run(new Text(line)));
|
|
|
|
|
body.Append(paragraph);
|
|
|
|
|
}
|
2024-10-28 20:23:38 +04:00
|
|
|
|
mainPart.Document.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-30 22:43:40 +04:00
|
|
|
|
|
2024-10-28 20:23:38 +04:00
|
|
|
|
bool CheckData(string[] data)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(data[i])) return false;
|
2024-09-30 22:43:40 +04:00
|
|
|
|
}
|
2024-10-28 20:23:38 +04:00
|
|
|
|
|
|
|
|
|
return true;
|
2024-09-30 22:43:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|