PIbd-33_Dyakonov_R_R_COP_14/WinFormsApp1/Form1.cs
dyakonovr b4035d6b13 lab2
2024-09-29 21:12:45 +04:00

234 lines
6.6 KiB
C#

using System.Security.Cryptography.Xml;
using WinFormsLibrary1;
using WinFormsLibrary1.Configs.Diagram;
using WinFormsLibrary1.Configs.Image;
using WinFormsLibrary1.Models;
using WinFormsLibrary1.Errors;
using WinFormsLibrary1.Configs.Diagram;
using WinFormsLibrary1.Configs.Table;
using PdfSharp.Pdf.Content.Objects;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private PdfWithImages pdfWithImages = new PdfWithImages();
private List<byte[]> selectedImages = new List<byte[]>();
private PdfWithTable pdfWithTable = new PdfWithTable();
private PdfWithDiagram pdfWithDiagram = new PdfWithDiagram();
public Form1()
{
InitializeComponent();
FillCustomCheckedListBox();
FillCustomDataTree();
}
private void FillCustomCheckedListBox()
{
List<string> list = new List<string>() { "Çíà÷åíèå 1", "Çíà÷åíèå 2", "Çíà÷åíèå 3" };
for (int i = 0; i < list.Count; i++)
{
customCheckedListBox.AddItem(list[i]);
}
}
private void FillCustomDataTree()
{
departamentComboBox.Items.Add("Îòäåë ïðîäàæ");
departamentComboBox.Items.Add("Îòäåë àíàëèòèêè");
departamentComboBox.Items.Add("IT îòäåë");
DataTreeNodeConfig config = new DataTreeNodeConfig(new List<string> { "Department", "Position", "Name" });
customDataTree.LoadConfig(config);
List<Employee> employees = new List<Employee>
{
new Employee("Èâàíîâ Èâàí", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
new Employee("Ïåòðîâ Ïåòð", "Àíàëèòèê", "Îòäåë àíàëèòèêè"),
new Employee("Ñèäîðîâ Ñèäîð", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
new Employee("Ìàðèÿ Ñìèðíîâà", "Ðàçðàáîò÷èê", "IT îòäåë"),
new Employee("Îëüãà Ïåòðîâà", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
};
foreach (var employee in employees)
{
customDataTree.AddObject(employee);
}
}
private void addInListboxItemButton_Click(object sender, EventArgs e)
{
string value = listboxItemValuetextBox.Text;
if (customCheckedListBox.Items.Contains(value)) customCheckedListBox.SelectedElement = value;
else customCheckedListBox.AddItem(value);
}
private void clearListboxButton_Click(object sender, EventArgs e)
{
customCheckedListBox.Clear();
}
private void getSelectedItemButton_Click(object sender, EventArgs e)
{
string value = customCheckedListBox.SelectedElement;
if (value == string.Empty) selectedItemLabel.Text = "Selected item: ~empty value~";
else selectedItemLabel.Text = "Selected item: " + value;
}
private void checkCustomTextBoxButton_Click(object sender, EventArgs e)
{
try
{
errorCustomTextBoxLabel.Text = customTextBox.Value == string.Empty ? "~empty value~" : customTextBox.Value;
}
catch (RangeNotSetException ex)
{
// Îáðàáàòûâàåì îøèáêó, åñëè äèàïàçîí íå çàäàí.
MessageBox.Show(ex.Message, "Îøèáêà äèàïàçîíà", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (TextLengthOutOfRangeException ex)
{
// Îáðàáàòûâàåì îøèáêó, åñëè äëèíà òåêñòà âíå äèàïàçîíà.
MessageBox.Show(ex.Message, "Îøèáêà äëèíû òåêñòà", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void addEmployeeButton_Click(object sender, EventArgs e)
{
if (positionTextBox.Text == null || nameTextBox.Text == null || departamentComboBox.SelectedItem == null)
{
return;
}
customDataTree.AddObject<Employee>(new(nameTextBox.Text, positionTextBox.Text, departamentComboBox.SelectedItem.ToString()));
customDataTree.Update();
}
private void getSelectedTreeItemButton_Click(object sender, EventArgs e)
{
Employee employee = customDataTree.GetSelectedObject<Employee>();
if (employee == null)
{
return;
}
positionTextBox.Text = employee.Position;
nameTextBox.Text = employee.Name;
departamentComboBox.SelectedItem = employee.Department;
}
// PDF WITH IMAGE
private void button1_Click(object sender, EventArgs e)
{
using OpenFileDialog openFileDialog = new OpenFileDialog
{
Multiselect = true,
Filter = "Èçîáðàæåíèÿ|*.jpg;*.jpeg;*.png;*.bmp"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Î÷èùàåì ïðåäûäóùèå äàííûå
selectedImages.Clear();
listBoxImages.Items.Clear();
// Äîáàâëÿåì âûáðàííûå èçîáðàæåíèÿ â ñïèñîê
foreach (string filePath in openFileDialog.FileNames)
{
selectedImages.Add(File.ReadAllBytes(filePath));
listBoxImages.Items.Add(Path.GetFileName(filePath));
}
}
}
private void createPdfButton_Click(object sender, EventArgs e)
{
if (selectedImages.Count == 0)
{
MessageBox.Show("Âûáåðèòå õîòÿ áû îäíî èçîáðàæåíèå!", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Êîíôèãóðàöèÿ äëÿ PDF-äîêóìåíòà
var config = new PdfWithImageConfig
{
FilePath = "PdfWithImage.pdf",
Header = "Äîêóìåíò ñ èçîáðàæåíèÿìè",
Images = selectedImages
};
// Ñîçäàíèå PDF ÷åðåç êîìïîíåíò
pdfWithImages.CreatePdf(config);
MessageBox.Show("PDF óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// PDF WITH TABLE
private void pdfTableButton_Click(object sender, EventArgs e)
{
PdfWithTableConfig<Human> config = new PdfWithTableConfig<Human>
{
FileName = "PdfWithTable.pdf",
DocumentTitle = "Ïðèìåð Òàáëèöû",
RowHeights = new List<double> { 1, 1, 1 },
Headers = new Dictionary<string, List<string>>
{
{ "Ëè÷íûå äàííûå", new List<string> {"Èìÿ", "Ôàìèëèÿ"} },
{ "Âîçðàñò", new List<string>() }
},
PropertiesPerRow = new List<string> { "Name", "Surname", "Age" },
Data = new List<Human>
{
new Human("Èâàí", "Èâàíîâ", 30),
new Human("Ïåòð", "Ïåòðîâ", 25),
new Human("Ñåðãåé", "Ñåðãååâ", 35)
}
};
try
{
// Âûçîâ ìåòîäà äëÿ ñîçäàíèÿ PDF
pdfWithTable.CreatePdf(config);
MessageBox.Show("PDF-äîêóìåíò óñïåøíî ñîçäàí!", "Óñïåõ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà ïðè ñîçäàíèè PDF: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// PDF TO DIAGRAM
private void pdfDiagramButton_Click(object sender, EventArgs e)
{
try
{
pdfWithDiagram.CreateDoc(new PdfWithDiagramConfig
{
FilePath = "PdfWithPieDiagram.pdf",
Header = "Çàãîëîâîê",
ChartTitle = "Êðóãîâàÿ äèàãðàììà",
LegendLocation = WinFormsLibrary1.Configs.Diagram.Location.Bottom,
Data = new Dictionary<string, List<(string Name, double Value)>>
{
{
"Product A", new List<(string Name, double Value)>
{
("1111", 30.0),
("2", 20.0),
("3", 50.0)
}
}
}
});
MessageBox.Show("PDF äîêóìåíò ñ äèàãðàììîé ñîçäàí!", "Óñïåõ");
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà: {ex.Message}");
}
}
}
}