From 4ff7ba29316819033d75dbb690c1fad9a16aa8b6 Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Wed, 11 Oct 2023 17:32:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SupportClasses/Enums/EnumAreaLegend.cs | 17 +++ .../Components/SupportClasses/LargeText.cs | 24 ++++ .../Components/WordText.Designer.cs | 36 +++++ COP/VisualCompLib/Components/WordText.cs | 123 ++++++++++++++++++ COP/VisualCompLib/VisualCompLib.csproj | 6 + COP/WinForms/FormWord.Designer.cs | 76 +++++++++++ COP/WinForms/FormWord.cs | 36 +++++ COP/WinForms/FormWord.resx | 123 ++++++++++++++++++ COP/WinForms/Program.cs | 2 +- 9 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 COP/VisualCompLib/Components/SupportClasses/Enums/EnumAreaLegend.cs create mode 100644 COP/VisualCompLib/Components/SupportClasses/LargeText.cs create mode 100644 COP/VisualCompLib/Components/WordText.Designer.cs create mode 100644 COP/VisualCompLib/Components/WordText.cs create mode 100644 COP/WinForms/FormWord.Designer.cs create mode 100644 COP/WinForms/FormWord.cs create mode 100644 COP/WinForms/FormWord.resx diff --git a/COP/VisualCompLib/Components/SupportClasses/Enums/EnumAreaLegend.cs b/COP/VisualCompLib/Components/SupportClasses/Enums/EnumAreaLegend.cs new file mode 100644 index 0000000..8b6af77 --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/Enums/EnumAreaLegend.cs @@ -0,0 +1,17 @@ +namespace VisualCompLib.Components.SupportClasses.Enums +{ + public enum EnumAreaLegend + { + None, + + Left, + + Top, + + Right, + + Bottom, + + TopRight + } +} diff --git a/COP/VisualCompLib/Components/SupportClasses/LargeText.cs b/COP/VisualCompLib/Components/SupportClasses/LargeText.cs new file mode 100644 index 0000000..35e9645 --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/LargeText.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VisualCompLib.Components.SupportClasses +{ + public class LargeText + { + public string FilePath = string.Empty; + + public string DocumentTitle = string.Empty; + + public string[] TextData; + + public LargeText(string filePath, string documentTitle, string[] textData) + { + FilePath = filePath; + DocumentTitle = documentTitle; + TextData = textData; + } + } +} diff --git a/COP/VisualCompLib/Components/WordText.Designer.cs b/COP/VisualCompLib/Components/WordText.Designer.cs new file mode 100644 index 0000000..e91353f --- /dev/null +++ b/COP/VisualCompLib/Components/WordText.Designer.cs @@ -0,0 +1,36 @@ +namespace VisualCompLib.Components +{ + partial class WordText + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/COP/VisualCompLib/Components/WordText.cs b/COP/VisualCompLib/Components/WordText.cs new file mode 100644 index 0000000..0bb53c9 --- /dev/null +++ b/COP/VisualCompLib/Components/WordText.cs @@ -0,0 +1,123 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using DocumentFormat.OpenXml; +using System.ComponentModel; +using VisualCompLib.Components.SupportClasses; + +namespace VisualCompLib.Components +{ + public partial class WordText : Component + { + private WordprocessingDocument? _wordDocument; + + private Body? _docBody; + public WordText() + { + InitializeComponent(); + } + + public WordText(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void CreateWordText(LargeText largeText) + { + if (string.IsNullOrEmpty(largeText.FilePath) || string.IsNullOrEmpty(largeText.DocumentTitle) || largeText.TextData.Length == 0) + { + return; + } + _wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document); + + //вытаскиваем главную часть из вордовского документа + MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); + + mainPart.Document = new Document(); + + //генерируем тело основной части документа + _docBody = mainPart.Document.AppendChild(new Body()); + + _wordDocument.Close(); + + AddText(largeText); + } + + private void AddText(LargeText largeText) + { + using (var document = WordprocessingDocument.Open(largeText.FilePath, true)) + { + var doc = document.MainDocumentPart.Document; + + #region Создание заголовка + + 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.DocumentTitle)); + + header.AppendChild(docRun); + doc.Body.Append(header); + #endregion + + #region Создание текста + for (int i = 0; i < largeText.TextData.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.TextData[i])); + + text.AppendChild(docRun2); + doc.Body.Append(text); + } + #endregion + doc.Save(); + } + } + } +} diff --git a/COP/VisualCompLib/VisualCompLib.csproj b/COP/VisualCompLib/VisualCompLib.csproj index 1ce33c4..4be1e4c 100644 --- a/COP/VisualCompLib/VisualCompLib.csproj +++ b/COP/VisualCompLib/VisualCompLib.csproj @@ -7,6 +7,12 @@ enable + + + + + + UserControl diff --git a/COP/WinForms/FormWord.Designer.cs b/COP/WinForms/FormWord.Designer.cs new file mode 100644 index 0000000..1c04f5b --- /dev/null +++ b/COP/WinForms/FormWord.Designer.cs @@ -0,0 +1,76 @@ +namespace WinForms +{ + partial class FormWord + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + wordText = new VisualCompLib.Components.WordText(components); + groupBox1 = new GroupBox(); + button1 = new Button(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // groupBox1 + // + groupBox1.Controls.Add(button1); + groupBox1.Location = new Point(12, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(120, 80); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Большой текст"; + // + // button1 + // + button1.Location = new Point(6, 45); + button1.Name = "button1"; + button1.Size = new Size(94, 29); + button1.TabIndex = 0; + button1.Text = "Создать"; + button1.UseVisualStyleBackColor = true; + button1.Click += button1_Click; + // + // FormWord + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(groupBox1); + Name = "FormWord"; + Text = "Невизуальные компоненты"; + groupBox1.ResumeLayout(false); + ResumeLayout(false); + } + + #endregion + + private VisualCompLib.Components.WordText wordText; + private GroupBox groupBox1; + private Button button1; + } +} \ No newline at end of file diff --git a/COP/WinForms/FormWord.cs b/COP/WinForms/FormWord.cs new file mode 100644 index 0000000..25db246 --- /dev/null +++ b/COP/WinForms/FormWord.cs @@ -0,0 +1,36 @@ +using VisualCompLib.Components.SupportClasses; + +namespace WinForms +{ + public partial class FormWord : Form + { + string[] testArray = { "Зайцев. Да и вонь же тут у вас, сеньор! Не продохнешь! Воняет сургучом, кислятиной какой-то, клопами... Пфуй!", "Смотритель. Без запаха нельзя.", "Зайцев. Завтра разбудите меня в шесть часов... И чтоб тройка была готова... Мне нужно к девяти часам в город поспеть.", "Смотритель. Ладно...", "Зайцев. Который теперь час?" }; + public FormWord() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + //фильтрация файлов для диалогового окна + using var dialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + LargeText largeText = new(dialog.FileName, "Ночь перед судом", testArray); + wordText.CreateWordText(largeText); + + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } +} diff --git a/COP/WinForms/FormWord.resx b/COP/WinForms/FormWord.resx new file mode 100644 index 0000000..5dda421 --- /dev/null +++ b/COP/WinForms/FormWord.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/COP/WinForms/Program.cs b/COP/WinForms/Program.cs index 1598162..ccc4695 100644 --- a/COP/WinForms/Program.cs +++ b/COP/WinForms/Program.cs @@ -11,7 +11,7 @@ namespace WinForms // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormForComponents()); + Application.Run(new FormWord()); } } } \ No newline at end of file