How to add a shading pattern to a custom shape

前端 未结 1 905
长情又很酷
长情又很酷 2021-01-28 02:21

I have drawn an equilateral triangle as follows using iText

canvas.setColorStroke(BaseColor.BLACK);
int x = start.getX();
int y = start.getY();
canvas.moveTo(x,y         


        
相关标签:
1条回答
  • 2021-01-28 03:01

    I've created an example called ShadedFill that fills the triangle you are drawing using a shading pattern that goes from pink to blue as show in the shaded_fill.pdf:

    enter image description here

    PdfContentByte canvas = writer.getDirectContent();
    float x = 36;
    float y = 740;
    float side = 70;
    PdfShading axial = PdfShading.simpleAxial(writer, x, y,
            x + side, y, BaseColor.PINK, BaseColor.BLUE);
    PdfShadingPattern shading = new PdfShadingPattern(axial);
    canvas.setShadingFill(shading);
    canvas.moveTo(x,y);        
    canvas.lineTo(x + side, y);
    canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
    canvas.closePathFillStroke();
    

    As you can see, you need to create a PdfShading object. I created an axial shading that varies from pink to blue from the coordinate (x, y) to the coordinate (x + side, y). With this axial shading, you can create a PdfShadingPattern that can be used as a parameter of the setShadingFill() method to set the fill color for the canvas.

    See ShadedFill for the full source code.

    0 讨论(0)
提交回复
热议问题