Mk0.Software.Massendrucker/Massendrucker/Main.cs
Manuel Kamper 75524f22bb initial
2019-03-08 16:22:20 +01:00

182 lines
6.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace Massendrucker
{
public partial class Main : Form
{
string tempDocPath = Path.Combine(Path.GetTempPath(), "OKB_PrintDocs");
public Main()
{
InitializeComponent();
CreateTempPath();
DruckerLaden(comboBox1);
}
private void CreateTempPath()
{
if(!Directory.Exists(tempDocPath))
{
Directory.CreateDirectory(tempDocPath);
}
else
{
foreach (string fileName in Directory.GetFiles(tempDocPath))
{
File.Delete(fileName);
}
}
}
private void DruckerLaden(ComboBox cb)
{
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
cb.Items.Add(PrinterSettings.InstalledPrinters[i].ToString());
}
cb.SelectedIndex = 0;
}
private void Button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
foreach (string fileName in Directory.GetFiles(tempDocPath))
{
File.Delete(fileName);
}
UnzipDocs(textBox1.Text, tempDocPath);
listBox1.DataSource = LoadDocList(tempDocPath);
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SetSelected(i, true);
}
}
private void UnzipDocs(string zip, string target)
{
if(zip!="")
{
using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Read, Encoding.GetEncoding(850)))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
entry.ExtractToFile(Path.Combine(target, entry.Name));
}
}
}
}
private List<string> LoadDocList(string docPath)
{
List<string> docs = new List<string>();
foreach (string fileName in Directory.GetFiles(tempDocPath))
{
string file = Path.GetFileName(fileName);
string ext = Path.GetExtension(fileName).ToLower();
if (ext == ".doc" || ext == ".docx")
{
docs.Add(file);
}
}
return docs;
}
private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count > 0)
{
button2.Enabled = true;
}
else
{
button2.Enabled = false;
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
Directory.Delete(tempDocPath, true);
}
private void Button2_Click(object sender, EventArgs e)
{
comboBox1.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
listBox1.Enabled = false;
radioButton1.Enabled = false;
radioButton2.Enabled = false;
numericUpDown1.Enabled = false;
progressBar1.Visible = true;
progressBar1.Maximum = listBox1.SelectedItems.Count;
object missing = Missing.Value;
object saveChanges = false;
Word.Application word = new Word.Application
{
ActivePrinter = comboBox1.Text,
Visible = false
};
foreach (string file in listBox1.SelectedItems)
{
Drucken(file, word);
progressBar1.PerformStep();
}
word.Quit(ref saveChanges, ref missing, ref missing);
MessageBox.Show("Alle Druckvorgänge sind abgeschlossen!", "ÖKB Massendrucker", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Visible = false;
progressBar1.Value = 0;
radioButton1.Enabled = true;
radioButton2.Enabled = true;
comboBox1.Enabled = true;
listBox1.Enabled = true;
button1.Enabled = true;
if (listBox1.SelectedItems.Count > 0)
{
button2.Enabled = true;
}
else
{
button2.Enabled = false;
}
numericUpDown1.Enabled = radioButton2.Checked;
if (numericUpDown1.Enabled) { numericUpDown1.Value += 1; }
}
private void Drucken(string file, Word.Application word)
{
object missing = Missing.Value;
object saveChanges = false;
object template = Path.Combine(tempDocPath, file);
object oFalse = false;
object oRange = (radioButton2.Checked) ? Word.WdPrintOutRange.wdPrintRangeOfPages : Word.WdPrintOutRange.wdPrintAllDocument;
object oPages = (radioButton2.Checked) ? numericUpDown1.Value.ToString() : missing;
word.Documents.Add(ref template, ref missing, ref missing, ref missing);
word.Application.PrintOut(ref oFalse, ref missing, ref oRange,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref oPages, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);
word.Documents.Close(ref saveChanges, ref missing, ref missing);
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
numericUpDown1.Enabled = radioButton2.Checked;
}
}
}