用于彩色 PDF 到灰度转换的 .NET 组件

发布于 2024-12-07 17:22:47 字数 1311 浏览 0 评论 0 原文

目前我使用Ghostscript彩色PDF转换为灰度PDF。现在我正在寻找可靠的.NET商业或非商业组件/库来替换ghostscript。我用谷歌搜索,没有找到任何能够轻松做到这一点或根本无法做到这一点的组件/库。

编辑 #1:

为什么 Ghostscript 对我不起作用:

我实现了 Ghostscript,并且正在使用它的本机 API。问题是 Ghostscript 不支持单个进程中解释器的多个实例。 -dJOBSERVER 模式也不适用于我,因为我不收集所有作业,它们会立即处理所有作业。碰巧 Ghostscript 正在处理大型作业,大约需要 20 分钟,同时我得到一些较小的作业,必须尽快处理,不能等待 20 分钟。另一个问题是 Ghostscript 页面处理的事件不容易捕获。我为 Ghostscript 标准输出消息编写了一个解析器,我可以读出已处理的页码,但在处理时不能读出每个页面,因为 GhostScript 会为一组已处理的页面推送消息。 Ghostscript 还有一些问题,例如生成错误的 pdf、重复字体问题......

您可以在这里找到我在 Ghostscript 中遇到的另一个问题:Ghostscript - PS 到 PDF - 倒置图像问题

-

更新一年后:

一年前我问了这个 问题。后来我使用 iTextSharp 制定了自己的解决方案。

您可以在此处查看将 PDF 转换为灰度的解决方案:

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

https://itextsharpextended.codeplex.com/

在大多数情况下对我有用:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自由如风 2024-12-14 17:22:47

不完全是一个答案,但我认为你太快地驳回 Ghostscript 了。

您是否了解 GhostScript API(用于进程内 Ghostscript)?或者 -dJOBSERVER 模式可以将一系列 PS 命令通过管道传输到其标准中?

然而,这仍然不会让你得到回调,而且它仍然不是多线程的。


如前所述,iText可以做到这一点,但这需要遍历所有内容和图像,寻找非灰度色彩空间并以特定于空间的方式转换它们。

您还必须替换您可能找到的任何图像中的像素数据。

好消息是 iText[Sharp] 能够在多个线程中运行,前提是每次从一个线程使用每个文档。

我怀疑建议的商业图书馆也是如此,这并不是一件好事。


然后我头顶上亮起了一盏灯……以灰度绘制。

混合模式和透明度组!

获取所有当前页面内容并将其粘贴到与覆盖页面的实心黑色矩形混合的透明度组中。我认为甚至还有 alpha 混合模式的亮度...让我们看看这里。

是的,PDF 参考第 11.6.5.2 节“软掩模字典”。您需要一个“亮度”组。

现在,坏消息。如果您切换到灰度的目标是节省空间,那么这将彻底失败。实际上,它会让每个文件变得更大一点……比如说每页 100 字节,不管怎样。

渲染 PDF 的软件最好也是非常热门的东西。你表弟的本科渲染项目不需要申请。这是高级图形内容,常见 PDF 文件很少使用,因此是最后要实现的内容。

所以...对于每个原始页面

  1. 创建一个新页面。

  2. 用黑色背景覆盖它。

  3. 用透明组中的白色矩形(之前已向后)覆盖它,该透明组使用软遮罩字典设置为原始页面内容的亮度(现在隐藏在 XObject 表单中)。

因为这都是您自己的代码,所以您将有足够的机会在每个页面的开头或结尾执行您想要执行的操作。

天啊,这简直太疯狂了!它确实需要一些 PDF-Fu,但远没有“在我逐步浏览文档时以各种方式转换每个颜色空间和图像”那么多。知识更深入,需要编写的代码更少。

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.

野鹿林 2024-12-14 17:22:47

这不是一个 .net 库,而是一个潜在的解决方法。您可以安装能够写入 PDF 文件的虚拟打印机。我建议使用 CutePDF,因为它是免费的,易于使用,并且可以很好地将大量文件格式“打印”为 PDF。使用 CutePDF 几乎可以完成普通打印机可以完成的所有操作,包括打印为灰度。

安装虚拟打印机后,您可以使用c#“打印”灰度版本。

编辑:我刚刚记得免费版本不是沉默的。一旦您打印到 CutePDF 打印机,它会要求您“另存为”。他们确实有一个可供购买的 SDK,但我不能说它是否能够帮助您转换为灰度。

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.

沙与沫 2024-12-14 17:22:47

如果商业产品对您来说是一个有效的选择,请允许我推荐 Amyuni PDF Creator 。网。通过使用它,您将能够枚举页面内的所有项目并相应地更改其颜色,图像也可以设置为灰度。 适用通常的免责声明

使用 Amyuni PDF Creator ActiveX 的示例代码,.Net 版本将类似:

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

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
                }
        }
小伙你站住 2024-12-14 17:22:47

一年前我问了这个问题。后来我使用 iTextSharp 制定了自己的解决方案。

您可以在此处查看将 PDF 转换为灰度的解决方案:https://itextsharpextended.codeplex.com/

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/

爱的十字路口 2024-12-14 17:22:47

iTextPdf 是一个用于创建/管理 pdf 的好产品,它有商业版和免费版。

查看 aspose.pdf for . net 它提供了以下功能以及更多功能。

  • 在 PDF 文档中添加和删除水印
  • 设置 PDF 文档的页边距、大小、方向、过渡类型、缩放系数和外观
  • ..

以及 这里是开源 pdf 库的列表。

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.

渔村楼浪 2024-12-14 17:22:47

经过大量调查后,我从 WebsupergooABCpdf >。他们的组件可以通过简单调用 重新着色方法。该组件是商业的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文