gpt4 book ai didi

c# - 如何使用 ABCpdf 将图像添加到 PDF 文档的专色 channel 中?

转载 作者:行者123 更新时间:2023-11-30 22:52:15 25 4
gpt4 key购买 nike

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

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

最佳答案

为此,您需要通过 AddImageObject 将图像作为对象添加到文档中并将其作为 PixMap 从文档的 ObjectSoup 容器中提取并应用 Recolor在具有目标颜色空间的 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 操作库。文档内容广泛但并不总是明确的,因此需要大量阅读和实验。

关于c# - 如何使用 ABCpdf 将图像添加到 PDF 文档的专色 channel 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58340241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com