提取 pdf 矢量对象

Extract pdf vector objects

有没有办法用C#提取特定颜色的矢量对象的坐标和尺寸?例如,像“dieline”或“cut line”?我尝试使用 PDFSharp 库,但它似乎没有这样的功能。我可以提取样本,但不能提取使用相应颜色的对象的坐标或尺寸。

您可以使用 PDF4NET 列出来自 PDF 页面的矢量图形,如下所示(假设您的模切线和切割线是分色):

PDFFixedDocument document = new PDFFixedDocument(pdfFile);

PDFContentExtractor ce = new PDFContentExtractor(document.Pages[pageNumber]);
PDFVisualObjectCollection pageVisualObjects = ce.ExtractVisualObjects(false);

for (int i = 0; i < pageVisualObjects.Count; i++)
{
    switch (pageVisualObjects[i].Type)
    {
        case PDFVisualObjectType.Path:
            PDFPathVisualObject pathVisualObject = pageVisualObjects[i] as PDFPathVisualObject;
            if ((pathVisualObject.Pen != null) && 
                (pathVisualObject.Pen.Color.ColorSpace.Type == PDFColorSpaceType.Separation))
            {
                PDFSeparationColorSpace cs = 
                    pathVisualObject.Pen.Color.ColorSpace as PDFSeparationColorSpace;
                if (cs.Colorant == "dieline")
                {
                    Console.WriteLine("dieline stroke");
                    for (int j = 0; j < pathVisualObject.PathItems.Count; j++)
                    {
                        Console.Write("{0}: ", pathVisualObject.PathItems[j].Type);
                        if (pathVisualObject.PathItems[j].Points != null)
                        {
                            for (int k = 0; k < pathVisualObject.PathItems[j].Points.Length; k++)
                            {
                                Console.Write("[{0:0.######}, {1:0.######}] ", 
                                    pathVisualObject.PathItems[j].Points[k].X, 
                                    pathVisualObject.PathItems[j].Points[k].Y);
                            }
                        }
                        Console.WriteLine("");
                    }
                }
            }
            if ((pathVisualObject.Brush != null) &&
                (pathVisualObject.Brush.Color.ColorSpace.Type == PDFColorSpaceType.Separation))
            {
                PDFSeparationColorSpace cs =
                    pathVisualObject.Brush.Color.ColorSpace as PDFSeparationColorSpace;
                if (cs.Colorant == "dieline")
                {
                    Console.WriteLine("dieline fill");
                    for (int j = 0; j < pathVisualObject.PathItems.Count; j++)
                    {
                        Console.Write("{0}: ", pathVisualObject.PathItems[j].Type);
                        if (pathVisualObject.PathItems[j].Points != null)
                        {
                            for (int k = 0; k < pathVisualObject.PathItems[j].Points.Length; k++)
                            {
                                Console.Write("[{0:0.######}, {1:0.######}] ",
                                    pathVisualObject.PathItems[j].Points[k].X,
                                    pathVisualObject.PathItems[j].Points[k].Y);
                            }
                        }
                        Console.WriteLine("");
                    }
                }
            }
            break;
    }
}

这是包含填充和描边矩形的示例 PDF 文件的输出:

dieline stroke
MoveTo: [20, 605]
LineTo: [270, 605]
LineTo: [270, 705]
LineTo: [20, 705]
CloseSubpath:
dieline fill
MoveTo: [20, 605]
LineTo: [270, 605]
LineTo: [270, 705]
LineTo: [20, 705]
CloseSubpath:

免责声明:我在开发 PDF4NET 库的公司工作。