using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

using foxit.printmanager;

namespace dotnet_demo
{
    public partial class MainForm : Form
    {
        string sn = "Rew1vB0TqoMIxX444UcpScARWdhgJpsJfzmyMLR32a0+EgrCjp2NaQ==";
        string key = "8f3g1sFttW8NAgfSNv+BvaJWy4Pp8flcm7ncNDkdvCCO4JwUFbGLyZdujReMaiplbYJwwxam3lr7uHtQaEf0xTSu8j2ZBdrZ8FS4OQkEUjOI9UEGb3Ic0GqmlP9T9OnSItOYqoZZA/OnYg5EaVP+c/ha32nUquOk+5riEdhYgINAZDdgJkPc+YSWsrfixOIROf6t34j3eRcyFnOY0MS7tSXi1AEPE0VZdyu+5bfmBSGvLI+WR5lQ+oOQR5hRGwqf990WEdBelNO7dxhZELVhEZ+/L60PpDf5y9A8qmrY6PiEU8nhqIyLpks0BxeIsYyZ3Cr82jjfbItPGyEARsIVzluBSgVMD4oGjojz8T6wGDEXUbmZH/pra94XAs8wZ28mzbmIEppWnhBqX0c6Ov8iXCfFfUZr4KNuPxMEgk4VOYTMOd70GEjGnylivw18hbn7ZpkWh2Zql+uU2HejvOgn+WHvT0HF9NhK0Xb5vH8aFkgBP92PJx8obIbxBVeeExczxvsqWufuPdXS2sPNIgMYZ2TtvqVVsiUToeGB2FQo09fzZOSH5Vi18u/siyvC3FmjeMf4+OYWPuDPNCcrb21NgcHam7XI8AAoGLTGtLLWR+fxynbBBcg2d2ZPcZ5CMFIfOBHRWCkcA0sDT3cl9nsBppHNJze/jVxHy+lREeQEA/etSIgohTGAWW3ZlpibRXqMxlmVWEPifCubbWTzQ6U54mRMpW2UghH3gvCNFNgdday3Av7EtlIJOdUuiMq3zmmd5J4Jt4C82CKnVewA0DtZ4EEtwRG9IYuLrfpuZlnjBAtoeXRV1lqUZ6BcCe0E3m9jO28sGkALuIeJQ74XG7X8lMC7lMWWQYiYD7D6G9QT6X3qV4vXJwNKRKpYc/rI+amEawTSnDYAMEaYPwtZ0rb/7BL+atsZZaI+ncebdfVfjDJZbCZAc+JnkuNlUn2xrNRuObw8h/G0lm2pNOEJ10S8R4+yplo5B1QmC2dCDhaTaW67QJPqnJPKbVmjw7i6KGBi+CDP3JnAKlOX/YUu8uk56JfeQUuGcQg/KaBMGctnchH2n1i3+oGbibhurQj22+OO66Q6mijKoCRP/QJm5WTGcO4c0iVX0B8dIlai/uv/pGU8Wps9MxysH5ViVIj56LWKuNfy1FNO9+wx4iHYnUxfW0Iccq3YJYAlOBkMv8VZIJl9aTEEFJ626LJEw/UbEBdR9CdlwnylPf4RuNW/lcGYMgq3xNVpp/bzYKwZivvQOFgMdX1tv8D33SHvAm3zrRCU+v94BJXnBi9k25fnKsc705IMwv+I6bueXA+mDnagj1oTqsWXZ0WnPAYI3WUO7sBf7XUH230wfYXnTSVhujJxiEP/2oWqi2813gtQkKwz9N2p6yT8Vsh3+BaAwwDvtOZMJKUFcabTBSL6NoouzmzwItJ4mIboT6KXaef66rs8jCU=";

        private struct NameID
        {
            public string Name;
            public int ID;

            public override string ToString()
            {
                return Name;
            }
        }

        private PrintManager printManager = null;
        private PrintJob job;
        private Timer jobStatusTimer;
        private bool IsInit = false;

