PDFBox 中的渐变描边
Gradient stroke in PDFBox
我正在尝试使用 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;
}
谁能帮我解决这个问题?
正如拍摄者已经指出的那样,您从阴影中创建了一个 PDColor
:
PDShadingType2 shading = createGradientColor(...);
PDShadingPattern pattern = new PDShadingPattern();
pattern.setShading(shading);
COSName name = page.getResources().add(pattern);
PDColor color = new PDColor(name, new PDPattern(null));
然后你用那个颜色画,例如:
try ( PDPageContentStream canvas = new PDPageContentStream(document, page)) {
canvas.setStrokingColor(color);
canvas.setLineWidth(5);
canvas.moveTo(0, 0);
canvas.lineTo(500, 500);
canvas.curveTo2(500, 250, 250, 250);
canvas.curveTo1(0, 250, 0, 0);
canvas.stroke();
}
假设渐变从红色到绿色,从 0,0 到 500,500,结果如下所示:
(DrawGradient 测试 testDrawWithGradientColor
)
我正在尝试使用 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;
}
谁能帮我解决这个问题?
正如拍摄者已经指出的那样,您从阴影中创建了一个 PDColor
:
PDShadingType2 shading = createGradientColor(...);
PDShadingPattern pattern = new PDShadingPattern();
pattern.setShading(shading);
COSName name = page.getResources().add(pattern);
PDColor color = new PDColor(name, new PDPattern(null));
然后你用那个颜色画,例如:
try ( PDPageContentStream canvas = new PDPageContentStream(document, page)) {
canvas.setStrokingColor(color);
canvas.setLineWidth(5);
canvas.moveTo(0, 0);
canvas.lineTo(500, 500);
canvas.curveTo2(500, 250, 250, 250);
canvas.curveTo1(0, 250, 0, 0);
canvas.stroke();
}
假设渐变从红色到绿色,从 0,0 到 500,500,结果如下所示:
(DrawGradient 测试 testDrawWithGradientColor
)