KOP-PIbd-32-Katysheva-N-E/ComponentsLibrary/ComponentBigText.cs

53 lines
1.2 KiB
C#
Raw Normal View History

2024-09-30 23:54:03 +04:00
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
{
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)
{
2024-09-30 23:54:03 +04:00
if (string.IsNullOrEmpty(fileUrl) || string.IsNullOrEmpty(fileName))
{
throw new ArgumentException("File URL or filename cannot be empty.");
}
2024-09-30 22:43:40 +04:00
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(Path.Combine(fileUrl, fileName), WordprocessingDocumentType.Document))
{
2024-09-30 23:54:03 +04:00
2024-09-30 22:43:40 +04:00
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
2024-09-30 23:54:03 +04:00
2024-09-30 22:43:40 +04:00
foreach (var line in text)
{
Paragraph paragraph = new Paragraph(new Run(new Text(line)));
body.Append(paragraph);
}
2024-09-30 23:54:03 +04:00
2024-09-30 22:43:40 +04:00
mainPart.Document.Append(body);
2024-09-30 23:54:03 +04:00
mainPart.Document.Save();
2024-09-30 22:43:40 +04:00
}
}
}
}