Made long text component

This commit is contained in:
ksenianeva 2023-11-02 19:56:58 +04:00
parent fa6498b47d
commit 502eb76c23
7 changed files with 189 additions and 3 deletions

View File

@ -0,0 +1,36 @@
namespace NevaevaLibrary.LogicalComponents
{
partial class WordLongTextComponent
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using static System.Net.Mime.MediaTypeNames;
namespace NevaevaLibrary.LogicalComponents
{
public partial class WordLongTextComponent : Component
{
public WordLongTextComponent()
{
InitializeComponent();
}
public WordLongTextComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private object missing = System.Reflection.Missing.Value;
public void createWithLongText(WordLongTextInfo wordInfo)
{
var winword = new Microsoft.Office.Interop.Word.Application();
var document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
document.PageSetup.RightMargin = 50;
document.PageSetup.LeftMargin = 50;
var header = document.Content.Paragraphs.Add(Type.Missing);
header.Range.Text = wordInfo.header;
header.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
header.Range.Font.Name = "Times New Roman";
header.Range.Font.Size = 22;
header.Range.Font.Bold = 2;
header.Format.SpaceAfter = 18;
header.Range.InsertParagraphAfter();
foreach (string text in wordInfo.paragraphs)
{
var paragraph = document.Content.Paragraphs.Add(Type.Missing);
paragraph.Range.Text = text;
paragraph.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.Font.Name = "Times New Roman";
paragraph.Range.Font.Size = 14;
paragraph.Range.Font.Bold = 0;
paragraph.Format.SpaceAfter = 18;
paragraph.Range.InsertParagraphAfter();
}
document.SaveAs(wordInfo.path, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NevaevaLibrary.LogicalComponents
{
public class WordLongTextInfo
{
public string path;
public string header;
public string[] paragraphs;
public WordLongTextInfo(string path, string header, string[] paragraphs)
{
this.path = path;
this.header = header;
this.paragraphs = paragraphs;
}
}
}

View File

@ -7,4 +7,25 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Interop.Access.Dao">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>12</VersionMajor>
<Guid>4ac9e1da-5bad-4ac7-86e3-24f4cdceca28</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<COMReference Include="Microsoft.Office.Interop.Word">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>7</VersionMinor>
<VersionMajor>8</VersionMajor>
<Guid>00020905-0000-0000-c000-000000000046</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
</Project> </Project>

View File

@ -28,6 +28,7 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container();
this.comboBoxControl = new NevaevaLibrary.ComboBoxControl(); this.comboBoxControl = new NevaevaLibrary.ComboBoxControl();
this.buttonInsert = new System.Windows.Forms.Button(); this.buttonInsert = new System.Windows.Forms.Button();
this.buttonClear = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button();
@ -38,6 +39,9 @@
this.listBoxControl = new NevaevaLibrary.ListBoxControl(); this.listBoxControl = new NevaevaLibrary.ListBoxControl();
this.buttonInsertList = new System.Windows.Forms.Button(); this.buttonInsertList = new System.Windows.Forms.Button();
this.buttonGetSelectedList = new System.Windows.Forms.Button(); this.buttonGetSelectedList = new System.Windows.Forms.Button();
this.buttonWordText = new System.Windows.Forms.Button();
this.wordLongTextComponent = new NevaevaLibrary.LogicalComponents.WordLongTextComponent(this.components);
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout(); this.SuspendLayout();
// //
// comboBoxControl // comboBoxControl
@ -91,7 +95,7 @@
// //
// mailControl // mailControl
// //
this.mailControl.Email = ""; this.mailControl.Email = null;
this.mailControl.Location = new System.Drawing.Point(11, 95); this.mailControl.Location = new System.Drawing.Point(11, 95);
this.mailControl.Name = "mailControl"; this.mailControl.Name = "mailControl";
this.mailControl.Size = new System.Drawing.Size(312, 94); this.mailControl.Size = new System.Drawing.Size(312, 94);
@ -136,11 +140,26 @@
this.buttonGetSelectedList.UseVisualStyleBackColor = true; this.buttonGetSelectedList.UseVisualStyleBackColor = true;
this.buttonGetSelectedList.Click += new System.EventHandler(this.buttonGetSelectedList_Click); this.buttonGetSelectedList.Click += new System.EventHandler(this.buttonGetSelectedList_Click);
// //
// buttonWordText
//
this.buttonWordText.Location = new System.Drawing.Point(10, 319);
this.buttonWordText.Name = "buttonWordText";
this.buttonWordText.Size = new System.Drawing.Size(145, 29);
this.buttonWordText.TabIndex = 10;
this.buttonWordText.Text = "Word (текст)";
this.buttonWordText.UseVisualStyleBackColor = true;
this.buttonWordText.Click += new System.EventHandler(this.buttonWordText_Click);
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog1";
//
// FormTest // FormTest
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1109, 316); this.ClientSize = new System.Drawing.Size(1109, 363);
this.Controls.Add(this.buttonWordText);
this.Controls.Add(this.buttonGetSelectedList); this.Controls.Add(this.buttonGetSelectedList);
this.Controls.Add(this.buttonInsertList); this.Controls.Add(this.buttonInsertList);
this.Controls.Add(this.listBoxControl); this.Controls.Add(this.listBoxControl);
@ -169,5 +188,8 @@
private NevaevaLibrary.ListBoxControl listBoxControl; private NevaevaLibrary.ListBoxControl listBoxControl;
private Button buttonInsertList; private Button buttonInsertList;
private Button buttonGetSelectedList; private Button buttonGetSelectedList;
private Button buttonWordText;
private NevaevaLibrary.LogicalComponents.WordLongTextComponent wordLongTextComponent;
private OpenFileDialog openFileDialog;
} }
} }

View File

@ -1,4 +1,5 @@
using System; using NevaevaLibrary.LogicalComponents;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -66,5 +67,15 @@ namespace TestApp
Worker? worker = listBoxControl.getSelectedItem<Worker>(); Worker? worker = listBoxControl.getSelectedItem<Worker>();
if (worker is not null) MessageBox.Show(worker.ToString() + $"\n{worker.name}, {worker.department}, {worker.workYears}"); if (worker is not null) MessageBox.Show(worker.ToString() + $"\n{worker.name}, {worker.department}, {worker.workYears}");
} }
private void buttonWordText_Click(object sender, EventArgs e)
{
string[] paragraphs = { "test1", "Составлен в соответствии с учебным планом направления 09.03.04. Цель данного практикума ориентировать студентов на содержание и порядок выполнения лабораторных задач во время прохождения ими курсов «Методы искусственного интеллекта» и «Машинное обучение». Даются задания на лабораторные работы. ",
"Работа подготовлена на кафедре «Информационные системы»." };
openFileDialog.Dispose();
string path = AppDomain.CurrentDomain.BaseDirectory + "test.docx";
wordLongTextComponent.createWithLongText(new WordLongTextInfo(path, "Header", paragraphs));
MessageBox.Show("Готово!");
}
} }
} }

View File

@ -57,4 +57,10 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="wordLongTextComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>248, 17</value>
</metadata>
</root> </root>