如何使用 ABCpdf 将图像添加到 PDF 文档的专色通道中?

How to add an image into a spot colour channel for a PDF document using ABCpdf?

在以编程方式使用 ABCpdf 时,我发现需要将新图像添加到 PDF 文件中的特定自定义颜色通道(称为专色)中。通常这些类型的通道用于印刷行业,以在印刷品上添加金色、银色等特殊颜色 material 并指示印刷机在此过程中使用特殊油墨。每个专色通道只能包含一种颜色(根据其 alpha 值具有不同的强度)。

问题在于弄清楚如何使用 ABCpdf 轻松地做到这一点。虽然该文档确实有一个使用 AddColorSpaceSpot 函数结合 AddText 函数的示例代码,但没有说明如何使用图像完成此操作。任何用 AddImage 替换 AddText 的尝试都是徒劳的,因为它只会将图像添加为普通的 RGB 或 CMYK 对象。

文档中的示例代码如下:

Doc theDoc = new Doc();
theDoc.Rect.Inset(20, 20);
theDoc.FontSize = 300;
theDoc.ColorSpace = theDoc.AddColorSpaceSpot("GOLD", "0 0 100 0");
for (int i = 1; i <= 10; i++) {
  theDoc.Color.Gray = 255 / i;
  theDoc.AddText(theDoc.Color.Gray.ToString());
  theDoc.Rect.Move(25, -50);
}
theDoc.Save(Server.MapPath("docaddcolorspacespot.pdf"));
theDoc.Clear();

基于上面的代码,我在控制台应用程序中尝试了以下操作:

Doc theDoc = new Doc();
theDoc.Rect.Inset(10, 10);
theDoc.ColorSpace = theDoc.AddColorSpaceSpot("Gold", "0 0 100 0");
theDoc.FontSize = 300;
theDoc.Color.Gray = 255;
theDoc.AddText("My Text");
theDoc.Save($"output/spot_test_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.pdf");
theDoc.Clear();

到目前为止一切正常,文本 "My Text" 出现在添加的正确 "Gold" 通道中的输出 PDF 文件中。

所以我用 theDoc.AddImage("images/gradient_alpha.png")' 替换了 theDoc.AddText("My Text"); 行,希望它将此图像添加到我创建的当前颜色 space 中,但它没有用。

手动创建新颜色space,图像对象和像素图对象也不起作用:

Doc theDoc = new Doc();

var cs = new ColorSpace(theDoc.ObjectSoup, ColorSpaceType.Separation);
cs.Doc.Color.String = "0 0 100 0";
cs.Doc.Color.Name = "Gold";
theDoc.ColorSpace = cs.ID;

var image = new XImage();
image.SetFile("images/gradient_alpha.png");
PixMap px = PixMap.FromXImage(theDoc.ObjectSoup, image);
px.Recolor(cs);

theDoc.AddImage(px.GetBitmap());

那么,我们如何正确地将图像添加到专色通道中呢? 阅读下面的答案找出答案!

为此,您需要通过 AddImageObject and extract it as a PixMap from the document's ObjectSoup container and apply a Recolor 在具有目标颜色 space 的 PixMap 上将图像作为对象添加到文档中。

这是我成功使用的最终代码:

Doc theDoc = new Doc();
theDoc.ColorSpace = theDoc.AddColorSpaceSpot("Gold", "0 0 100 0");
ColorSpace goldSpotColor = (ColorSpace)theDoc.ObjectSoup[theDoc.ColorSpace];

XImage image = XImage.FromFile("images/gradient_alpha.png", new XReadOptions());

int theID = theDoc.AddImageObject(image, true);
int imageID = theDoc.GetInfoInt(theID, "XObject");
PixMap thePM = (PixMap)theDoc.ObjectSoup[imageID];

thePM.Recolor(goldSpotColor);

theDoc.Save($"output/spot_test_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.pdf");
theDoc.Clear();

乍一看,要实现我们想要的目标似乎需要太多步骤,但 ABCpdf 是一个非常强大的低级 PDF 操作库。文档内容广泛但并不总是明确的,因此需要大量阅读和实验。