KOP-PIbd-32-Katysheva-N-E/ComponentsLibrary/ComponentBigText.cs
2024-09-30 23:54:03 +04:00

53 lines
1.2 KiB
C#

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.ComponentModel;
namespace ComponentsLibrary
{
public partial class ComponentBigText : Component
{
public ComponentBigText()
{
InitializeComponent();
}
public ComponentBigText(IContainer container)
{
container.Add(this);
InitializeComponent();
}
//публичный метод
public void SetText(string fileUrl, string fileName, string[] text)
{
if (string.IsNullOrEmpty(fileUrl) || string.IsNullOrEmpty(fileName))
{
throw new ArgumentException("File URL or filename cannot be empty.");
}
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(Path.Combine(fileUrl, fileName), WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
foreach (var line in text)
{
Paragraph paragraph = new Paragraph(new Run(new Text(line)));
body.Append(paragraph);
}
mainPart.Document.Append(body);
mainPart.Document.Save();
}
}
}
}