private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            if (cbPrinter.SelectedItem == null)
            {
                MessageBox.Show("Please select a printer.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // load document
            while (true)
            {
                try
                {
                    if (fpFile.SelectedFile == null)
                    {
                        using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(@"PdfDocumentSourceSamples.Resources.DefaultDocument.pdf"))
                            pdfDocumentSource.LoadFromStream(stream);
                    }
                    else
                    {
                        pdfDocumentSource.LoadFromFile(fpFile.SelectedFile.FullName);
                    }
                    break;
                }
                catch (PdfPasswordException)
                {
                    var password = PasswordWindow.DoEnterPassword(fpFile.SelectedFile.FullName);
                    if (password == null)
                    {
                        return;
                    }
                    pdfDocumentSource.Credential.Password = password;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            //
            OutputRange outputRange;

            if (rbtnPagesAll.IsChecked.Value)
            {
                outputRange = OutputRange.All;
            }
            else
            {
                if (!OutputRange.TryParse(tbPagesRange.Text, out outputRange))
                {
                    MessageBox.Show("Invalid range of pages.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            try
            {
                C1PrintOptions po = new C1PrintOptions();
                po.PrintQueue  = (PrintQueue)((C1ComboBoxItem)cbPrinter.SelectedItem).Tag;
                po.OutputRange = outputRange;
                pdfDocumentSource.Print(po);
                MessageBox.Show("Document was successfully printed.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #2
0
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            // load document
            while (true)
            {
                try
                {
                    if (fpFile.SelectedFile == null)
                    {
                        using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(@"PdfDocumentSourceSamples.Resources.DefaultDocument.pdf"))
                            pdfDocumentSource.LoadFromStream(stream);
                    }
                    else
                    {
                        pdfDocumentSource.LoadFromFile(fpFile.SelectedFile.FullName);
                    }
                    break;
                }
                catch (PdfPasswordException)
                {
                    var password = PasswordWindow.DoEnterPassword(fpFile.SelectedFile.FullName);
                    if (password == null)
                    {
                        return;
                    }
                    pdfDocumentSource.Credential.Password = password;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            //
            OutputRange outputRange;

            if (rbtnPagesAll.IsChecked.Value)
            {
                outputRange = OutputRange.All;
            }
            else
            {
                if (!OutputRange.TryParse(tbPagesRange.Text, out outputRange))
                {
                    MessageBox.Show("Invalid range of pages.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            // execute action
            ExportProvider ep = (ExportProvider)((C1ComboBoxItem)cbExportFilter.SelectedItem).Tag;

            saveFileDialog.DefaultExt = "." + ep.DefaultExtension;
            saveFileDialog.FileName   = (fpFile.SelectedFile == null ? "DefaultDocument" : System.IO.Path.GetFileName(fpFile.SelectedFile.FullName)) + "." + ep.DefaultExtension;
            saveFileDialog.Filter     = String.Format("{0} (*.{1})|*.{1}|All files (*.*)|*.*", ep.FormatName, ep.DefaultExtension);
            bool?dr = saveFileDialog.ShowDialog();

            if (!dr.HasValue || !dr.Value)
            {
                return;
            }

            try
            {
                var exporter = ep.NewExporter();
                exporter.ShowOptions = false;
                exporter.Preview     = true;
                exporter.FileName    = saveFileDialog.FileName;
                exporter.Range       = outputRange;
                pdfDocumentSource.Export(exporter);
                MessageBox.Show("Document was successfully exported.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public static string DoEnterPassword(string fileName)
        {
            PasswordWindow f = new PasswordWindow();

            return(f.EnterPassword(fileName));
        }