校正的CMYK图像中的颜色显得褪色

时间:2014-03-19 11:33:31

标签: c# .net pdf compression itextsharp

我正在压缩包含许多图像的pdf。我在浏览PDF压缩时得到的以下代码。它适用于RBG格式图像,但在CMYK格式的情况下,图像以反色显示(作为负片)。不知何故,我能够转换反转的颜色,但图像颜色已经褪色。

请建议我该怎么办。提前谢谢。

{
    PdfReader.unethicalreading = true;
    string pdfFile = @"C:\TestPdf.pdf";
    PdfReader reader = new PdfReader(pdfFile);
    long quality = 50L;
    int n = reader.XrefSize;
    for (int i = 0; i < n; i++)
    {
        PdfObject obj = reader.GetPdfObject(i);
        if (obj == null || !obj.IsStream()) { continue; }
        PdfDictionary dict = (PdfDictionary)PdfReader.GetPdfObject(obj);
        PdfObject pdfcolorspace = dict.Get(PdfName.COLORSPACE);
        PdfName subType = (PdfName)PdfReader.GetPdfObject(dict.Get(PdfName.SUBTYPE));
        if (!PdfName.IMAGE.Equals(subType)) { continue; }
        PRStream stream = (PRStream)obj;
        try
        {
            PdfImageObject image = new PdfImageObject(stream);
            PdfName filter = (PdfName)image.Get(PdfName.FILTER);
            if ( PdfName.JBIG2DECODE.Equals(filter) || PdfName.JPXDECODE.Equals(filter) || PdfName.CCITTFAXDECODE.Equals(filter) || PdfName.FLATEDECODE.Equals(filter)) 
            continue;
            System.Drawing.Image img = image.GetDrawingImage();
            if (img == null) continue;
            var ll = image.GetImageBytesType();
            int width = img.Width;
            int height = img.Height;
            using (System.Drawing.Bitmap dotnetImg = new System.Drawing.Bitmap(img))
            {
                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
                System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
                eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter( System.Drawing.Imaging.Encoder.Quality, quality);
                using (MemoryStream msImg = new MemoryStream())
                {
                    dotnetImg.Save(msImg, codec, eParams);
                    msImg.Position = 0;
                    stream.Clear();
                    if (pdfcolorspace == PdfName.DEVICECMYK)
                    {
                        img.Save(msImg, ImageFormat.Jpeg);
                        stream.Put(PdfName.COLORSPACE, PdfName.DEVICECMYK);
                    }
                    else
                    {
                        stream.Put(PdfName.COLORSPACE, PdfName.DEVICERGB);
                    }
                    stream.SetData( msImg.ToArray(), true, PdfStream.BEST_COMPRESSION);
                    stream.Put(PdfName.TYPE, PdfName.XOBJECT);
                    stream.Put(PdfName.SUBTYPE, PdfName.IMAGE);
                    stream.Put(PdfName.FILTER, PdfName.DCTDECODE);
                    stream.Put(PdfName.WIDTH, new PdfNumber(width));
                    stream.Put(PdfName.HEIGHT, new PdfNumber(height));
                    stream.Put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
                }
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            reader.RemoveUnusedObjects();
        }
    }

    PdfStamper stamper = new PdfStamper(reader, new FileStream(@"C:\Compress.pdf", FileMode.Create), PdfWriter.VERSION_1_5);
    stamper.FormFlattening = false;
    stamper.SetFullCompression();
    stamper.Close();
    reader.Close();
}

0 个答案:

没有答案