From 2068af18d038091300365fd07301d0df5163497d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Fri, 20 Oct 2023 09:00:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B4=D1=84=20=D1=81=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestView/Form1.Designer.cs | 28 ++++++- TestView/Form1.cs | 18 ++++- TestView/Form1.resx | 6 ++ TestView/TestView.csproj | 4 + .../NotVisualComponents/ImagesForPDF.cs | 22 ++++++ .../NotVisualComponents/PdfImages.Designer.cs | 36 +++++++++ .../NotVisualComponents/PdfImages.cs | 73 +++++++++++++++++++ ViewComponents/ViewComponents.csproj | 7 +- .../Input_text.Designer.cs | 0 .../{ => VisualComponents}/Input_text.cs | 0 .../{ => VisualComponents}/Input_text.resx | 0 .../List_with_choice.Designer.cs | 0 .../List_with_choice.cs | 0 .../List_with_choice.resx | 0 .../MyTreeView.Designer.cs | 0 .../{ => VisualComponents}/MyTreeView.cs | 0 .../{ => VisualComponents}/MyTreeView.resx | 0 17 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 ViewComponents/NotVisualComponents/ImagesForPDF.cs create mode 100644 ViewComponents/NotVisualComponents/PdfImages.Designer.cs create mode 100644 ViewComponents/NotVisualComponents/PdfImages.cs rename ViewComponents/{ => VisualComponents}/Input_text.Designer.cs (100%) rename ViewComponents/{ => VisualComponents}/Input_text.cs (100%) rename ViewComponents/{ => VisualComponents}/Input_text.resx (100%) rename ViewComponents/{ => VisualComponents}/List_with_choice.Designer.cs (100%) rename ViewComponents/{ => VisualComponents}/List_with_choice.cs (100%) rename ViewComponents/{ => VisualComponents}/List_with_choice.resx (100%) rename ViewComponents/{ => VisualComponents}/MyTreeView.Designer.cs (100%) rename ViewComponents/{ => VisualComponents}/MyTreeView.cs (100%) rename ViewComponents/{ => VisualComponents}/MyTreeView.resx (100%) diff --git a/TestView/Form1.Designer.cs b/TestView/Form1.Designer.cs index a6590f3..47e9a48 100644 --- a/TestView/Form1.Designer.cs +++ b/TestView/Form1.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + components = new System.ComponentModel.Container(); buttonAdd = new Button(); buttonClear = new Button(); buttonSelect = new Button(); @@ -42,6 +43,9 @@ buttonGetIndex = new Button(); buttonSetIndex = new Button(); myTreeView = new ViewComponents.MyTreeView(); + buttonPdfImages = new Button(); + pdfImages1 = new ViewComponents.NotVisualComponents.PdfImages(components); + openFileDialog1 = new OpenFileDialog(); SuspendLayout(); // // buttonAdd @@ -85,7 +89,10 @@ // // input_text // + input_text.Element = "Range exeption"; input_text.Location = new Point(312, 12); + input_text.MaxLen = -1; + input_text.MinLen = -1; input_text.Name = "input_text"; input_text.Size = new Size(240, 119); input_text.TabIndex = 5; @@ -179,11 +186,27 @@ myTreeView.Size = new Size(226, 204); myTreeView.TabIndex = 8; // + // buttonPdfImages + // + buttonPdfImages.Location = new Point(60, 363); + buttonPdfImages.Name = "buttonPdfImages"; + buttonPdfImages.Size = new Size(197, 29); + buttonPdfImages.TabIndex = 15; + buttonPdfImages.Text = "Загрузить изображения"; + buttonPdfImages.UseVisualStyleBackColor = true; + buttonPdfImages.Click += buttonPdfImages_Click; + // + // openFileDialog1 + // + openFileDialog1.FileName = "openFileDialog1"; + openFileDialog1.Multiselect = true; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(909, 450); + ClientSize = new Size(899, 600); + Controls.Add(buttonPdfImages); Controls.Add(myTreeView); Controls.Add(buttonSetIndex); Controls.Add(buttonGetIndex); @@ -220,5 +243,8 @@ private Button buttonGetIndex; private Button buttonSetIndex; private ViewComponents.MyTreeView myTreeView; + private Button buttonPdfImages; + private ViewComponents.NotVisualComponents.PdfImages pdfImages1; + private OpenFileDialog openFileDialog1; } } \ No newline at end of file diff --git a/TestView/Form1.cs b/TestView/Form1.cs index a5d65e0..ec3dded 100644 --- a/TestView/Form1.cs +++ b/TestView/Form1.cs @@ -1,4 +1,6 @@ +using System.Windows.Forms; using ViewComponents.Exeption; +using ViewComponents.NotVisualComponents; namespace TestView { @@ -59,7 +61,7 @@ namespace TestView throw new TextBoundsNotSetExeption(" "); if (input_text.Element.Equals("Value exeption")) throw new TextOutOfBoundsExeption(" "); - + MessageBox.Show(input_text.Element, " "); } catch (TextBoundsNotSetExeption ex) @@ -80,7 +82,7 @@ namespace TestView private void buttonAddBook_Click(object sender, EventArgs e) { - + myTreeView.addItem(new Book("", " ..", "̸ ")); myTreeView.addItem(new Book("", " ..", " ")); myTreeView.addItem(new Book("", " ", "1984")); @@ -108,5 +110,17 @@ namespace TestView { } + + private void buttonPdfImages_Click(object sender, EventArgs e) + { + var res = openFileDialog1.ShowDialog(this); + if (res != DialogResult.OK) return; + var files = openFileDialog1.FileNames; + openFileDialog1.Dispose(); + string path = "C:\\Users\\xarla\\OneDrive\\\\test.pdf"; + MessageBox.Show(path); + if (pdfImages1.CreatePdfDoc(new ImagesForPDF(path, " ", files))) MessageBox.Show("!"); + else MessageBox.Show(", "); + } } } \ No newline at end of file diff --git a/TestView/Form1.resx b/TestView/Form1.resx index f298a7b..c599dda 100644 --- a/TestView/Form1.resx +++ b/TestView/Form1.resx @@ -57,4 +57,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 150, 17 + \ No newline at end of file diff --git a/TestView/TestView.csproj b/TestView/TestView.csproj index 6a6365a..306de08 100644 --- a/TestView/TestView.csproj +++ b/TestView/TestView.csproj @@ -9,6 +9,10 @@ TestView + + + + diff --git a/ViewComponents/NotVisualComponents/ImagesForPDF.cs b/ViewComponents/NotVisualComponents/ImagesForPDF.cs new file mode 100644 index 0000000..a9d7657 --- /dev/null +++ b/ViewComponents/NotVisualComponents/ImagesForPDF.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ViewComponents.NotVisualComponents +{ + public class ImagesForPDF + { + public ImagesForPDF(string filepath, string header, string[] imagepaths) + { + this.filepath = filepath; + this.header = header; + this.imagepaths = imagepaths; + } + + public string filepath; + public string header; + public string[] imagepaths; + } +} diff --git a/ViewComponents/NotVisualComponents/PdfImages.Designer.cs b/ViewComponents/NotVisualComponents/PdfImages.Designer.cs new file mode 100644 index 0000000..9ad24d4 --- /dev/null +++ b/ViewComponents/NotVisualComponents/PdfImages.Designer.cs @@ -0,0 +1,36 @@ +namespace ViewComponents.NotVisualComponents +{ + partial class PdfImages + { + /// + /// Обязательная переменная конструктора. + /// + 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/ViewComponents/NotVisualComponents/PdfImages.cs b/ViewComponents/NotVisualComponents/PdfImages.cs new file mode 100644 index 0000000..e9e13b8 --- /dev/null +++ b/ViewComponents/NotVisualComponents/PdfImages.cs @@ -0,0 +1,73 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.Rendering; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ViewComponents.NotVisualComponents +{ + public partial class PdfImages : Component + { + public PdfImages() + { + InitializeComponent(); + } + + public PdfImages(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public bool CreatePdfDoc (ImagesForPDF imagesForPDF) + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + CheckFileExsists(imagesForPDF); + // создание документа + Document _document = new Document(); + var style = _document.Styles["Normal"]; + style.Font.Name = "Arial"; + style.Font.Size = 25; + style = _document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + + //добавление заголовка + var section = _document.AddSection(); + var paragraph = section.AddParagraph(imagesForPDF.header); + paragraph.Format.Alignment = ParagraphAlignment.Center; + paragraph.Format.SpaceAfter = "2cm"; + + //добавление изображений + foreach (string path in imagesForPDF.imagepaths) + if (File.Exists(path)) section.AddImage(path); + + //сохранение документа + PdfDocumentRenderer renderer = new PdfDocumentRenderer(true); + renderer.Document = _document; + renderer.RenderDocument(); + renderer.PdfDocument.Save(imagesForPDF.filepath); + + return true; + } + + private void CheckFileExsists(ImagesForPDF imagesForPDF) + { + if (string.IsNullOrEmpty(imagesForPDF.filepath) || string.IsNullOrEmpty(imagesForPDF.header) || imagesForPDF.imagepaths.Length == 0) + { + throw new ArgumentNullException(); + } + if (!File.Exists(imagesForPDF.filepath)) + { + throw new FileNotFoundException(imagesForPDF.filepath); + } + } + + } +} diff --git a/ViewComponents/ViewComponents.csproj b/ViewComponents/ViewComponents.csproj index cb2cc1c..d699f21 100644 --- a/ViewComponents/ViewComponents.csproj +++ b/ViewComponents/ViewComponents.csproj @@ -8,7 +8,12 @@ - + + + + + + UserControl TestView diff --git a/ViewComponents/Input_text.Designer.cs b/ViewComponents/VisualComponents/Input_text.Designer.cs similarity index 100% rename from ViewComponents/Input_text.Designer.cs rename to ViewComponents/VisualComponents/Input_text.Designer.cs diff --git a/ViewComponents/Input_text.cs b/ViewComponents/VisualComponents/Input_text.cs similarity index 100% rename from ViewComponents/Input_text.cs rename to ViewComponents/VisualComponents/Input_text.cs diff --git a/ViewComponents/Input_text.resx b/ViewComponents/VisualComponents/Input_text.resx similarity index 100% rename from ViewComponents/Input_text.resx rename to ViewComponents/VisualComponents/Input_text.resx diff --git a/ViewComponents/List_with_choice.Designer.cs b/ViewComponents/VisualComponents/List_with_choice.Designer.cs similarity index 100% rename from ViewComponents/List_with_choice.Designer.cs rename to ViewComponents/VisualComponents/List_with_choice.Designer.cs diff --git a/ViewComponents/List_with_choice.cs b/ViewComponents/VisualComponents/List_with_choice.cs similarity index 100% rename from ViewComponents/List_with_choice.cs rename to ViewComponents/VisualComponents/List_with_choice.cs diff --git a/ViewComponents/List_with_choice.resx b/ViewComponents/VisualComponents/List_with_choice.resx similarity index 100% rename from ViewComponents/List_with_choice.resx rename to ViewComponents/VisualComponents/List_with_choice.resx diff --git a/ViewComponents/MyTreeView.Designer.cs b/ViewComponents/VisualComponents/MyTreeView.Designer.cs similarity index 100% rename from ViewComponents/MyTreeView.Designer.cs rename to ViewComponents/VisualComponents/MyTreeView.Designer.cs diff --git a/ViewComponents/MyTreeView.cs b/ViewComponents/VisualComponents/MyTreeView.cs similarity index 100% rename from ViewComponents/MyTreeView.cs rename to ViewComponents/VisualComponents/MyTreeView.cs diff --git a/ViewComponents/MyTreeView.resx b/ViewComponents/VisualComponents/MyTreeView.resx similarity index 100% rename from ViewComponents/MyTreeView.resx rename to ViewComponents/VisualComponents/MyTreeView.resx