4

Currently i use Ghostscript to convert color PDF's to grayscale PDF's. Now i'm looking for reliable .NET commercial or not commercial component/library for ghostscript replacement. I googled and I did not find any component/library that is able to do that easily or to do that at all.

EDIT #1:

Why Ghostscript does not work for me:

I implemented Ghostscript and I'm using it's native API's. The problem is that Ghostscript does not support multiple instances of the interpreter within a single process. -dJOBSERVER mode also does not work for me because i don't collect all job and them process them all at once. It happens that Ghostscript is processing large job which takes around 20 minutes and meanwhile i get some smaller job which has to be processed ASAP and cannot wait 20 minutes. Other problem is that Ghostscript page processed events are not easily to catch. I wrote a parser for ghostscript stdout messages and i can read out processed page number but not for each page when it's processed as ghostscript pushes message for group of processed pages. There are couple of more problems with Ghostscript like producing bad pdf's, duplicating font problems.....

You can find one more problem i had with ghostscript here: Ghostscript - PS to PDF - Inverted images problem

-

a year after UPDATE:

Before a year a go i asked this question. Later i made my own solution by using iTextSharp.

You can take a look at the converting PDF to grayscale solution here:

http://habjan.blogspot.com/2013/09/proof-of-concept-converting-pdf-files.html

or

https://itextsharpextended.codeplex.com/

Works for me in most cases :)

Community
  • 1
  • 1
HABJAN
  • 9,212
  • 3
  • 35
  • 59

6 Answers6

3

Not quite an answer, but I think you dismiss Ghostscript too quickly.

Are you aware of the GhostScript API (for in-process Ghostscript)? Or of the -dJOBSERVER mode that can take a series of PS commands piped to its standard in?

That still won't get you your callbacks however, and it's still not multi-threaded.


As previously stated, iText could do it, but it would be a matter of walking through all the content and images looking for non-grayscale color spaces and converting them in a space-specific manner.

You'd also have to replace the pixel data in any images you might find.

The good news is that iText[Sharp] is capable of operating in multiple threads, provided each document is used from one thread at a time.

I suspect this is also the case for the suggested commercial library, which isn't such a good deal.


And then a light went on above my head... drawn in gray scale.

Blending modes and transparency groups!

Take all the current page content and stick it in a transparency group that is blended with a solid black rectangle that covers the page. I think there's even a luminosity to alpha blend mode... lets see here.

Yep, PDF reference section 11.6.5.2 "Soft Mask Dictionaries". You'll want a "luminosity" group.

Now, the bad news. If your goal in switching to gray scale is to save space, this will fail utterly. It'll actually make each file a little larger... say a 100 bytes per page, give or take.

The software rendering the PDF better be pretty hot stuff too. Your cousin's undergrad rendering project need not apply. This is advanced graphics stuff here, infrequently used by Common PDF Files, so the last sort of thing to be implemented.

So... For each original page

  1. Create a new page.

  2. Cover it with a black background.

  3. Cover it with a white rectangle (had it backwards earlier) in a transparency group that uses a soft mask dictionary set to be the luminosity of the original page's content (now stashed in an XObject Form).

Because this is all your own code, you'll have ample opportunity to do whatever it is you want to do at the beginning or end of each page.

By golly, that's just crazy enough to work! It does require some PDF-Fu, but not nearly as much as the "convert each color space and image in various ways as I step through the document". Deeper knowledge, less code to write.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • Hi, i'm aware of Ghostscript API, the problem with that is you can initialize only one instance of ghostscript per application instance. JOBSERVER mode will also not work for me because i have to process jobs as they came. So, while i'm processing large job which takes 20 minutes meanwhile i can get 3 more jobs which has to wait untill first one is done. Using iTextSharp to walk through pdf and convert content and images to grayscale is complex. There can be alot of problems with transparency, shadings, color blending. – HABJAN Oct 07 '11 at 16:40
  • About your last suggestion, i don't know if printer will recognize pdf page as grayscale if i use suggested logic. – HABJAN Oct 07 '11 at 16:41
  • Event i will not use your logic, +100 for your effort. ;-) – HABJAN Oct 10 '11 at 06:19
