Exemple #1
0
        public Image SelectImage(string pdfFileName)
        {
            PdfImageViewer viewer;

            try
            {
                viewer = new PdfImageViewer(pdfFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            try
            {
                pages.Items.Clear();
                for (int i = 0; i < viewer.PageCount; i++)
                {
                    int k = viewer.GetImageNames(i).Length;
                    if (k > 0)
                    {
                        pages.Items.Add(i + 1);
                    }
                }
                if (pages.Items.Count == 0)
                {
                    MessageBox.Show("Images in PDF file are not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }
                pages.SelectedIndex = 0;

                if (pages.Items.Count == 1)
                {
                    return(GetImageFromPage(viewer, 0));
                }

                label1.Text = string.Format(_labelText, pages.Items.Count);

                _selected = false;
                ShowDialog();
                if (_selected)
                {
                    Image result = GetImageFromPage(viewer, (int)pages.SelectedItem - 1);
                    return(result);
                }

                return(null);
            }
            finally
            {
                viewer.Dispose();
            }
        }
        /// <summary>
        /// Select PDF page and returns page images as single image.
        /// </summary>
        public static BitmapSource SelectPdfPageImage(string filename, ref int pageIndex)
        {
            // create PdfImageViewer
            PdfImageViewer viewer;

            try
            {
                viewer = new PdfImageViewer(filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(null);
            }

            try
            {
                // collect pages with images
                List <int> pageWithImages = new List <int>();
                for (int i = 0; i < viewer.PageCount; i++)
                {
                    string[] imageNames = viewer.GetImageNames(i);
                    if (imageNames.Length > 0)
                    {
                        pageWithImages.Add(i);
                    }
                }

                if (pageWithImages.Count == 0)
                {
                    MessageBox.Show("Images in PDF file are not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return(null);
                }

                if (pageWithImages.Count > 1)
                {
                    // select page
                    SelectPdfPageWindow selectPdfPageWindow = new SelectPdfPageWindow(pageWithImages.ToArray());
                    selectPdfPageWindow.ShowDialog();
                    pageIndex = pageWithImages[selectPdfPageWindow.pagesComboBox.SelectedIndex];
                }
                else
                {
                    pageIndex = pageWithImages[0];
                }

                return(GetPdfPageImage(viewer, pageIndex));
            }
            finally
            {
                viewer.Dispose();
            }
        }