We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF to grayscale not working as expected

I am using the Aspose PDF library for .NET, version 10.7.


I am trying to convert PDFs to grayscale and using the code posted at http://www.aspose.com/docs/display/pdfnet/Convert+a+PDF+from+RGB+colorspace+to+Grayscale

There are three issues I’m finding:

  1. The images are not being converted into grayscale, but rather as black and white, which leads to poor converted image quality
  2. Not all images are being converted and not all background colors convert to grayscale.
  3. When attempting to convert some PDF documents an Index was outside the bounds of the array exception is thrown by the Aspose.Imaging.Save method

For issue (1) and (2), I’ve attached two PDFs: Brochure.PDF, which is the original, and Brochure-Gray.pdf, which is the PDF converted to grayscale. Note how the images are black and white and not grayscale. (They also have the “Evaluation only” message, which is perplexing since the Aspose.Pdf software is licensed. Do I need to also buy an Aspose.Imaging license to convert PDFs with images to grayscale?)

Also note that the light blue background on the left was converted to white. I would have expected it to be a light gray. Also note that the purple backgroun on the top right isn’t converted - it’s purple in the grayscale version!



For issue (3) I’ve attached a sample PDF named Brochure3Panel.pdf. When attempting to convert this to grayscale it throws the following exception:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
›.‚.(ƒ‡ ) +275
‰‚.•‚.(Int32 , Int32& , Int32& , Int32[] , Int32& , ‚ ) +403
‰‚.•‚.‘(Int32[] , Int32 , Int32 , Int32 , ‚ , Int32[] , Int32& , Int32[] , Int32& , Boolean ) +308
‰‚.•‚.‰e(Int32[] , Rectangle , ˜‚ ) +930
.‡.Process(Rectangle , Int32[] , Point , Point ) +62
.„.Process(Rectangle , Int32[] , Point , Point ) +126
.†.Process(Rectangle ) +239
.ˆ.Process(Rectangle , , ˆ , Int32 , Int32 ) +1118
..€›†(Rectangle , Œ‹† ) +65
.†.Process(Rectangle ) +43
.ˆ.Process(Rectangle , , ˆ , Int32 , Int32 ) +1118
.„.“(Rectangle , Int32[] , Boolean , Œ‹† ) +236
.„.›“(Rectangle , Boolean , Œ‹† ) +359
.„.€›†(Rectangle , Œ‹† ) +76
.›‡.Save(„ , —‡ , Boolean , —™ , ž ) +797
.„.‰(Image , Stream ,  ) +417
Aspose.Imaging.Image.Save(Stream ,  ) +72

[„: Image export failed.]
Aspose.Imaging.Image.Save(Stream ,  ) +197
Aspose.Pdf.XImageCollection.†•‘(Stream , †a , ›š ) +260
Aspose.Pdf.XImageCollection.š™(Int32 , Stream , Int32 , Boolean ) +178
Aspose.Pdf.XImageCollection.Replace(Int32 , Stream , Int32 , Boolean ) +32
Aspose.Pdf.Engine.Presentation.RgbToDeviceGrayConversionStrategy.˜‘(Resources ) +140
Aspose.Pdf.Engine.Presentation.RgbToDeviceGrayConversionStrategy.Convert(Page page) +42
appNet.Helpers.MakePdfGrayscale(…) +1808



Thanks!
Hi Scott,

Thanks for your inquiry.

scott.k.mitchell:

For issue (1) and (2), I've attached two PDFs: Brochure.PDF, which is the original, andBrochure-Gray.pdf, which is the PDF converted to grayscale. Note how the images are black and white and not grayscale. (They also have the "Evaluation only" message, which is perplexing since the Aspose.Pdf software is licensed. Do I need to also buy an Aspose.Imaging license to convert PDFs with images to grayscale?)

Also note that the light blue background on the left was converted to white. I would have expected it to be a light gray. Also note that the purple backgroun on the top right isn't converted - it's purple in the grayscale version!



I have tested your document using Aspose.Pdf for .NET 11.1.0 and noticed the black and white issue and colored elements issue in resultant PDF document. We have already noticed the issue so we have linked your thread to the following issues. However, Aspose.Imaging evaluation mark was fixed in a later version, so please download and try latest version of Aspose.Pdf for .NET, it will resolve this issue.

PDFNEWNET-39246: All the colored elements are not being converted to grayscale
PDFNEWNET-39257: Conversion is black and white instead grayscale

scott.k.mitchell:
For issue (3) I've attached a sample PDF named Brochure3Panel.pdf. When attempting to convert this to grayscale it throws the following exception:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   ›.‚.(ƒ‡ ) +275
   ‰‚.•‚.(Int32 , Int32& , Int32& , Int32[] , Int32& , ‚ ) +403
   ‰‚.•‚.‘(Int32[] , Int32 , Int32 , Int32 , ‚ , Int32[] , Int32& , Int32[] , Int32& , Boolean ) +308
   ‰‚.•‚.‰e(Int32[] , Rectangle , ˜‚ ) +930
   .‡.Process(Rectangle , Int32[] , Point , Point ) +62
   .„.Process(Rectangle , Int32[] , Point , Point ) +126
   .†.Process(Rectangle ) +239
   .ˆ.Process(Rectangle ,  , ˆ , Int32 , Int32 ) +1118
   ..€›†(Rectangle , Œ‹† ) +65
   .†.Process(Rectangle ) +43
   .ˆ.Process(Rectangle ,  , ˆ , Int32 , Int32 ) +1118
   .„.“(Rectangle , Int32[] , Boolean , Œ‹† ) +236
   .„.›“(Rectangle , Boolean , Œ‹† ) +359
   .„.€›†(Rectangle , Œ‹† ) +76
   .›‡.Save(„ , —‡ , Boolean , —™ , ž ) +797
   .„.‰(Image , Stream ,  ) +417
   Aspose.Imaging.Image.Save(Stream ,  ) +72

