// ---------------------------------------------------------------------------

        /**
         * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
         *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
         */
        public void RenderImage(ImageRenderInfo renderInfo)
        {
            PdfImageObject image = renderInfo.GetImage();
            //PdfName filter = (PdfName)image.Get(PdfName.FILTER);

            string imageName      = string.Format("{0:0000}_{1:0000}.{2}", PageIndex, ImagesList.Count, image.GetImageBytesType().FileExtension);
            var    pageImageIndex = new PageImageIndex
            {
                ImageName  = imageName,
                ImageIndex = ImagesList.Count,
                PageIndex  = PageIndex
            };
            var imageType = image.GetImageBytesType();
            //if (imageType != PdfImageObject.ImageBytesType.JBIG2)
            //{
            //var bmp = image.GetDrawingImage();

            // Write image to file
            string pathToSave = string.Format(@"{0}\{1}", OutputPath, imageName);

            //bmp.Save(string.Format(pathToSave));
            // bmp.Dispose();

            // Sometime gdi+ error happen. We must write byte directly to disk
            if (!Directory.Exists(OutputPath))
            {
                Directory.CreateDirectory(OutputPath);
            }
            var bytes = image.GetImageAsBytes();

            File.WriteAllBytes(pathToSave, bytes);

            ImagesList.Add(pageImageIndex, null);
            //}
        }
		// ---------------------------------------------------------------------------     
		/**
		 * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
		 *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
		 */
        public void RenderImage(ImageRenderInfo renderInfo)
        {
            PdfImageObject image = renderInfo.GetImage();
            //PdfName filter = (PdfName)image.Get(PdfName.FILTER);

            string imageName = string.Format("{0:0000}_{1:0000}.{2}", PageIndex, ImagesList.Count, image.GetImageBytesType().FileExtension);
            var pageImageIndex = new PageImageIndex
            {
                ImageName = imageName,
                ImageIndex = ImagesList.Count,
                PageIndex = PageIndex
            };
            var imageType = image.GetImageBytesType();
            //if (imageType != PdfImageObject.ImageBytesType.JBIG2)
            //{
            //var bmp = image.GetDrawingImage();                

            // Write image to file
            string pathToSave = string.Format(@"{0}\{1}", OutputPath, imageName);
            //bmp.Save(string.Format(pathToSave));
            // bmp.Dispose();

            // Sometime gdi+ error happen. We must write byte directly to disk
            if (!Directory.Exists(OutputPath))
                Directory.CreateDirectory(OutputPath);
            var bytes = image.GetImageAsBytes();
            File.WriteAllBytes(pathToSave, bytes);

            ImagesList.Add(pageImageIndex, null);
            //}
        }
Exemple #3
0
        /// <summary>
        /// will merge a group of images into one
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <param name="imageNames"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="index"></param>
        public KeyValuePair <PageImageIndex, System.Drawing.Image> MergeGroup(Dictionary <PageImageIndex, System.Drawing.Image> groupImages, string outputPath)
        {
            int maxWidth = 0, maxHeight = 0, position = 0;

            foreach (var imageInfo in groupImages)
            {
                maxWidth   = Math.Max(imageInfo.Value.Width, maxWidth);
                maxHeight += imageInfo.Value.Height;
            }

            // Create new image with max width and sum of all height
            System.Drawing.Bitmap   newImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
            System.Drawing.Graphics g        = System.Drawing.Graphics.FromImage(newImage);

            // merge all images
            g.Clear(System.Drawing.SystemColors.AppWorkspace);
            foreach (var img in groupImages)
            {
                if (position == 0)
                {
                    g.DrawImage(img.Value, new System.Drawing.Point(0, 0));
                    position++;
                    maxHeight = img.Value.Height;
                }
                else
                {
                    g.DrawImage(img.Value, new System.Drawing.Point(0, maxHeight));
                    maxHeight += img.Value.Height;
                }

                img.Value.Dispose();
                var imagePath = string.Format(@"{0}\{1}", outputPath, img.Key.ImageName);
                File.Delete(imagePath);
            }

            g.Dispose();

            var pageDetails                  = new PageImageIndex
            {
                PageIndex  = groupImages.First().Key.PageIndex,
                ImageIndex = 1,
                ImageName  = string.Format("{0:0000}_{1:0000}.jpg", groupImages.First().Key.PageIndex, 1)
            };

            var newImagePath = string.Format(@"{0}\{1}", outputPath, pageDetails.ImageName);

            newImage.Save(newImagePath);
            newImage.Dispose();

            return(new KeyValuePair <PageImageIndex, System.Drawing.Image>(pageDetails, null));
        }
        /// <summary>
        /// will merge a group of images into one
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <param name="imageNames"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="index"></param>
        public KeyValuePair <PageImageIndex, AppKit.NSImage> MergeGroup(Dictionary <PageImageIndex, AppKit.NSImage> groupImages, string outputPath)
        {
            double maxWidth = 0, maxHeight = 0;
            int    position = 0;

            foreach (var imageInfo in groupImages)
            {
                maxWidth   = Math.Max(imageInfo.Value.Size.Width, maxWidth);
                maxHeight += imageInfo.Value.Size.Height;
            }

            // Create new image with max width and sum of all height
            var newImage = new NSImage(new CoreGraphics.CGSize(maxWidth, maxHeight));

            newImage.LockFocus();

            // merge all images
            foreach (var img in groupImages)
            {
                if (position == 0)
                {
                    img.Value.Draw(new CGPoint(), new CGRect(), NSCompositingOperation.DestinationAtop, 1);
                    position++;
                    maxHeight = img.Value.Size.Height;
                }
                else
                {
                    img.Value.Draw(new CGPoint(0, maxHeight), new CGRect(), NSCompositingOperation.DestinationAtop, 1);
                    maxHeight += img.Value.Size.Height;
                }

                img.Value.Dispose();
                var imagePath = string.Format(@"{0}\{1}", outputPath, img.Key.ImageName);
                File.Delete(imagePath);
            }

            var pageDetails = new PageImageIndex
            {
                PageIndex  = groupImages.First().Key.PageIndex,
                ImageIndex = 1,
                ImageName  = string.Format("{0:0000}_{1:0000}.jpg", groupImages.First().Key.PageIndex, 1)
            };

            var newImagePath  = string.Format(@"{0}\{1}", outputPath, pageDetails.ImageName);
            var newImageData2 = newImage.AsTiff();

            newImageData2.Save(newImagePath, false);
            newImage.Dispose();

            return(new KeyValuePair <PageImageIndex, NSImage>(pageDetails, null));
        }
        /// <summary>
        /// will merge a group of images into one
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <param name="imageNames"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="index"></param>
        public KeyValuePair<PageImageIndex, System.Drawing.Image> MergeGroup(Dictionary<PageImageIndex, System.Drawing.Image> groupImages, string outputPath)
        {
            int maxWidth = 0, maxHeight = 0, position = 0;

            foreach (var imageInfo in groupImages)
            {
                maxWidth = Math.Max(imageInfo.Value.Width, maxWidth);
                maxHeight += imageInfo.Value.Height;
            }

            // Create new image with max width and sum of all height
            System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage);

            // merge all images
            g.Clear(System.Drawing.SystemColors.AppWorkspace);
            foreach (var img in groupImages)
            {
                if (position == 0)
                {
                    g.DrawImage(img.Value, new System.Drawing.Point(0, 0));
                    position++;
                    maxHeight = img.Value.Height;
                }
                else
                {
                    g.DrawImage(img.Value, new System.Drawing.Point(0, maxHeight));
                    maxHeight += img.Value.Height;
                }

                img.Value.Dispose();
                var imagePath = string.Format(@"{0}\{1}", outputPath, img.Key.ImageName);
                File.Delete(imagePath);
            }

            g.Dispose();

            var pageDetails = new PageImageIndex
            {
                PageIndex = groupImages.First().Key.PageIndex,
                ImageIndex = 1,
                ImageName = string.Format("{0:0000}_{1:0000}.jpg", groupImages.First().Key.PageIndex, 1)
            };

            var newImagePath = string.Format(@"{0}\{1}", outputPath, pageDetails.ImageName);
            newImage.Save(newImagePath);
            newImage.Dispose();

            return new KeyValuePair<PageImageIndex, System.Drawing.Image>(pageDetails, null);
        }