COP_labs/Labs/WordBigText.cs

108 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.ExtendedProperties;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Labs.HelperClasses.WordBigText;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Labs
{
public partial class WordBigText : Component
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
public WordBigText()
{
InitializeComponent();
}
public WordBigText(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateWord(WordBigTextConfig config)
{
if (string.IsNullOrEmpty(config.FilePath) || string.IsNullOrEmpty(config.DocumentTitle) || config.Paragraphs == null || config.Paragraphs.Length <= 0)
{
throw new ArgumentException("Не заполнены все параметры для создания");
}
// Создание пустого word файла
_wordDocument = WordprocessingDocument.Create(config.FilePath, WordprocessingDocumentType.Document);
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
_docBody = mainPart.Document.AppendChild(new Body());
//Создание заголовка
//Св-ва заголовка
ParagraphProperties paragraphProperties = new ParagraphProperties();
//выравнивание по середине
paragraphProperties.AppendChild(new Justification
{
Val = JustificationValues.Center
});
Paragraph header = new Paragraph();
header.AppendChild(paragraphProperties);
// Добавление текста заголовка
Run run = new Run();
// Свойства абзаца (заголовка) через RunProperties
RunProperties runProperties = new RunProperties();
//сам шрифт в 2 раза меньше
runProperties.AppendChild(new FontSize { Val = "48" });
runProperties.AppendChild(new Bold());
run.AppendChild(runProperties);
run.AppendChild(new Text(config.DocumentTitle));
header.AppendChild(run);
// Заголовок в тело документа
_docBody.AppendChild(header);
// Добавление основного текста
foreach (var par in config.Paragraphs)
{
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
// Выравнивание по ширине
paragraphProperties2.AppendChild(new Justification
{
Val = JustificationValues.Both
});
Paragraph paragraph = new Paragraph();
paragraph.AppendChild(paragraphProperties2);
Run run2 = new Run();
// Свойства шрифта через RunProperties
RunProperties runProperties2 = new RunProperties();
//сам шрифт в 2 раза меньше
runProperties2.AppendChild(new FontSize { Val = "32" });
run2.AppendChild(runProperties2);
run2.AppendChild(new Text(par));
paragraph.AppendChild(run2);
_docBody.AppendChild(paragraph);
}
_wordDocument.Save();
_wordDocument.Dispose();
}
}
}