2

This isn't a .net library, but rather a potential work-around. You could install a virtual printer that is capable of writing PDF files. I would suggest CutePDF, as it's free, easy to use and does a great job 'printing' a large number of file formats to PDF. You can do nearly everything with CutePDF that you can do with a normal printer, including printing to grayscale.

After the virtual printer is installed, you can use c# to 'print' a greyscale version.

Edit: I just remembered that the free version is not silent. Once you print to the CutePDF printer, it will ask you to 'Save As'. They do have an SDK available for purchase, but I couldn't say whether it would be able to help you convert to grayscale.

Chronicide
  • 1,112
  • 1
  • 9
  • 32
  • Thanks for your answer, i already considered this kind of option and limitation here is that you cannot convert more than 1 pdf document at a same time. Anyway, +1 for your help. Thanks – HABJAN Oct 03 '11 at 06:49
1

Before a year a go i asked this question. Later i made my own solution by using iTextSharp.

You can take a look at the converting PDF to grayscale solution here: https://itextsharpextended.codeplex.com/

HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • I saw your solution, it sound fine for "many cases" (I would not say most cases), it is a good starting point. However this solution as it stands have many scenarios where it is not going to work: color raster images (specially images with custom palettes), PDF drawings that use non-standard color spaces, PDF annotations, PDF XForms, PDF patterns, etc. – yms Sep 25 '13 at 17:38
1

If a commercial product is a valid option for you, allow me to recommend Amyuni PDF Creator .Net. By using it you will be able to enumerate all items inside the page and change their colors accordingly, images can also be set as grayscale. Usual disclaimers apply

Sample code using Amyuni PDF Creator ActiveX, the .Net version would be similar:

        pdfdoc.ReportState = ReportStateConstants.acReportStateDesign;
        object[] page_items = (object[])pdfdoc.get_ObjectAttribute("Pages[1]", "Objects");

        string[] color_attributes = new string[] { "TextColor", "BackColor", "BorderColor", "StrokeColor" };
        foreach (acObject page_item in page_items)
        {
            object _type = page_item["ObjectType"];
            if ((ACPDFCREACTIVEX.ObjectTypeConstants)_type == ACPDFCREACTIVEX.ObjectTypeConstants.acObjectTypePicture)
            {
                page_item["GrayScale"] = true;
            }
            else
                foreach (string attr_name in color_attributes)
                {
                    try
                    {
                        Color color = System.Drawing.ColorTranslator.FromWin32((int)page_item[attr_name]);
                        int grayColor = (int)(0.3 * color.R + 0.59 * color.G + 0.11 * color.B);
                        int newColorRef = System.Drawing.ColorTranslator.ToWin32(Color.FromArgb(grayColor, grayColor, grayColor));
                        page_item[attr_name] = newColorRef;
                    }
                    catch { } //not all items have all kinds of color attributes
                }
        }
yms
  • 10,361
  • 3
  • 38
  • 68
  • I saw Amyuni PDF Creator and i could not find a way to easily convert items to grayscale. Anyway, thanks for suggestion. – HABJAN Oct 07 '11 at 16:45
0

iTextPdf a good product for creating/managing pdf it has got both commercial and free versions.

Have a look at aspose.pdf for .net it provides below features and a lot more.

  • Add and remove watermarks from PDF document
  • Set page margin, size, orientation, transition type, zoom factor and appearance of PDF document
  • ..

And here is a list of open source pdf libraries.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • Hi, iText is java library. In aspose.pdf i could not find any color conversion features. Thanks. – HABJAN Oct 07 '11 at 16:47
0

After a lot of investigation i found out about ABCpdf from Websupergoo. Their component can easily convert any PDF page to grayscale by simple call to Recolor method. The component is commercial.

HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • xfinium-pdf also a best and simple one considered for recoloring.(http://www.xfiniumpdf.com/xfinium-pdf-wpf-silverlight-winrt/xfinium-pdf-features.html) – Jay Sep 25 '13 at 03:05