private void Do_Image(PdfStream stream) { byte[] imageBuffer = null; PdfName filter = PdfName.NONE; if (stream.Contains(PdfName.FILTER)) { filter = stream.GetAsName(PdfName.FILTER); } int imageWidth = stream.GetAsNumber(PdfName.WIDTH).IntValue; int imageHeight = stream.GetAsNumber(PdfName.HEIGHT).IntValue; int imageBpp = stream.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue; PRStream prStream = stream as PRStream; bool cannotReadImage = false; Bitmap image = null; try { PdfImageObject pdfImage = new PdfImageObject(prStream); image = pdfImage.GetDrawingImage() as Bitmap; string strFile = "C:\\PDF_Print\\pdf" + bmpfileidx.ToString() + ".bmp"; image.Save(strFile); bmpfileidx++; } catch { try { if (filter == PdfName.FLATEDECODE) { byte[] streamBuffer = PdfReader.GetStreamBytes(prStream); image = this.CreateBitmapFromFlateDecodeImage(streamBuffer, imageWidth, imageHeight, imageBpp); string strFile = "C:\\PDF_Print\\pdf_" + bmpfileidx.ToString() + ".bmp"; image.Save(strFile); bmpfileidx++; } } catch { cannotReadImage = true; } } if (!cannotReadImage) { image = this.ConvertToGrayscale(image); //image = this.ConvertToBlackWhite(image); using (var ms = new MemoryStream()) { System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 80L); myEncoderParameters.Param[0] = myEncoderParameter; image.Save(ms, jgpEncoder, myEncoderParameters); imageBuffer = ms.ToArray(); //imageBuffer = ms.GetBuffer(); iTextSharp.text.Image newImage = iTextSharp.text.Image.GetInstance(imageBuffer); newImage.SimplifyColorspace(); // PdfImage tempPdfImage = new PdfImage(newImage, newImage.ToString(), null); // prStream.Clear(); // prStream.SetDataRaw(imageBuffer); // prStream.Merge(tempPdfImage); // imageBuffer = newImage.OriginalData; // prStream.Clear(); prStream.SetData(imageBuffer, false, PRStream.NO_COMPRESSION); prStream.Put(PdfName.TYPE, PdfName.XOBJECT); prStream.Put(PdfName.SUBTYPE, PdfName.IMAGE); prStream.Put(PdfName.FILTER, PdfName.DCTDECODE); prStream.Put(PdfName.WIDTH, new PdfNumber(image.Width)); prStream.Put(PdfName.HEIGHT, new PdfNumber(image.Height)); prStream.Put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); prStream.Put(PdfName.COLORSPACE, PdfName.DEVICERGB); prStream.Put(PdfName.LENGTH, new PdfNumber(imageBuffer.LongLength)); } } }