        public MainForm()
        {
            InitializeComponent();
            jobStatusTimer = new Timer();
            jobStatusTimer.Enabled = false;
            jobStatusTimer.Interval = 1000;
            jobStatusTimer.Tick += JobStatusTimer_Tick;
            textBoxFoxitSN.Text = sn;
            textBoxFoxitKey.Text = key;
        }

        private void buttonInit_Click(object sender, EventArgs e)
        {
            printManager = new PrintManager();
            int res = printManager.Init(textBoxFoxitSN.Text, textBoxFoxitKey.Text);
            IsInit = true;
            if (res == 1)
            {
                labelInitResult.ForeColor = Color.Green;
                labelInitResult.Text = "Print Manager initialized okay";
                UpdatePrinterList();

                buttonInit.Enabled = false;
                RunTest.Enabled = true;
                TestList.Enabled = true;
                textBoxFoxitSN.Enabled = false;
                textBoxFoxitKey.Enabled = false;

                // Set default Job settings
                numericUpDownCustomScale.Enabled = false;
                comboBoxScaling.SelectedIndex = 0;
                comboBoxRotation.SelectedIndex = 0;

                checkBoxDuplex.Checked = false;
                comboBoxDuplex.Enabled = false;
                comboBoxDuplex.SelectedIndex = 0;

                checkBoxQuality.Checked = false;
                comboBoxQuality.Enabled = false;
                comboBoxQuality.SelectedIndex = 2;

                checkBoxColorMode.Checked = false;
                comboBoxColorMode.Enabled = false;
                comboBoxColorMode.SelectedIndex = 1;

                checkBoxPriority.Checked = false;
                numericUpDownPriority.Enabled = false;
                numericUpDownPriority.Value = 99;

                checkBoxCollate.Checked = false;
                comboBoxCollate.Enabled = false;
                comboBoxCollate.SelectedIndex = 0;

                comboBoxPrintUnit.Enabled = true;
                comboBoxPrintUnit.SelectedIndex = 0;

                numericUpDownCustomScale.Enabled = false;
                numericUpDownCustomScale.Value = 100;
            }
            else
            {
                labelInitResult.ForeColor = Color.Red;
                labelInitResult.Text = "Error initializing Print Manager, please check sn and key";
            }
        }

        private void RunTest_Click(object sender, EventArgs e)
        {
            var result = new List<string>();
            if (printManager != null)
            {
                if (TestList.SelectedItem != null)
                {
                    var selectedItem = (string)TestList.SelectedItem;
                    if (selectedItem == "Default printer name")
                    {
                        RunDefaultPrinterName(result);
                    }
                    else if (selectedItem == "Printer list")
                    {
                        RunPrinterList(result);
                    }
                    else if (selectedItem == "Paper source list")
                    {
                        RunPaperSourceList(result);
                    }
                    else if (selectedItem == "Paper size list")
                    {
                        RunPaperList(result);
                    }
                    else if (selectedItem == "Easy print PDF")
                    {
                        RunEasyPrintPDF(result);
                    }
                }
            }
            TestResult.Lines = result.ToArray();
        }

        private void RunDefaultPrinterName(List<string> result)
        {
            result.Add(printManager.GetDefaultPrinterName());
        }

        private void RunPrinterList(List<string> result)
        {
            int printerCount = printManager.GetPrinterCount();
            if (printerCount > 0)
            {
                for (int printerIndex = 0; printerIndex < printerCount; printerIndex++)
                {
                    result.Add(printerIndex + " = " + printManager.GetPrinterName(printerIndex));
                }
            }
            else
            {
                result.Add("No printers");
            }
        }

        private void RunPaperSourceList(List<string> result)
        {
            if (PrinterList.SelectedIndex >= 0)
            {
                var printerName = (string)PrinterList.SelectedItem;
                int paperSourceCount = printManager.GetPaperSourceCount(printerName);
                if (paperSourceCount > 0)
                {
                    for (int paperSourceIndex = 0; paperSourceIndex < paperSourceCount; paperSourceIndex++)
                    {
                        int paperSourceID = printManager.GetPaperSourceID(printerName, paperSourceIndex);
                        string paperSourceName = printManager.GetPaperSourceName(printerName, paperSourceIndex);
                        result.Add(paperSourceIndex + " = " + paperSourceName + " [" + paperSourceID + "]");
                    }
                }
                else
                {
                    result.Add("No paper sources for this printer");
                }
            }
            else
            {
                result.Add("Please select a printer");
            }
        }