[„: Image export failed.]
Aspose.Imaging.Image.Save(Stream ,  ) +197
Aspose.Pdf.XImageCollection.†•‘(Stream , †a , ›š ) +260
Aspose.Pdf.XImageCollection.š™(Int32 , Stream , Int32 , Boolean ) +178
Aspose.Pdf.XImageCollection.Replace(Int32 , Stream , Int32 , Boolean ) +32
Aspose.Pdf.Engine.Presentation.RgbToDeviceGrayConversionStrategy.˜‘(Resources ) +140
Aspose.Pdf.Engine.Presentation.RgbToDeviceGrayConversionStrategy.Convert(Page page) +42
appNet.Helpers.MakePdfGrayscale(…) +1808



I have noticed the reported exception with Brochure3Panel.pdf document, so logged a ticket PDFNEWNET-40125 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress.

We are sorry for the inconvenience caused.

Best Regards,

Tilal,


Are there any updates on this? I also need to export a PPT as Grayscale and have been unable to figure out how to do it. I get Black and White (with no gray scale) and several panel elements remain in color.

Thanks,
mike

Using document As New Aspose.Pdf.Document(strSource)
Dim strategy As Aspose.Pdf.RgbToDeviceGrayConversionStrategy = New Aspose.Pdf.RgbToDeviceGrayConversionStrategy()
Dim idxPage As Integer = 1
For Each p As Aspose.Pdf.Page In document.Pages

’ get instance of particular page inside PDF
Dim inner_page As Aspose.Pdf.Page = document.Pages(idxPage)
’ convert the RGB colorspace image to GrayScale colorspace
strategy.Convert(inner_page)
idxPage += 1
Next p
’ save resultant file
document.Save(strDest)
End Using

Hi Mike,


Thanks for your inquiry. I am afraid the above reported issues are still not resolved, as our product team is looking into other issues in the queue. Furthermore, please note usually issues vary from file to file. We will appreciate it if you please share your sample PDF document here, we will double check the issue and will guide you accordingly.

We are sorry for the inconvenience.

Best Regards,

Thanks for looking into this Tilal.


In case anyone else is interested, I came up with a code way to convert the bitmap to grayscale and then save that converted bitmap to the pdf - and not use the aspose feature. The code fragment is below.

Dim grayscale As New Imaging.ColorMatrix(New Single()() _
{
New Single() {0.299, 0.299, 0.299, 0, 0},
New Single() {0.587, 0.587, 0.587, 0, 0},
New Single() {0.114, 0.114, 0.114, 0, 0},
New Single() {0, 0, 0, 1, 0},
New Single() {0, 0, 0, 0, 1}
})


Dim sld As Aspose.Slides.Slide = sf.Slides(1)

'Convert slide to image
Dim bmp As Bitmap = sld.GetThumbnail(1.0, 1.0)

'–convert to grayscale (converts bmp to grayscale)
Dim imgattr As New Imaging.ImageAttributes()
imgattr.SetColorMatrix(grayscale)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imgattr)
End Using

Hi Mike,


Thanks for sharing the workaround. However, we will notify you as soon as we resolve above reported issues, so the requirement can be accomplished directly within Aspose.Pdf API.

Best Regards,

The issues you have found earlier (filed as PDFNET-39257) have been fixed in Aspose.Pdf for .NET 17.3.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(12)

4 years later and this is still not fixed in Aspose.PDF 20.09.0.

I use this code:

    private static Document ConvertToGrayscale(Document document)
    {
        var strategy = new RgbToDeviceGrayConversionStrategy();
        
        foreach (var p in document.Pages)
        {
            strategy.Convert(p);
        }

        return document;
    }

For the majority of my pdf’s this works fine but I just noticed that on certain files, some images are not converted to grayscale but retain their color. And also the file size is increased from the original file. I followed the example from here: https://docs.aspose.com/pdf/java/optimize-pdf-document/#OptimizePDFDocument-ConvertaPDFfromRGBcolorspacetoGrayscale

Sadly i cannot share my document because of privacy but be aware that this issue still exists.

@stebe95

We really apologize for the inconvenience faced.

Please note that some issues are related to specific PDF files and in order to investigate and fix them properly, we need that specific document. Would you kindly provide your sample document in a private message if you cannot share it publicly. We will test the scenario in our environment and address the issue accordingly.

You can send a private message by clicking over username and pressing Blue Message Button.

The issues you have found earlier (filed as PDFNET-40125) have been fixed in Aspose.PDF for .NET 21.7.