简体   繁体   中英

.NET component for color PDF to grayscale conversion

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 :)

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.

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.

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/

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
                }
        }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM