private void PdfPrintLibraryTestForm_Load(object sender, EventArgs e)
 {
     PdfPrintTest.SetConfigFromIni(_configFile);
     _timer.Interval     = 100;
     _timer.Tick        += _timer_Tick;
     _previewPaperHeight = _configFile.PreviewPageHeight;
     _previewPaperWidth  = _configFile.PreviewPageWidth;
     _watermarkForm      = new WatermarkForm(_watermark, _previewPaperWidth, _previewPaperHeight, _previewImagePath);
     Text += (PdfPrintTest.Is64 ? " 64 bit" : " 32 bit") + " ver. " + Application.ProductVersion;
     if (PdfPrintTest.Is64)
     {
         // on 64 bit PrinterSettingsDialog isn't available so we hide it...
         btnSetPrinterSettings.Visible      = false;
         btnSetPrinterSettingsAdobe.Visible = false;
         cbUsePrinterSettings.Visible       = false;
         cbUsePrinterSettingsAdobe.Visible  = false;
     }
     cmbxTiffCompression.SelectedIndex = 0;
     EnableOrDisableFieldsBasedOnDefaultCheck(false);
     EnableOrDisableFieldsBasedOnDefaultCheck(true);
     FillPrinters();
     if (cmbxPrinterNames.Items.Count > 0)
     {
         LoadPrinterDefaults((new PrinterSettings()).PrinterName);
     }
 }
        private void FillPrinterDefaults(PrinterInfo printerInfo, bool isAdobe)
        {
            if (isAdobe)
            {
                cbCollateAdobe.Checked          = printerInfo.Collate;
                numCopiesAdobe.Value            = printerInfo.Copies;
                rbDuplexSimplexAdobe.Checked    = printerInfo.Duplex == Duplex.Simplex;
                rbDuplexHorizontalAdobe.Checked = printerInfo.Duplex == Duplex.Horizontal;
                rbDuplexVerticalAdobe.Checked   = printerInfo.Duplex == Duplex.Vertical;
                rbLandscapeAdobe.Checked        = printerInfo.Landscape;
                rbPortraitAdobe.Checked         = !printerInfo.Landscape;
                cbPrintInColorAdobe.Checked     = printerInfo.Color;
            }
            else
            {
                cbCollate.Checked          = printerInfo.Collate;
                numCopies.Value            = printerInfo.Copies;
                rbDuplexSimplex.Checked    = printerInfo.Duplex == Duplex.Simplex;
                rbDuplexHorizontal.Checked = printerInfo.Duplex == Duplex.Horizontal;
                rbDuplexVertical.Checked   = printerInfo.Duplex == Duplex.Vertical;
                rbLandscape.Checked        = printerInfo.Landscape;
                rbPortrait.Checked         = !printerInfo.Landscape;
                cbPrintInColor.Checked     = printerInfo.Color;
            }

            PdfPrintTest.FillComboBox(isAdobe ? cmbxPrinterResolutionsAdobe : cmbxPrinterResolutions, printerInfo.PrinterResolutions, printerInfo.PrinterResolution);
            PdfPrintTest.FillComboBox(isAdobe ? cmbxPaperSizeAdobe : cmbxPaperSize, printerInfo.PaperSizes, printerInfo.PaperSize);
            PdfPrintTest.FillComboBox(isAdobe ? cmbxPaperSourceAdobe : cmbxPaperSource, printerInfo.PaperSources, printerInfo.PaperSource);
        }
        private void FillPrinters()
        {
            var    installedPrinters = PrinterSettings.InstalledPrinters;
            var    printerSettings   = new PrinterSettings();
            string defaultPrinter    = printerSettings.PrinterName;

            cmbxPrinterNames.SelectedIndexChanged -= cmbxPrinterNames_SelectedIndexChanged;
            PdfPrintTest.FillComboBox(cmbxPrinterNames, installedPrinters, defaultPrinter);

            cmbxPrinterNamesAdobe.SelectedIndexChanged -= cmbxPrinterNamesAdobe_SelectedIndexChanged;
            PdfPrintTest.FillComboBox(cmbxPrinterNamesAdobe, installedPrinters, defaultPrinter);

            cmbxPrinterNames.SelectedIndexChanged      += cmbxPrinterNames_SelectedIndexChanged;
            cmbxPrinterNamesAdobe.SelectedIndexChanged += cmbxPrinterNamesAdobe_SelectedIndexChanged;
        }
        private void btnPrintWithAdobe_Click(object sender, EventArgs e)
        {
            try
            {
                if (!PdfPrintTest.IsFileNameCorrect(tbFileName.Text))
                {
                    return;
                }
                var printLibrary = PdfPrintTest.GetPdfPrint(_configFile);
                if (!SetSettingsFromGUIAdobe(printLibrary))
                {
                    return;
                }

                var             resultMessage = "";
                PdfPrint.Status result        = PdfPrint.Status.OK;
                if (cbUsePrinterSettingsAdobe.Checked)
                {
                    if (_printerSettingsAdobe == null)
                    {
                        MessageBox.Show("PrinterSettings not set. Press Set Printer Settings button");
                        return;
                    }
                    result = printLibrary.PrintWithAdobe(tbFileName.Text, _printerSettingsAdobe, ref resultMessage);
                }
                else
                {
                    result = printLibrary.PrintWithAdobe(tbFileName.Text, ref resultMessage);
                }

                MessageBox.Show("Result message=" + resultMessage);
                if (result == PdfPrint.Status.OK)
                {
                    MessageBox.Show("Done successfully.");
                }
                else
                {
                    MessageBox.Show("Error. " + PdfPrintTest.DecodeStatusCode(result));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception " + ex.Message);
            }
        }
 private void SetPrinterInfo(object printerName) // We want to read just once to avoid double access of printerSettings which can be slow in case of non-local printer
 {
     _printerInfo       = PdfPrintTest.GetPrinterInfo(printerName);
     _printerInfoLoaded = true;
 }
        private void btnSavePdfAsImage_Click(object sender, EventArgs e)
        {
            var printLibrary = PdfPrintTest.GetPdfPrint(_configFile);
            var colorType    = PdfPrint.ColorType.Original;

            if (rbAsItIsInPDF.Checked)
            {
                colorType = PdfPrint.ColorType.Original;
            }
            if (rbBlackAndWhite.Checked)
            {
                colorType = PdfPrint.ColorType.BlackAndWhite;
            }
            if (rbGray.Checked)
            {
                colorType = PdfPrint.ColorType.GrayScale;
            }

            try
            {
                PdfPrint.Status result;
                if (cbxMultiTiff.Checked)
                {
                    var tiffCompression = GetTiffCompression();
                    if (cbIsStreamImage.Checked)
                    {
                        result = printLibrary.SavePdfPagesAsMultiPageTiff(PdfPrintTest.GetStream(tbFileName.Text), tbImageFileName.Text, (int)numFrom.Value,
                                                                          (int)numToPage.Value, (int)numZoom.Value,
                                                                          (float)numXResolution.Value, (float)numYResolution.Value, colorType, tbPassword.Text, tiffCompression);
                    }
                    else
                    {
                        result = printLibrary.SavePdfPagesAsMultiPageTiff(tbFileName.Text, tbImageFileName.Text, (int)numFrom.Value,
                                                                          (int)numToPage.Value, (int)numZoom.Value,
                                                                          (float)numXResolution.Value, (float)numYResolution.Value, colorType, tbPassword.Text, tiffCompression);
                    }
                }
                else
                {
                    if (cbIsStreamImage.Checked)
                    {
                        result = printLibrary.SavePdfPagesAsImages(PdfPrintTest.GetStream(tbFileName.Text), tbImageFileName.Text, (int)numFrom.Value,
                                                                   (int)numToPage.Value, (int)numZoom.Value,
                                                                   (float)numXResolution.Value, (float)numYResolution.Value, colorType,
                                                                   (long)numQuality.Value, tbPassword.Text);
                    }
                    else
                    {
                        result = printLibrary.SavePdfPagesAsImages(tbFileName.Text, tbImageFileName.Text, (int)numFrom.Value,
                                                                   (int)numToPage.Value, (int)numZoom.Value,
                                                                   (float)numXResolution.Value, (float)numYResolution.Value, colorType,
                                                                   (long)numQuality.Value, tbPassword.Text);
                    }
                }

                if (result == PdfPrint.Status.OK)
                {
                    MessageBox.Show("Done successfully.");
                }
                else
                {
                    MessageBox.Show("Error. " + PdfPrintTest.DecodeStatusCode(result));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception " + ex.Message);
            }
        }
        private bool SetSettingsFromGUI(PdfPrint printLibrary)
        {
            if (!cbUsePrinterSettings.Checked)
            {
                if (cmbxPrinterNames.SelectedItem == null)
                {
                    MessageBox.Show("Select printer.");
                    return(false);
                }

                if (!cbDefaultPrintInColor.Checked)
                {
                    printLibrary.PrintInColor = cbPrintInColor.Checked;
                }

                if (!cbDefaultCollate.Checked)
                {
                    printLibrary.Collate = cbCollate.Checked;
                }

                if (!cbDefaultPrintOrientation.Checked)
                {
                    if (rbLandscape.Checked)
                    {
                        printLibrary.IsLandscape = true;
                    }
                    if (rbPortrait.Checked)
                    {
                        printLibrary.IsLandscape = false;
                    }
                }

                if (!cbDefaultDuplex.Checked)
                {
                    if (rbDuplexHorizontal.Checked)
                    {
                        printLibrary.DuplexType = Duplex.Horizontal;
                    }
                    if (rbDuplexVertical.Checked)
                    {
                        printLibrary.DuplexType = Duplex.Vertical;
                    }
                    if (rbDuplexSimplex.Checked)
                    {
                        printLibrary.DuplexType = Duplex.Simplex;
                    }
                }

                if (!cbDefaultNumCopies.Checked)
                {
                    printLibrary.Copies = (short)numCopies.Value;
                }

                var printerSettings = new PrinterSettings();
                var defaultPrinter  = printerSettings.PrinterName;
                PdfPrintTest.SetDefaultPrinter(cmbxPrinterNames.SelectedItem.ToString());
                var newPrinterSettings = new PrinterSettings();

                if (!cbDefaultPaperSize.Checked)
                {
                    if (cmbxPaperSize.SelectedItem == null)
                    {
                        MessageBox.Show("Select paper size");
                        return(false);
                    }

                    foreach (PaperSize size in newPrinterSettings.PaperSizes)
                    {
                        if (size.ToString().Equals(cmbxPaperSize.SelectedItem.ToString()))
                        {
                            printLibrary.PaperSize = size;
                            break;
                        }
                    }
                }

                if (!cbDefaultPaperSource.Checked)
                {
                    if (cmbxPaperSource.SelectedItem == null)
                    {
                        MessageBox.Show("Select paper source");
                        return(false);
                    }

                    foreach (PaperSource paperSource in newPrinterSettings.PaperSources)
                    {
                        if (paperSource.ToString().Equals(cmbxPaperSource.SelectedItem.ToString()))
                        {
                            printLibrary.PaperSource = paperSource;
                            break;
                        }
                    }
                }

                if (!cbDefaultPrintInColor.Checked)
                {
                    printLibrary.PrintInColor = cbPrintInColor.Checked;
                }

                if (!cbDefaultPrinterResolution.Checked)
                {
                    if (cmbxPrinterResolutions.SelectedItem == null)
                    {
                        MessageBox.Show("Select printer resolution");
                        return(false);
                    }

                    foreach (PrinterResolution printerResolution in newPrinterSettings.PrinterResolutions)
                    {
                        if (printerResolution.ToString().Equals(cmbxPrinterResolutions.SelectedItem.ToString()))
                        {
                            printLibrary.PrinterResolution = printerResolution;
                            break;
                        }
                    }
                }

                printLibrary.PrinterName = cmbxPrinterNames.SelectedItem.ToString();
                PdfPrintTest.SetDefaultPrinter(defaultPrinter);
            }

            printLibrary.IsContentCentered = cbCentered.Checked;
            printLibrary.IsAutoRotate      = cbAutoRotate.Checked;

            if (rbScaleActualSize.Checked)
            {
                printLibrary.Scale = PdfPrint.ScaleTypes.None;
            }
            if (rbScaleFit.Checked)
            {
                printLibrary.Scale = PdfPrint.ScaleTypes.FitToMargins;
            }
            if (rbScaleShrink.Checked)
            {
                printLibrary.Scale = PdfPrint.ScaleTypes.Shrink;
            }

            printLibrary.Pages = tbPrintPages.Text;

            if (rbPrintEvenOdd.Checked)
            {
                printLibrary.RangeType = PdfPrint.RangeTypes.EvenAndOdd;
            }
            if (rbPrintEven.Checked)
            {
                printLibrary.RangeType = PdfPrint.RangeTypes.JustEven;
            }
            if (rbPrintOdd.Checked)
            {
                printLibrary.RangeType = PdfPrint.RangeTypes.JustOdd;
            }
            return(true);
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!PdfPrintTest.IsFileNameCorrect(tbFileName.Text))
            {
                return;
            }

            var printLibrary = PdfPrintTest.GetPdfPrint(_configFile);

            if (!SetSettingsFromGUI(printLibrary))
            {
                return;
            }

            PdfPrint.Status result = PdfPrint.Status.OK;

            if (cbUsePrintDialog.Checked)
            {
                printLibrary.SettingDialog = true;
            }

            if (!cbIsStream.Checked)
            {
                string jobName = string.IsNullOrEmpty(tbDocumentName.Text.Trim()) ? tbFileName.Text : tbDocumentName.Text.Trim();
                if (cbUsePrinterSettings.Checked)
                {
                    if (cbUseWatermark.Checked)
                    {
                        result = printLibrary.Print(tbFileName.Text, _printerSettings, tbPassword.Text, _watermark, jobName);
                    }
                    else
                    {
                        result = printLibrary.Print(tbFileName.Text, _printerSettings, tbPassword.Text, jobName);
                    }
                }
                else
                {
                    if (cbUseWatermark.Checked)
                    {
                        result = printLibrary.Print(tbFileName.Text, tbPassword.Text, _watermark, jobName);
                    }
                    else
                    {
                        result = printLibrary.Print(tbFileName.Text, tbPassword.Text, jobName);
                    }
                }
            }
            else
            {
                if (cbUsePrinterSettings.Checked)
                {
                    if (cbUseWatermark.Checked)
                    {
                        result = printLibrary.Print(PdfPrintTest.GetStream(tbFileName.Text), _printerSettings, tbPassword.Text, tbDocumentName.Text, _watermark);
                    }
                    else
                    {
                        result = printLibrary.Print(PdfPrintTest.GetStream(tbFileName.Text), _printerSettings, tbPassword.Text, tbDocumentName.Text);
                    }
                }
                else
                {
                    if (cbUseWatermark.Checked)
                    {
                        result = printLibrary.Print(PdfPrintTest.GetStream(tbFileName.Text), tbPassword.Text, tbDocumentName.Text, _watermark);
                    }
                    else
                    {
                        result = printLibrary.Print(PdfPrintTest.GetStream(tbFileName.Text), tbPassword.Text, tbDocumentName.Text);
                    }
                }
            }

            if (result == PdfPrint.Status.OK)
            {
                MessageBox.Show("Done successfully.");
            }
            else
            {
                MessageBox.Show("Error. " + PdfPrintTest.DecodeStatusCode(result));
            }
        }