Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 28, 2016 6:13 pm

Can someone point me in the right direction on how to use Spire.PDF to convert a color PDF into grayscale?

[email protected]
 
Posts: 1
Joined: Wed Apr 20, 2016 11:53 pm

Fri Apr 29, 2016 2:22 am

Hello,

Thanks for your inquiry.
Sorry that our Spire.PDF doesn't support to convert a color PDF into grayscale at present.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Tue May 28, 2019 9:17 pm

I am interested in being able to convert .PDF files to greyscale in C#. Is this a feature that might be added to your software?

bankacount
 
Posts: 2
Joined: Thu Feb 21, 2019 2:37 pm

Wed May 29, 2019 3:20 am

Hello Bankacount,

Thanks for your message.
Yes, we already added this as a new feature into our upgrade list, but I’m afraid it can’t be finished in a short time. Once it is available, I will let you know. As a workaround way, you can convert color Pdf file to black & white image firstly, and then drawing the image to a new Pdf. Below is the sample code for you reference. If there is any question, just feel free to write back.
Code: Select all
PdfDocument pdf = new PdfDocument(@"Color.pdf");
PdfDocument newPdf = new PdfDocument();
for (int i = 0; i <pdf.Pages.Count; i++)
{
    // Save to GreyScale images
    object image = pdf.SaveAsImage(i, 400, 400);
    Bitmap bm = image as Bitmap;
    for (int x = 0; x <bm.Width; x++)
    {
        for (int y = 0; y  <bm.Height; y++)
        {
            Color pixel = bm.GetPixel(x, y);
            int R = pixel.R;
            int G = pixel.G;
            int B = pixel.B;
            int value = ((R + G + B) / 3);
            bm.SetPixel(x, y, Color.FromArgb(value, value, value));
        }
     }
    MemoryStream GreyImgeStream = new MemoryStream();
    bm.Save(GreyImgeStream, ImageFormat.Png);
    //draw GreyScale images to new pdf
    PdfImage pdfimg = PdfImage.FromStream(GreyImgeStream);
    PdfPageBase page = newPdf.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
    //Set image display location and size
    float widthFitRate = pdfimg.PhysicalDimension.Width/(page.Canvas.ClientSize.Width);
    float heightFitRate = (pdfimg.PhysicalDimension.Height)/(page.Canvas.ClientSize.Height);
    float fitRate = Math.Max(widthFitRate, heightFitRate);
    float fitWidth = (pdfimg.PhysicalDimension.Width / fitRate);
    float fitHeight = (pdfimg.PhysicalDimension.Height / fitRate);
    PointF position = new PointF(0, 0);
    SizeF size = new SizeF(fitWidth, fitHeight);
    page.Canvas.DrawImage(pdfimg, position, size);
}
//save
newPdf.SaveToFile("Result.pdf", Spire.Pdf.FileFormat.PDF);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Thu May 30, 2019 8:37 pm

Thank you for your help! I look forward to seeing the update once it's completed.

Chad

bankacount
 
Posts: 2
Joined: Thu Feb 21, 2019 2:37 pm

Fri May 31, 2019 1:50 am

Hello Bankacount,

Ok, once it is achieved in the future, we will keep you informed.
Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Tue Feb 02, 2021 9:26 am

Hello,

Thanks for your patience.
Glad to inform you that we just released Spire.PDF Pack Version:7.2.0 which supports converting PDF into grayscale file. Welcome to download it from the following links and refer to the below sample code to test.
Our website: https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Nuget: https://www.nuget.org/packages/Spire.Pdf/7.2.0
Code: Select all
PdfGrayConverter convert = new PdfGrayConverter(inputFile);
convert.ToGrayPdf(outputFile);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF