无法在 PDF 文档中找到 ColorSpace 对象的位置

Unable find location of ColorSpace objects in PDF document

我想识别 PDF 中的 ColorSpace 对象并获取它们在页面中的位置(颜色空间的坐标、宽度和高度)。我尝试遍历 Contents.ContentContext.Resources.ColorSpaces 中的 BaseDataObject,我可以识别文件中的 Pantone Colorspaces(如屏幕截图所示),但无法找到有关 对象的位置(x、y、w 和 h)

在哪里可以找到 ColorSpaces 和嵌入图像等可见对象(打开文档时可见)的确切位置?

我正在使用“pdfclown”库从 PDF 中提取有关 ColorSpaces 的信息。任何输入都会有所帮助。提前致谢。

ContentScanner cs =  new ContentScanner(page);     
System.Collections.Generic.List<org.pdfclown.documents.contents.colorSpaces.ColorSpace> list = cs.Contents.ContentContext.Resources.ColorSpaces.Values.ToList();
    for (int i = 0; i < list.Count; i++)
    {
            org.pdfclown.objects.PdfArray array = (org.pdfclown.objects.PdfArray)list[i].BaseDataObject;
            foreach (org.pdfclown.objects.PdfObject s in array)
            { 
                //print colorspace and its x,y,w,h
            }
    }

PDF Document(有 CMYK 和 Pantone 颜色)

Screenshot

I want to identify the ColorSpace objects in PDF and fetch their location(coordinates, width and height of the colorspace) in the page.

我假设你指的是这里的方块:

注意,这些是 不是 PDF ColorSpace 对象,这些是一些简单的(矩形)路径充满了不同的颜色并在其上绘制了一些文本。

PDF ColorSpace不是彩色区域的具体渲染,它们是抽象颜色规范:

Colours may be described in any of a variety of colour systems, or colour spaces. Some colour spaces are related to device colour representation (grayscale, RGB, CMYK), others to human visual perception (CIE-based). Certain special features are also modelled as colour spaces: patterns, colour mapping, separations, and high-fidelity and multitone colour.

(ISO 32000-1, section 8.6 "Colour Spaces")

当您寻找具有坐标、宽度和高度的东西时,您正在寻找使用这些抽象色彩空间的绘图说明 , 不适用于 纯色空间 .

I tried traversing through the BaseDataObject in Contents.ContentContext.Resources.ColorSpaces, I can identify the Pantone Colorspaces in file (as shown in screenshot), but unable to find info regarding the location(x,y,w and h) of the object.

通过查看 cs.Contents.ContentContext.Resources.ColorSpaces,您可以获得所有特殊颜色空间的枚举在当前上下文中可用,但不是实际用途。要获得实际用法,您必须遍历 ContentScanner cs,即您必须检查当前上下文中的指令,例如像这样:

SeparationColorSpace space = null;
double X = 0, Y = 0, Width = 0, Height = 0;

void ScanForSpecialColorspaceUsage(ContentScanner cs)
{
    cs.MoveFirst();
    while (cs.MoveNext())
    {
        ContentObject content = cs.Current;
        if (content is CompositeObject)
        {
            ScanForSpecialColorspaceUsage(cs.ChildLevel);
        }
        else if (content is SetFillColorSpace _cs)
        {
            ColorSpace _space = cs.Contents.ContentContext.Resources.ColorSpaces[_cs.Name];
            space = _space as SeparationColorSpace;
        }
        else if (content is SetDeviceCMYKFillColor || content is SetDeviceGrayFillColor || content is SetDeviceRGBFillColor)
        {
            space = null;
        }
        else if (content is DrawRectangle _dr)
        {
            if (space != null)
            {
                X = _dr.X;
                Y = _dr.Y;
                Width = _dr.Width;
                Height = _dr.Height;
            }
        }
        else if (content is PaintPath _pp)
        {
            if (space != null && _pp.Filled && (X != 0 || Y != 0 || Width != 0 || Height != 0))
            {
                String name = ((PdfName)((PdfArray)space.BaseDataObject)[1]).ToString();
                Console.WriteLine("Filling rectangle at {0}, {1} with size {2}x{3} using {4}", X, Y, Width, Height, name);
            }
            X = 0;
            Y = 0;
            Width = 0;
            Height = 0;
        }
    }
}

注意: 这只是一个 proof-of-concept,尽可能简化仍然有效在您的 PDF 中找到上面屏幕截图中的方块。

对于一般的解决方案,您必须大大扩展它:

  • 该代码仅检查给定的内容扫描器,即仅检查已为其初始化的内容流,在您的情况下为页面内容流。

    从这样的上下文流中可以引用其他内容流,例如一个表单 XObject。要在通用文档中捕获有趣颜色空间的所有用法,您还必须递归地检查此类相关内容流。

  • 代码忽略当前变换矩阵。

    可以通过一条指令改变当前的变换矩阵,使按照指令完成的所有绘图的坐标都按照仿射变换进行改变。要在通用文档中正确获取所有坐标和尺寸,您必须对它们应用当前变换矩阵。

  • 代码忽略 save-graphics-state/restore-graphics-state 指令。

    当前图形状态(包括填充颜色和当前变换矩阵)可以存储在堆栈中并从中恢复。要在通用文档中获得正确的颜色、坐标和尺寸,您必须跟踪保存和恢复的图形状态(或使用 cs.State 中的数据进行颜色和转换,PDF Clown 会为您执行此操作)。

  • 代码仅查看 Separation 颜色空间。

    如果您对其他颜色空间也感兴趣,那么您已经概括了这一点。

  • 代码只理解非常具体、琐碎的路径:只理解由定义矩形的单个指令生成的路径。

    对于通用解决方案,您必须支持任意路径。