        private void RunPaperList(List<string> result)
        {
            if (PrinterList.SelectedIndex >= 0)
            {
                var printerName = (string)PrinterList.SelectedItem;
                int paperSizeCount = printManager.GetPaperSizeCount(printerName);
                if (paperSizeCount > 0)
                {
                    for (int paperSizeIndex = 0; paperSizeIndex < paperSizeCount; paperSizeIndex++)
                    {
                        int paperWidth = printManager.GetPaperSizeWidth(printerName, paperSizeIndex);
                        int paperHeight = printManager.GetPaperSizeHeight(printerName, paperSizeIndex);
                        int paperID = printManager.GetPaperSizeID(printerName, paperSizeIndex);
                        string paperName = printManager.GetPaperSizeName(printerName, paperSizeIndex);
                        result.Add(paperSizeIndex + " = " + paperName + " (" + paperWidth + "x" + paperHeight + ") [" + paperID + "]");
                    }
                }
                else
                {
                    result.Add("No paper types for this printer");
                }
            }
            else
            {
                result.Add("Please select a printer");
            }
        }

        private void RunEasyPrintPDF(List<string> result)
        {
            if (PrinterList.SelectedIndex >= 0)
            {
                var printerName = (string)PrinterList.SelectedItem;
                var documentPath = System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\testfiles\\poem.pdf";
                if (File.Exists(documentPath))
                {
                    if (MessageBox.Show("The file \"poem.pdf\" will be printed on \"" + printerName + "\". Are you sure?", "Confirm print", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        int callResult = printManager.PrintPDFFromFileToPrinter(documentPath, "", printerName);
                        result.Add("Print result: " + callResult);
                    }
                }
                else
                {
                    result.Add("PDF to print could not be found");
                }
            }
            else
            {
                result.Add("Please select a printer");
            }
        }

        private void UpdatePrinterList()
        {
            PrinterList.Items.Clear();
            comboBoxPrinter.Items.Clear();
            if (printManager != null)
            {
                int printerCount = printManager.GetPrinterCount();
                for (int printerIndex = 0; printerIndex < printerCount; printerIndex++)
                {
                    PrinterList.Items.Add(printManager.GetPrinterName(printerIndex));
                    comboBoxPrinter.Items.Add(printManager.GetPrinterName(printerIndex));
                }
            }
            if ((PrinterList.SelectedIndex < 0) && (PrinterList.Items.Count > 0))
            {
                PrinterList.SelectedIndex = PrinterList.Items.IndexOf(printManager.GetDefaultPrinterName());
            }
            if ((comboBoxPrinter.SelectedIndex < 0) && (comboBoxPrinter.Items.Count > 0))
            {
                comboBoxPrinter.SelectedIndex = comboBoxPrinter.Items.IndexOf(printManager.GetDefaultPrinterName());
            }
        }

        private void UpdateSourcePaperList()
        {
            string printerName = (string)comboBoxPrinter.SelectedItem;
            comboBoxPaperSize.Items.Clear();
            NameID autoPaper = new NameID();
            autoPaper.Name = "(Auto select)";
            autoPaper.ID = -1;
            comboBoxPaperSize.Items.Add(autoPaper);

            comboBoxPaperSource.Items.Clear();
            if (printManager != null)
            {
                int sourceCount = printManager.GetPaperSourceCount(printerName);
                for (int sourceIndex = 0; sourceIndex < sourceCount; sourceIndex++)
                {
                    NameID source = new NameID();
                    source.Name = printManager.GetPaperSourceName(printerName, sourceIndex);
                    source.ID = printManager.GetPaperSourceID(printerName, sourceIndex);
                    comboBoxPaperSource.Items.Add(source);
                }

                int paperSizeCount = printManager.GetPaperSizeCount(printerName);
                for (int paperSizeIndex = 0; paperSizeIndex < paperSizeCount; paperSizeIndex++)
                {
                    NameID paper = new NameID();
                    paper.Name = printManager.GetPaperSizeName(printerName, paperSizeIndex) + " (" +
                        printManager.GetPaperSizeWidth(printerName, paperSizeIndex) + "x" +
                        printManager.GetPaperSizeHeight(printerName, paperSizeIndex) + ")";
                    paper.ID = printManager.GetPaperSizeID(printerName, paperSizeIndex);
                    comboBoxPaperSize.Items.Add(paper);
                }
            }
            if ((comboBoxPaperSource.SelectedIndex < 0) && (comboBoxPaperSource.Items.Count > 0))
            {
                int defaultSourceID = printManager.GetDefaultPaperSource(printerName);
                int itemIndex = 0;
                while ((comboBoxPaperSource.SelectedIndex < 0) && (itemIndex < comboBoxPaperSource.Items.Count))
                {
                    Object obj = comboBoxPaperSource.Items[itemIndex];
                    if (obj is NameID)
                    {
                        if (((NameID)obj).ID == defaultSourceID)
                        {
                            comboBoxPaperSource.SelectedIndex = itemIndex;
                        }
                    }
                    itemIndex++;
                }
                if (comboBoxPaperSource.SelectedIndex < 0)
                {
                    comboBoxPaperSource.SelectedIndex = 0;
                }
            }
            if ((comboBoxPaperSize.SelectedIndex < 0) && (comboBoxPaperSize.Items.Count > 0))
            {
                int defaultPaperSizeID = printManager.GetDefaultPaperSize(printerName);
                int itemIndex = 0;
                while ((comboBoxPaperSize.SelectedIndex < 0) && (itemIndex < comboBoxPaperSize.Items.Count))
                {
                    Object obj = comboBoxPaperSize.Items[itemIndex];
                    if (obj is NameID)
                    {
                        if (((NameID)obj).ID == defaultPaperSizeID)
                        {
                            comboBoxPaperSize.SelectedIndex = itemIndex;
                        }
                    }
                    itemIndex++;
                }
                if (comboBoxPaperSize.SelectedIndex < 0)
                {
                    comboBoxPaperSize.SelectedIndex = 0;
                }
            }
        }

        private void TestList_SelectedIndexChanged(object sender, EventArgs e)
        {
            TestResult.Text = "";
            PrinterList.Visible = TestList.SelectedIndex > 1;
        }

        private void comboBoxPrinter_SelectedIndexChanged(object sender, EventArgs e)
        {
            // The selected printer was changed, so update the list of paper sources and paper sizes
            UpdateSourcePaperList();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            numericUpDownCustomScale.Enabled = comboBoxScaling.SelectedIndex == 3;
        }

        private void checkBoxDuplex_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxDuplex.Enabled = checkBoxDuplex.Checked;
        }

