如何从pdf获取底纹填充值?

时间:2019-04-29 06:00:17

标签: java pdfbox

我正在尝试使用pdfbox从pdf获取正确的颜色和坐标。渐变始终为黑色(请注意我的评论和代码)。 如何手动或使用绘画转换正确获取颜色和坐标?

/**
     * Shape shading fill is processed here
     * @param shadingName
     * @throws IOException 
     */
    @Override
    public void shadingFill(COSName shadingName) throws IOException { 
        PDShading shading = getResources().getShading(shadingName);
        PDAbstractPattern pattern = getResources().getPattern(shadingName);

        PDGraphicsState state = getGraphicsState();
        PDColor nonStrokingColor = state.getNonStrokingColor();
        PDColorSpace colorSpace = nonStrokingColor.getColorSpace();

        GeneralPath path = (GeneralPath)linePath.clone();
        Shape shape = path.createTransformedShape(xform);  

        if (shading == null){
            // found shading!
            PDShadingPattern shadingPattern = (PDShadingPattern)pattern;
            shading = shadingPattern.getShading();            
        }

        // gradient
        if (shading != null){
            PDColorSpace shading_color_space = shading.getColorSpace();
            COSBase a = shading_color_space.getCOSObject();
            float[] b = shading_color_space.getDefaultDecode(1);
            PDColor c = shading_color_space.getInitialColor();


            Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();

            /*
            // experimental
            Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
            AffineTransform imageTransform = ctmNew.createAffineTransform();
            imageTransform.scale(1.0 / shape.getBounds2D().getWidth(), -1.0 / shape.getBounds2D().getHeight());
            imageTransform.translate(0, -shape.getBounds2D().getHeight());
             */

            // TODO: gradients are always black?!
            Paint paint = shading.toPaint(ctm);


            graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
            graphics.setPaint(paint);
            graphics.setClip(null);
            lastClip = null;   

            //testing
            graphics.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0), 1000, 1000, new Color(200, 200, 255)));
            Paint ptest = graphics.getPaint();

            graphics.fill(shape);

        }

        // reset
        linePath.reset(); 
}

0 个答案:

没有答案