using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

using System.Drawing.Printing;

// the PDF Print namespace
using EvoPdf.PdfPrint;

namespace PdfPrintDemo
{
    public partial class PdfPrintForm : Form
    {
        private void btnPrintPdf_Click(object sender, EventArgs e)
        {
            if (pdfFileTextBox.Text.Trim().Equals(String.Empty))
            {
                MessageBox.Show("Please choose a source PDF file", "Choose PDF file", MessageBoxButtons.OK);
                return;
            }

            // the source pdf file
            string pdfFileName = pdfFileTextBox.Text.Trim();

            // start page number
            int startPageNumber = int.Parse(textBoxStartPage.Text.Trim());
            // end page number
            // when it is 0 the conversion will continue up to the end of document
            int endPageNumber = 0;
            if (textBoxEndPage.Text.Trim() != String.Empty)
                endPageNumber = int.Parse(textBoxEndPage.Text.Trim());

            // create the PDF printer 
            PdfPrint pdfPrint = new PdfPrint();

            // set the license key
            pdfPrint.LicenseKey = "oy08LDo/LDwsOiI8LD89Ij0+IjU1NTUsPA==";

            // set the document name
            pdfPrint.DocumentName = "PDF Silent Printing";

            // enable or disable color printing
            pdfPrint.DefaultPageSettings.Color = cbPrintColor.Checked;

            // set the PDF printing color and resolution
            pdfPrint.Color = GetSelectedPrintColor();
            pdfPrint.Resolution = int.Parse(textBoxResolution.Text);

            // select the printer
            string selectedPrinterName = GetSelectedPrinterName();
            if (selectedPrinterName != null)
                pdfPrint.PrinterSettings.PrinterName = selectedPrinterName;

            // set paper size
            PaperSize selectedPaperSize = GetSelectedPaperSize();
            if (selectedPaperSize != null)
                pdfPrint.DefaultPageSettings.PaperSize = selectedPaperSize;

            // set paper orientation
            pdfPrint.DefaultPageSettings.Landscape = GetSelectedPageOrientation() == "Landscape";

            // set paper margins 
            pdfPrint.DefaultPageSettings.Margins = new Margins((int)(float.Parse(leftMarginTextBox.Text) * 100),
                (int)(float.Parse(rightMarginTextBox.Text) * 100),
                (int)(float.Parse(topMarginTextBox.Text) * 100),
                (int)(float.Parse(bottomMarginTextBox.Text) * 100));

            // enable or disable the usage of printer hard margins 
            // when calculating the space available for printing in paper
            pdfPrint.UseHardMargins = cbUseHardMargins.Checked;

            // enable or disable the status dialog during printing
            pdfPrint.ShowStatusDialog = cbShowStatusDialog.Checked;

            // the demo output directory
            string outputDirectory = Path.Combine(Application.StartupPath, @"DemoFiles\Output");

            Cursor = Cursors.WaitCursor;

            try
            {
                pdfPrint.Print(pdfFileName, startPageNumber, endPageNumber);
            }
            catch (Exception ex)
            {
                // The conversion failed
                MessageBox.Show(String.Format("An error occurred. {0}", ex.Message), "Error");
                return;
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }

            MessageBox.Show("Print Completed", "Print Completed", MessageBoxButtons.OK);
        }

        private string GetSelectedPrinterName()
        {
            if (printerNameComboBox.SelectedItem != null)
                return (string)printerNameComboBox.SelectedItem;

            return null;
        }

        private PaperSize GetSelectedPaperSize()
        {
            if (printerNameComboBox.SelectedItem == null)
                return null;

            string selectedPrinterName = (string)printerNameComboBox.SelectedItem;

            PrinterSettings printerSettings = new PrinterSettings();
            printerSettings.PrinterName = selectedPrinterName;

            if (paperSizeComboBox.SelectedItem != null)
            {
                string paperSizeName = (string)paperSizeComboBox.SelectedItem;

                for (int i = 0; i < printerSettings.PaperSizes.Count; i++)
                    if (printerSettings.PaperSizes[i].PaperName == paperSizeName)
                    {
                        return printerSettings.PaperSizes[i];
                    }
            }

            return null;
        }

        public string GetSelectedPageOrientation()
        {
            return (string)paperOrientationComboBox.SelectedItem;
        }

        private PrintColor GetSelectedPrintColor()
        {
            switch (colorSpaceComboBox.SelectedItem.ToString())
            {
                case "Color":
                    return PrintColor.Color;
                case "Gray":
                    return PrintColor.Gray;
                default:
                    return PrintColor.Color;
            }
        }

        private void SelectPrinter(string printerName)
        {
            PrinterSettings printerSettings = new PrinterSettings();
            printerSettings.PrinterName = printerName;

            paperSizeComboBox.Items.Clear();
            for (int i = 0; i < printerSettings.PaperSizes.Count; i++)
                paperSizeComboBox.Items.Add(printerSettings.PaperSizes[i].PaperName);

            if (printerSettings.PaperSizes.Count > 0)
                paperSizeComboBox.SelectedIndex = 0;
        }

        private void printerNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectPrinter((string)printerNameComboBox.SelectedItem);
        }        

        private void btnChoosePDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPdfDialog = new OpenFileDialog();
            openPdfDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            openPdfDialog.Multiselect = false;

            if (openPdfDialog.ShowDialog() == DialogResult.OK)
            {
                pdfFileTextBox.Text = openPdfDialog.FileName;
            }

            openPdfDialog.Dispose();
        }

        private void btnViewPdf_Click(object sender, EventArgs e)
        {
            if (pdfFileTextBox.Text.Trim() == String.Empty)
                return;

            string pdfFileName = pdfFileTextBox.Text.Trim();
            try
            {
                System.Diagnostics.Process.Start(pdfFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Cannot open PDF file '{0}'. {1}", pdfFileName, ex.Message));
            }
        }

        public PdfPrintForm()
        {
            InitializeComponent();
        }

        private void PdfPrintForm_Load(object sender, EventArgs e)
        {
            pdfFileTextBox.Text = Path.Combine(Application.StartupPath, @"DemoFiles\Input\Demo.pdf");

            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                printerNameComboBox.Items.Add(PrinterSettings.InstalledPrinters[i]);

            if (PrinterSettings.InstalledPrinters.Count > 0)
            {
                PrinterSettings defaultPrinterSettings = new PrinterSettings();

                string defaultPrinterName = defaultPrinterSettings.PrinterName;
                printerNameComboBox.SelectedItem = defaultPrinterName;
            }

            paperOrientationComboBox.SelectedItem = "Portrait";

            colorSpaceComboBox.SelectedItem = "Color";
        }
    }
}
