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.

Fri Jun 14, 2019 11:03 pm

Hello .. Good night!
My question ...
Is it possible to detect monochrome (black and white) or colored pages within a pdf file?
The idea is to create an algorithm that returns the quantities of color or mono pages of each analyzed file.
Does Spire.PDF have this functionality?
Right now, thank you very much for your attention.

gafforelli
 
Posts: 37
Joined: Sat Sep 15, 2018 6:38 pm

Mon Jun 17, 2019 7:05 am

Hello,

Thank you for contacting.
Sorry to tell that our Spire.PDF doesn't support detecting monochrome (black and white) or colored pages within a PDF file at present. We will add it as a new feature into our upgrade list. Once it is achieved, we will let you know. Apologize for the inconvenience caused. In addition, could you please share us with your sample PDF file? So that we can better test your case when the feature is done. You could send it via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

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

Mon Jun 17, 2019 2:06 pm

Hi Lisa, good morning!
Thank you very much for the quick return.
Great news to know that they will work on this functionality.
I'll send you a sample document, but basically, it's recognizing the color scheme of a page and the context applied will be the printing.
There is a demand to know which pages are black and white or colored for the purpose of controlling PDF printing on color printers.
In the sequence I'll send the email.
Thank you so much again.

gafforelli
 
Posts: 37
Joined: Sat Sep 15, 2018 6:38 pm

Tue Jun 18, 2019 1:41 am

Hello,

Thanks for your feedback. Look forward to your email.
Have a great day!

Sincerely,
Lisa
E-iceblue support team
User avatar

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

Fri Jul 12, 2019 8:27 am

Hello,

Thank you for sharing your sample files via email.
After further investigations, please use the following code to detect whether the PDF page is black/white or not. If there is any question, welcome to write back.
Code: Select all
static void Main(string[] args)
{
     //Please pay attention to apply the valid license, otherwise the warning message will affect the result
     Spire.License.LicenseProvider.SetLicenseKey("your license key");
     PdfDocument doc = new PdfDocument();
     doc.LoadFromFile(@"mono.pdf");
     for (int i = 0; i < doc.Pages.Count; i++)
     {
         var image = doc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Bitmap);
         Console.WriteLine("The page" + (i + 1) + "is black and white:" + isBlackWhite(image));
         image.Dispose();            }
     Console.ReadKey();
}
   public static bool isBlackWhite(Image image)
   {
       Color c = new Color();
       using (Bitmap bmp = new Bitmap(image))
       {
           for (int y = 0; y < bmp.Height; y++)
           {
               for (int x = 0; x < bmp.Width; x++)
               {
                   c = bmp.GetPixel(x, y);                       
                   if (GetRGBDiff(c.R, c.G, c.B) > 50)
                   {
                       return false;
                   }
               }
           }               
           return true;
       }
   }       
   public static int GetRGBDiff(int r, int g, int b)
   {                   
       int[] _int = new int[]{Math.Abs(r-g),Math.Abs(r-b),Math.Abs(g-b) };
       int max = 0;
       for (int y = 0; y < _int.Length; y++)
       {
           if (_int[y] > max)
           {
               max = _int[y];
           }
       }
       return max;
   }

Sincerely,
Lisa
E-iceblue support team
User avatar

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

Wed Jul 17, 2019 7:59 pm

Hello, how are you?
Thank you very much for the feedback and sample code.

:)

gafforelli
 
Posts: 37
Joined: Sat Sep 15, 2018 6:38 pm

Thu Jul 18, 2019 1:23 am

Hello,

I’m doing well, thanks for your feedback. Just feel free to contact us if you need other helps.
Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

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

Return to Spire.PDF