Ejemplo n.º 1
0
        public void Compress(PdfObject pdfObject, PdfDictionary pdfImageObject, PdfStamper stamper, int pageNum)
        {
            var image    = new PdfImageObject((PRStream)pdfImageObject);
            var oldBytes = image.GetImageAsBytes();

            var compressedBitmapInfo = CompressImage(oldBytes, CompressionQuality.Low, pageNum);

            Bitmap bitmap = compressedBitmapInfo.Item1;

            if (compressedBitmapInfo.Item1 == null)
            {
                return;
            }

            iTextSharp.text.Image compressedImage;
            using (var shrinkedBitmap = ShrinkImage(bitmap, _recommendedCompression))
            {
                if (shrinkedBitmap == null)
                {
                    return;
                }

                Image palettedImage = shrinkedBitmap;

                if (compressedBitmapInfo.Item2 != PixelFormat.Undefined)
                {
                    var bitsPerPixel = GetIntPalette(compressedBitmapInfo.Item2);

                    if (bitsPerPixel > 0)
                    {
                        palettedImage = GdiPaletteConverter.ConvertBitmapTo1Or8Bpp(shrinkedBitmap, bitsPerPixel);
                    }
                }

                using (var msInternal = new MemoryStream())
                {
                    //Checked for T_ file, resolution: Png format took less space up to 10% rather JPEG
                    //var newBytes = ConvertImageToBytes(palettedImage, 90);
                    palettedImage.Save(msInternal, _imageOutputFormat);
                    var newBytes = msInternal.ToArray();
                    compressedImage = iTextSharp.text.Image.GetInstance(newBytes);
                }

                shrinkedBitmap.Dispose();
                if (compressedBitmapInfo.Item1 != null)
                {
                    compressedBitmapInfo.Item1.Dispose();
                }
                palettedImage.Dispose();
            }

            PdfReader.KillIndirect(pdfObject);
            var mask = compressedImage.ImageMask;

            if (mask != null)
            {
                stamper.Writer.AddDirectImageSimple(mask);
            }
            stamper.Writer.AddDirectImageSimple(compressedImage, (PRIndirectReference)pdfObject);
        }
Ejemplo n.º 2
0
        private ImageFormat _imageOutputFormat = ImageFormat.Png; //for T_.., Sthlm.. files

        public void Compress(PdfObject pdfObject, PdfDictionary pdfImageObject, PdfStamper stamper, int pageNum)
        {
            var stream = (PRStream)pdfImageObject;
            var image  = new PdfImageObject(stream);

            var imageBytes = image.GetImageAsBytes();

            using (var ms = new MemoryStream(imageBytes))
            {
                var fileType     = FreeImage.GetFileTypeFromStream(ms);
                var sourceBitmap = FreeImage.LoadFromStream(ms, FREE_IMAGE_LOAD_FLAGS.PNG_IGNOREGAMMA, ref fileType);
                var dotnetBitmap = FreeImage.GetBitmap(sourceBitmap);

                iTextSharp.text.Image compressedImage;
                using (var shrinkedBitmap = ShrinkImage(dotnetBitmap, _recommendedCompression))
                {
                    if (shrinkedBitmap == null)
                    {
                        return;
                    }

                    System.Drawing.Image newImage = shrinkedBitmap;
                    if (DefineBitPerPixel(sourceBitmap) <= 8)
                    {
                        newImage = GdiPaletteConverter.ConvertBitmapTo1Or8Bpp(shrinkedBitmap, 1);
                    }

                    using (var msInternal = new MemoryStream())
                    {
                        newImage.Save(msInternal, _imageOutputFormat);
                        var newBytes = msInternal.ToArray();
                        compressedImage = iTextSharp.text.Image.GetInstance(newBytes);
                    }

                    shrinkedBitmap.Dispose();
                    newImage.Dispose();
                }

                dotnetBitmap.Dispose();
                FreeImage.Unload(sourceBitmap);
                sourceBitmap.SetNull();

                PdfReader.KillIndirect(pdfObject);
                stamper.Writer.CompressionLevel = PdfStream.BEST_COMPRESSION;

                var mask = compressedImage.ImageMask;
                if (mask != null)
                {
                    stamper.Writer.AddDirectImageSimple(mask);
                }
                stamper.Writer.AddDirectImageSimple(compressedImage, (PRIndirectReference)pdfObject);
            }
        }