pdfbox中的渐变笔划

t3psigkw  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(306)

我正在尝试使用java pdfbox绘制渐变笔划颜色而非填充颜色的曲线,有人可以帮助我如何做到这一点吗?
对于填充我正在使用的形状:

contentStream.clip();
contentStream.shadingFill(PdfUtils.createGradientColor(gradientFactors));

public static PDShadingType2 createGradientColor(GradientFactors gradientFactors) throws IOException {
    Color startColor = gradientFactors.getStartColor().getColor();
    Color endColor = gradientFactors.getEndColor().getColor();

    COSDictionary fdict = new COSDictionary();

    fdict.setInt(COSName.FUNCTION_TYPE, 2);

    COSArray domain = new COSArray();
    domain.add(COSInteger.get(0));
    domain.add(COSInteger.get(1));

    COSArray c0 = new COSArray();
    c0.add(new COSFloat(startColor.getRed() / 255f));
    c0.add(new COSFloat(startColor.getGreen() / 255f));
    c0.add(new COSFloat(startColor.getBlue() / 255f));

    COSArray c1 = new COSArray();
    c1.add(new COSFloat(endColor.getRed() / 255f));
    c1.add(new COSFloat(endColor.getGreen() / 255f));
    c1.add(new COSFloat(endColor.getBlue() / 255f));

    fdict.setItem(COSName.DOMAIN, domain);
    fdict.setItem(COSName.C0, c0);
    fdict.setItem(COSName.C1, c1);
    fdict.setInt(COSName.N, 1);

    PDFunctionType2 func = new PDFunctionType2(fdict);

    PDShadingType2 axialShading = new PDShadingType2(new COSDictionary());

    axialShading.setColorSpace(PDDeviceRGB.INSTANCE);
    axialShading.setShadingType(PDShading.SHADING_TYPE2);

    COSArray coords1 = new COSArray();
    coords1.add(new COSFloat(gradientFactors.getX1()));
    coords1.add(new COSFloat(gradientFactors.getY1()));
    coords1.add(new COSFloat(gradientFactors.getX2()));
    coords1.add(new COSFloat(gradientFactors.getY2()));

    axialShading.setCoords(coords1);
    axialShading.setFunction(func);

    return axialShading;
}

谁能帮我一下吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题