PDFBOX:渐变不填充特定形状
PDFBOX: Gradient not fill only the specific shape
我正在尝试使用渐变色填充绘制的形状,但渐变色填充了整个区域,附上我的代码:
创建渐变颜色的代码:
public static PDColor createSpotColorFromRGB(String spotColorName, int r, int g, int b) throws IOException {
PDSeparation seperation = new PDSeparation();
COSDictionary dictionary = new COSDictionary();
COSArray C0 = new COSArray();
COSArray C1 = new COSArray();
COSArray range = new COSArray();
COSArray domain = new COSArray();
float[] components = null;
seperation.setColorantName(spotColorName);
components = new float[] { r / 255f, g / 255f, b / 255f };
seperation.setAlternateColorSpace(PDDeviceRGB.INSTANCE);
C0.add(COSInteger.ZERO);
C0.add(COSInteger.ZERO);
C0.add(COSInteger.ZERO);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
for (int i = 0; i < components.length; i++) {
C1.add(new COSFloat(components[i]));
}
domain.add(COSInteger.ZERO);
domain.add(COSInteger.ONE);
dictionary.setItem(COSName.C1, C1);
dictionary.setItem(COSName.C0, C0);
dictionary.setItem(COSName.DOMAIN, domain);
dictionary.setItem(COSName.FUNCTION_TYPE, COSInteger.TWO);
dictionary.setItem(COSName.N, COSInteger.ONE);
dictionary.setItem(COSName.RANGE, range);
PDFunction functionTyp2 = new PDFunctionType2(dictionary);
seperation.setTintTransform(functionTyp2);
return new PDColor(new float[] { 1f }, seperation);
}
绘制矩形的代码:
contentStream.saveGraphicsState();
extendedGraphicsStateForDrawing = new PDExtendedGraphicsState();
extendedGraphicsStateForDrawing.setAlphaSourceFlag(true);
extendedGraphicsStateForDrawing.setStrokingAlphaConstant(alphaStroke);
extendedGraphicsStateForDrawing.setNonStrokingAlphaConstant(alphaFill);
contentStream.setGraphicsStateParameters(extendedGraphicsStateForDrawing);
contentStream.moveTo(5, 5);
contentStream.lineTo(10,5);
contentStream.lineTo(10,10);
contentStream.lineTo(10,5);
contentStream.closePath();
contentStream.shadingFill(PdfUtils.createGradientColor(gradiantFactors));
contentStream.restoreGraphicsState();
为什么所有页面都用渐变填充,而不仅仅是矩形:
谢谢。
通话
contentStream.clip();
在做阴影之前。阴影填充不像正常填充那样工作。
我正在尝试使用渐变色填充绘制的形状,但渐变色填充了整个区域,附上我的代码:
创建渐变颜色的代码:
public static PDColor createSpotColorFromRGB(String spotColorName, int r, int g, int b) throws IOException {
PDSeparation seperation = new PDSeparation();
COSDictionary dictionary = new COSDictionary();
COSArray C0 = new COSArray();
COSArray C1 = new COSArray();
COSArray range = new COSArray();
COSArray domain = new COSArray();
float[] components = null;
seperation.setColorantName(spotColorName);
components = new float[] { r / 255f, g / 255f, b / 255f };
seperation.setAlternateColorSpace(PDDeviceRGB.INSTANCE);
C0.add(COSInteger.ZERO);
C0.add(COSInteger.ZERO);
C0.add(COSInteger.ZERO);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
range.add(COSInteger.ZERO);
range.add(COSInteger.ONE);
for (int i = 0; i < components.length; i++) {
C1.add(new COSFloat(components[i]));
}
domain.add(COSInteger.ZERO);
domain.add(COSInteger.ONE);
dictionary.setItem(COSName.C1, C1);
dictionary.setItem(COSName.C0, C0);
dictionary.setItem(COSName.DOMAIN, domain);
dictionary.setItem(COSName.FUNCTION_TYPE, COSInteger.TWO);
dictionary.setItem(COSName.N, COSInteger.ONE);
dictionary.setItem(COSName.RANGE, range);
PDFunction functionTyp2 = new PDFunctionType2(dictionary);
seperation.setTintTransform(functionTyp2);
return new PDColor(new float[] { 1f }, seperation);
}
绘制矩形的代码:
contentStream.saveGraphicsState();
extendedGraphicsStateForDrawing = new PDExtendedGraphicsState();
extendedGraphicsStateForDrawing.setAlphaSourceFlag(true);
extendedGraphicsStateForDrawing.setStrokingAlphaConstant(alphaStroke);
extendedGraphicsStateForDrawing.setNonStrokingAlphaConstant(alphaFill);
contentStream.setGraphicsStateParameters(extendedGraphicsStateForDrawing);
contentStream.moveTo(5, 5);
contentStream.lineTo(10,5);
contentStream.lineTo(10,10);
contentStream.lineTo(10,5);
contentStream.closePath();
contentStream.shadingFill(PdfUtils.createGradientColor(gradiantFactors));
contentStream.restoreGraphicsState();
为什么所有页面都用渐变填充,而不仅仅是矩形:
谢谢。
通话
contentStream.clip();
在做阴影之前。阴影填充不像正常填充那样工作。