【问题标题】:How to add an image into a spot colour channel for a PDF document using ABCpdf?如何使用 ABCpdf 将图像添加到 PDF 文档的专色通道中?
【发布时间】:2019-10-11 11:23:17
【问题描述】:

在以编程方式使用 ABCpdf 时,我发现需要将新图像添加到 PDF 文件中的特定自定义颜色通道(称为专色)中。通常这类通道用于印刷行业,用于在印刷材料上添加特殊颜色,如金色、银色等,并指示印刷机在此过程中使用特殊油墨。每个专色通道只能包含 1 种颜色(根据其 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();

到目前为止一切正常,文本“我的文本”出现在已添加的正确“黄金”频道的输出 PDF 文件中。

所以我将theDoc.AddText("My Text"); 行替换为theDoc.AddImage("images/gradient_alpha.png")',希望它能将此图像添加到我创建的当前色彩空间中,但它不起作用。

手动创建新的色彩空间、图像对象和像素图对象也不起作用:

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());

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

【问题讨论】:

    标签: c# .net abcpdf


    【解决方案1】:

    要完成此操作,您需要通过AddImageObject 将图像作为对象添加到文档中,并将其作为PixMap 从文档的ObjectSoup 容器中提取,并在具有目标色彩空间的PixMap 上应用Recolor

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

    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 操作库。文档内容广泛但并不总是明确的,因此需要大量阅读和实验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      相关资源
      最近更新 更多