示例#1
0
        private Image GetImageFromPage(PdfImageViewer viewer, int pageIndex)
        {
            string[]  names  = viewer.GetImageNames(pageIndex);
            ArrayList images = new ArrayList();

            for (int i = 0; i < names.Length; i++)
            {
                try
                {
                    images.Add(viewer.GetImage(pageIndex, names[i]));
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("Image '{0}': {1}", names[i], e.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (images.Count == 0)
            {
                return(null);
            }
            if (images.Count == 1)
            {
                return((Image)images[0]);
            }

            // merge images
            int padding = 5;
            int heigth  = 0;
            int width   = 0;
            int n       = images.Count;

            for (int i = 0; i < n; i++)
            {
                Image current = (Image)images[i];
                if (width < current.Width)
                {
                    width = current.Width;
                }
                heigth += current.Height;
            }
            width  += 3;
            heigth += (n + 1) * padding;
            Bitmap   result = new Bitmap(width, heigth, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g      = Graphics.FromImage(result);

            g.FillRectangle(Brushes.White, new Rectangle(0, 0, width, heigth));
            int dy = 1;

            for (int i = 0; i < n; i++)
            {
                Image current = (Image)images[i];
                g.DrawImageUnscaled(current, new Point(1, dy));
                dy += current.Height + padding;
                current.Dispose();
            }
            g.Dispose();

            return(result);
        }
示例#2
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();
            }
        }
        /// <summary>
        /// Returns a page images as single image.
        /// </summary>
        private static BitmapSource GetPdfPageImage(PdfImageViewer viewer, int pageIndex)
        {
            string[] imageNames = viewer.GetImageNames(pageIndex);

            // collect page images
            List <BitmapSource> images = new List <BitmapSource>();

            for (int i = 0; i < imageNames.Length; i++)
            {
                try
                {
                    images.Add(viewer.GetImage(pageIndex, imageNames[i]));
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("Image '{0}': {1}", imageNames[i], e.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            if (images.Count == 0)
            {
                return(null);
            }
            if (images.Count == 1)
            {
                return(images[0]);
            }

            // draw images on page
            int padding = 5;
            int height  = 0;
            int width   = 0;
            int n       = images.Count;

            for (int i = 0; i < n; i++)
            {
                BitmapSource current = images[i];
                if (width < current.PixelWidth)
                {
                    width = current.PixelWidth;
                }
                height += current.PixelHeight;
            }
            width  += 3;
            height += (n + 1) * padding;
            DrawingVisual  drawingVisual  = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, width, height));
            height = 0;
            for (int i = 0; i < images.Count; i++)
            {
                BitmapSource bitmap = images[i];
                if (bitmap.PixelWidth > width)
                {
                    width = bitmap.PixelWidth;
                }
                drawingContext.DrawImage(bitmap, new Rect(0, height, bitmap.PixelWidth, bitmap.PixelHeight));
                height += bitmap.PixelHeight + padding;
            }
            drawingContext.Close();

            // returns combined images
            RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

            bmp.Render(drawingVisual);
            return(bmp);
        }
 /// <summary>
 /// Returns a page images as single image.
 /// </summary>
 public static BitmapSource GetPdfPageImage(string filename, int pageIndex)
 {
     using (PdfImageViewer viewer = new PdfImageViewer(filename))
         return(GetPdfPageImage(viewer, pageIndex));
 }