        private void checkBoxQuality_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxQuality.Enabled = checkBoxQuality.Checked;
        }

        private void checkBoxColorMode_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxColorMode.Enabled = checkBoxColorMode.Checked;
        }

        private void checkBoxPriority_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownPriority.Enabled = checkBoxPriority.Checked;
        }

        private void checkBoxCollate_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxCollate.Enabled = checkBoxCollate.Checked;
        }

        private void buttonRemoveFile_Click(object sender, EventArgs e)
        {
            if (listViewJobFiles.SelectedItems.Count > 0)
            {
                listViewJobFiles.Items.Remove(listViewJobFiles.SelectedItems[0]);
            }
            buttonBeginJob.Enabled = listViewJobFiles.Items.Count > 0;
        }

        private void buttonEditFile_Click(object sender, EventArgs e)
        {
            if (listViewJobFiles.SelectedItems.Count > 0)
            {
                ListViewItem fileItem = listViewJobFiles.SelectedItems[0];
                var fileItemForm = new FileItemForm();
                string password = "";
                if (fileItem.SubItems.Count > 1)
                {
                    password = fileItem.SubItems[1].Text;
                }
                string pageList = "";
                if (fileItem.SubItems.Count > 2)
                {
                    pageList = fileItem.SubItems[2].Text;
                    if (pageList == "All") pageList = "";
                }
                fileItemForm.SetValues(fileItem.Text, password, pageList);
                if (fileItemForm.ShowDialog() == DialogResult.OK)
                {
                    fileItem.Text = fileItemForm.GetFileName();
                    fileItem.SubItems[1].Text = fileItemForm.GetPassword();
                    pageList = fileItemForm.GetPageList();
                    if (pageList == "") pageList = "All";
                    fileItem.SubItems[2].Text = pageList;
                }
            }
        }

        private void buttonAddFile_Click(object sender, EventArgs e)
        {
            var fileItemForm = new FileItemForm();
            if (fileItemForm.ShowDialog() == DialogResult.OK)
            {
                ListViewItem item = new ListViewItem(fileItemForm.GetFileName());
                item.SubItems.Add(fileItemForm.GetPassword());
                string pageList = fileItemForm.GetPageList();
                if (pageList == "") pageList = "All";
                item.SubItems.Add(pageList);
                listViewJobFiles.Items.Add(item);
            }
            buttonBeginJob.Enabled = listViewJobFiles.Items.Count > 0;
        }

        private void listViewJobFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonEditFile.Enabled = listViewJobFiles.SelectedItems.Count > 0;
            buttonRemoveFile.Enabled = listViewJobFiles.SelectedItems.Count > 0;
        }

        private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
        {
            e.Cancel = buttonInit.Enabled;
        }

        private void buttonBeginJob_Click(object sender, EventArgs e)
        {
            if (printManager != null)
            {
                // Set the render mode
                if (checkBoxBitmap.Checked)
                {
                    printManager.SetRenderMode(1);
                }
                else
                {
                    printManager.SetRenderMode(0);
                }

                // Create the print job
                job = printManager.NewJob();

                // Add the files to print, with their passwords and the page list, if set
                foreach (ListViewItem fileItem in listViewJobFiles.Items)
                {
                    string password = "";
                    if (fileItem.SubItems.Count > 1)
                    {
                        password = fileItem.SubItems[1].Text;
                    }
                    string pageList = "";
                    if (fileItem.SubItems.Count > 2)
                    {
                        pageList = fileItem.SubItems[2].Text;
                        if (pageList == "All") pageList = "";
                    }
                    job.AddPDFFromFile(fileItem.Text, password);
                    if (pageList != "")
                    {
                        job.SetPageList(fileItem.Index, pageList);
                    }
                }

                // Set the printer name
                string printerName;
                if (comboBoxPrinter.SelectedIndex >= 0)
                {
                    printerName = comboBoxPrinter.SelectedItem.ToString();
                    job.SetPrinter(printerName);
                }

                // Set the number of copies
                job.SetCopies((int)numericUpDownCopies.Value);

                // Set the default source (paper tray or bin)
                if (comboBoxPaperSource.SelectedIndex >= 0)
                {
                    Object obj = comboBoxPaperSource.SelectedItem;
                    if (obj is NameID)
                    {
                        int paperSourceID = ((NameID)obj).ID;
                        job.SetPaperSource(paperSourceID);
                    }
                }

                // Set the paper size
                if (comboBoxPaperSize.SelectedIndex >= 0)
                {
                    Object obj = comboBoxPaperSize.SelectedItem;
                    if (obj is NameID)
                    {
                        int paperSizeID = ((NameID)obj).ID;
                        job.SetPaperSize(paperSizeID);
                    }
                }

                // Set the document name, if selected
                if (textBoxDocumentName.Text != "")
                {
                    job.SetDocumentName(textBoxDocumentName.Text);
                }

                // Set the duplex mode (printing on both sides of the paper), if selected
                if (checkBoxDuplex.Checked)
                {
                    int duplexMode = comboBoxDuplex.SelectedIndex + 1;
                    if ((duplexMode >= 1) && (duplexMode <= 3))
                    {
                        job.SetDuplex(duplexMode);
                    }
                }

                // Set the print quality, if selected
                if (checkBoxQuality.Checked)
                {
                    int quality = -comboBoxQuality.SelectedIndex - 1;
                    if ((quality >= -4) && (quality <= -1))
                    {
                        job.SetQuality(quality);
                    }
                }

                // Set the color mode, if selected
                if (checkBoxColorMode.Checked)
                {
                    int colorMode = comboBoxColorMode.SelectedIndex + 1;
                    if ((colorMode >= 1) && (colorMode <= 3))
                    {
                        job.SetColor(colorMode);
                    }
                }

                // Set the job priority, if selected
                if (checkBoxPriority.Checked)
                {
                    int priority = (int)numericUpDownPriority.Value;
                    if ((priority >= 1) && (priority <= 99))
                    {
                        job.SetPriority(priority);
                    }
                }

                // Set the page collation (multiple copies of document are separated), if selected
                if (checkBoxCollate.Checked)
                {
                    int collate = comboBoxCollate.SelectedIndex;
                    if ((collate >= 0) && (collate <= 1))
                    {
                        job.SetCollate(collate);
                    }
                }

                // Set the rotation mode
                int rotateMode = comboBoxRotation.SelectedIndex;
                if ((rotateMode < 0) || (rotateMode > 2)) rotateMode = 0;
                job.SetRotation(rotateMode);

                // Set the scaling mode
                int scalingMode = comboBoxScaling.SelectedIndex;
                if ((scalingMode < 0) || (scalingMode > 3)) scalingMode = 0;
                job.SetScaling(scalingMode);
                if (scalingMode == 3)
                {
                    job.SetCustomScale((double)numericUpDownCustomScale.Value);
                }

                bool isAutoCenter = checkBoxAutoCenter.Checked;
                job.SetAutoCenter(isAutoCenter);

                job.SetPrintUnitType(comboBoxPrintUnit.SelectedIndex);
                job.SetLeftMargin((double)numericMarginLeft.Value);
                job.SetRightMargin((double)numericMarginRight.Value);
                job.SetTopMargin((double)numericMarginTop.Value);
                job.SetBottomMargin((double)numericMarginBottom.Value);

                // Start printing
                job.Begin();
                buttonBeginJob.Enabled = false;
                buttonCancelJob.Enabled = true;
                buttonCloseJob.Enabled = true;
                jobStatusTimer.Enabled = true;
            }
        }

        private void JobStatusTimer_Tick(object sender, EventArgs e)
        {
            if ((printManager != null) && (job != null))
            {
                int jobStatus = job.GetStatus();
                if (jobStatus == 1000)
                {
                    labelStatus.Text = "Status: Job has started";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1001)
                {
                    labelStatus.Text = "Status: Job was cancelled";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1002)
                {
                    labelStatus.Text = "Status: Job has completed";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1003)
                {
                    labelStatus.Text = "Status: Job is paused";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1004)
                {
                    labelStatus.Text = "Status: Printing error";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1005)
                {
                    labelStatus.Text = "Status: Unknown job status";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1006)
                {
                    labelStatus.Text = "Status: Failed to query job status";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1007)
                {
                    labelStatus.Text = "Status: Printer Paper out";
                    progressBarPrintPercent.Value = 0;
                }
                else if (jobStatus == 1008)
                {
                    labelStatus.Text = "Status: Failed to query printer status";
                    progressBarPrintPercent.Value = 0;
                }
                else if ((jobStatus >= 2000) && (jobStatus <= 2100))
                {
                    labelStatus.Text = "Status: Printing in progress";
                    progressBarPrintPercent.Value = jobStatus - 2000;
                }
                else
                {
                    labelStatus.Text = "Status: Unknown";
                }
            }
        }

        private void buttonCancelJob_Click(object sender, EventArgs e)
        {
            if ((printManager != null) && (job != null))
            {
                job.Cancel();;
                job = null;
                buttonBeginJob.Enabled = true;
                buttonCancelJob.Enabled = false;
                buttonCloseJob.Enabled = false;
                progressBarPrintPercent.Value = 0;
                labelStatus.Text = "Status:"; 
            }
        }

        private void buttonCloseJob_Click(object sender, EventArgs e)
        {
            if ((printManager != null) && (job != null))
            {
                job.Close();;
                job = null;
                buttonBeginJob.Enabled = true;
                buttonCancelJob.Enabled = false;
                buttonCloseJob.Enabled = false;
                progressBarPrintPercent.Value = 0;
                labelStatus.Text = "Status:";
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
          if (printManager != null && IsInit)
            printManager.Release();
        }
  }
}
