128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using DocumentFormat.OpenXml.Packaging;
|
||
using DocumentFormat.OpenXml.Wordprocessing;
|
||
using DocumentFormat.OpenXml;
|
||
using System.ComponentModel;
|
||
using COP_1.cop_2.helpers;
|
||
|
||
namespace COP_1.cop_2
|
||
{
|
||
public partial class WordBigText : Component
|
||
{
|
||
private WordprocessingDocument? _wordDocument;
|
||
private Body? _docBody;
|
||
|
||
public WordBigText()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public WordBigText(IContainer container)
|
||
{
|
||
container.Add(this);
|
||
InitializeComponent();
|
||
}
|
||
|
||
public void CreateWordText(WordBigTextInfo largeText)
|
||
{
|
||
if (string.IsNullOrEmpty(largeText.FilePath) || string.IsNullOrEmpty(largeText.Title) || !CheckData(largeText.Paragraphs))
|
||
{
|
||
throw new Exception("Не все данные заполнены");
|
||
}
|
||
_wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document);
|
||
|
||
//вытаскиваем главную часть из вордовского документа
|
||
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
|
||
|
||
mainPart.Document = new Document();
|
||
|
||
//генерируем тело основной части документа
|
||
_docBody = mainPart.Document.AppendChild(new Body());
|
||
|
||
_wordDocument.Dispose();
|
||
|
||
AddText(largeText);
|
||
}
|
||
|
||
private void AddText(WordBigTextInfo largeText)
|
||
{
|
||
using (var document = WordprocessingDocument.Open(largeText.FilePath, true))
|
||
{
|
||
var doc = document.MainDocumentPart.Document;
|
||
//Создание заголовка
|
||
|
||
ParagraphProperties paragraphProperties = new();
|
||
|
||
paragraphProperties.AppendChild(new Justification
|
||
{
|
||
Val = JustificationValues.Center
|
||
});
|
||
|
||
paragraphProperties.AppendChild(new Indentation());
|
||
|
||
Paragraph header = new();
|
||
|
||
header.AppendChild(paragraphProperties);
|
||
|
||
var docRun = new Run();
|
||
|
||
var properties = new RunProperties();
|
||
|
||
properties.AppendChild(new FontSize
|
||
{
|
||
Val = "48"
|
||
});
|
||
|
||
properties.AppendChild(new Bold());
|
||
|
||
docRun.AppendChild(properties);
|
||
|
||
docRun.AppendChild(new Text(largeText.Title));
|
||
|
||
header.AppendChild(docRun);
|
||
doc.Body.Append(header);
|
||
|
||
//Создание текста
|
||
|
||
for (int i = 0; i < largeText.Paragraphs.Length; i++)
|
||
{
|
||
ParagraphProperties paragraphProperties2 = new();
|
||
|
||
paragraphProperties2.AppendChild(new Justification
|
||
{
|
||
Val = JustificationValues.Both
|
||
});
|
||
|
||
paragraphProperties2.AppendChild(new Indentation());
|
||
|
||
Paragraph text = new();
|
||
|
||
text.AppendChild(paragraphProperties2);
|
||
|
||
var docRun2 = new Run();
|
||
|
||
var properties2 = new RunProperties();
|
||
|
||
properties2.AppendChild(new FontSize { Val = "24"});
|
||
|
||
docRun2.AppendChild(properties2);
|
||
docRun2.AppendChild(new Text(largeText.Paragraphs[i]));
|
||
|
||
text.AppendChild(docRun2);
|
||
doc.Body.Append(text);
|
||
}
|
||
doc.Save();
|
||
}
|
||
}
|
||
|
||
bool CheckData(string[] data)
|
||
{
|
||
for (int i = 0; i < data.Length; i++)
|
||
{
|
||
if (string.IsNullOrEmpty(data[i])) return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|
||
}
|