com.aspose.words

  • java.lang.Object
    • com.aspose.words.Fill
public class Fill 
extends java.lang.Object

Defines a fill for a shape.

Use the Shape.Fill property to access fill properties of a shape. You do not create instances of the Fill class directly.

Although the Fill class provides properties to specify solid color fill only, all of the more complex fill types, including as gradient, pattern and texture are fully preserved during document open-save cycles.

Example:

Demonstrates how to create shapes with fill.
DocumentBuilder builder = new DocumentBuilder();

builder.writeln();
builder.writeln();
builder.writeln();
builder.write("Some text under the shape.");

// Create a red balloon, semitransparent.
// The shape is floating and its coordinates are (0,0) by default, relative to the current paragraph.
Shape shape = new Shape(builder.getDocument(), ShapeType.BALLOON);
shape.setFillColor(Color.RED);
shape.getFill().setOpacity(0.3);
shape.setWidth(100);
shape.setHeight(100);
shape.setTop(-100);
builder.insertNode(shape);

builder.getDocument().save(getArtifactsDir() + "Shape.Fill.doc");
See Also:
Shape.Fill

Property Getters/Setters Summary
java.awt.ColorgetColor()
void
setColor(java.awt.Colorvalue)
           Defines the color of a fill.
byte[]getImageBytes()
Gets the raw bytes of the fill texture or pattern.
booleangetOn()
void
setOn(booleanvalue)
           Determines whether the shape will be filled.
doublegetOpacity()
void
setOpacity(doublevalue)
           Defines the transparency of a fill. Valid range from 0 to 1, where 0 is fully transparent and 1 is fully opaque.
 

    • Property Getters/Setters Detail

      • getColor/setColor

        public java.awt.Color getColor() / public void setColor(java.awt.Color value)
        
        Defines the color of a fill.

        The default value is java.awt.Color.WHITE.

        Example:

        Shows to create a variety of shapes.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Draw a dotted horizontal half-transparent red line with an arrow on the left end and a diamond on the other
        Shape arrow = new Shape(doc, ShapeType.LINE);
        arrow.setWidth(200.0);
        arrow.getStroke().setColor(Color.RED);
        arrow.getStroke().setStartArrowType(ArrowType.ARROW);
        arrow.getStroke().setStartArrowLength(ArrowLength.LONG);
        arrow.getStroke().setStartArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setEndArrowType(ArrowType.DIAMOND);
        arrow.getStroke().setEndArrowLength(ArrowLength.LONG);
        arrow.getStroke().setEndArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setDashStyle(DashStyle.DASH);
        arrow.getStroke().setOpacity(0.5);
        
        Assert.assertEquals(arrow.getStroke().getJoinStyle(), JoinStyle.MITER);
        
        builder.insertNode(arrow);
        
        // Draw a thick black diagonal line with rounded ends
        Shape line = new Shape(doc, ShapeType.LINE);
        line.setTop(40.0);
        line.setWidth(200.0);
        line.setHeight(20.0);
        line.setStrokeWeight(5.0);
        line.getStroke().setEndCap(EndCap.ROUND);
        
        builder.insertNode(line);
        
        // Draw an arrow with a green fill
        Shape filledInArrow = new Shape(doc, ShapeType.ARROW);
        filledInArrow.setWidth(200.0);
        filledInArrow.setHeight(40.0);
        filledInArrow.setTop(100.0);
        filledInArrow.getFill().setColor(Color.GREEN);
        filledInArrow.getFill().setOn(true);
        
        builder.insertNode(filledInArrow);
        
        // Draw an arrow filled in with the Aspose logo and flip its orientation
        Shape filledInArrowImg = new Shape(doc, ShapeType.ARROW);
        filledInArrowImg.setWidth(200.0);
        filledInArrowImg.setHeight(40.0);
        filledInArrowImg.setTop(160.0);
        filledInArrowImg.setFlipOrientation(FlipOrientation.BOTH);
        
        BufferedImage image = ImageIO.read(getAsposelogoUri().toURL().openStream());
        Graphics2D graphics2D = image.createGraphics();
        
        // When we flipped the orientation of our arrow, the image content was flipped too
        // If we want it to be displayed the right side up, we have to reverse the arrow flip on the image
        AffineTransform at = new AffineTransform();
        at.concatenate(AffineTransform.getScaleInstance(1, -1));
        at.concatenate(AffineTransform.getTranslateInstance(0, -image.getHeight()));
        graphics2D.transform(at);
        graphics2D.drawImage(image, 0, 0, null);
        graphics2D.dispose();
        
        filledInArrowImg.getImageData().setImage(image);
        builder.insertNode(filledInArrowImg);
        
        filledInArrowImg.getStroke().setJoinStyle(JoinStyle.ROUND);
        
        doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
      • getImageBytes

        public byte[] getImageBytes()
        
        Gets the raw bytes of the fill texture or pattern.

        The default value is null.

        Example:

        Shows to create a variety of shapes.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Draw a dotted horizontal half-transparent red line with an arrow on the left end and a diamond on the other
        Shape arrow = new Shape(doc, ShapeType.LINE);
        arrow.setWidth(200.0);
        arrow.getStroke().setColor(Color.RED);
        arrow.getStroke().setStartArrowType(ArrowType.ARROW);
        arrow.getStroke().setStartArrowLength(ArrowLength.LONG);
        arrow.getStroke().setStartArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setEndArrowType(ArrowType.DIAMOND);
        arrow.getStroke().setEndArrowLength(ArrowLength.LONG);
        arrow.getStroke().setEndArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setDashStyle(DashStyle.DASH);
        arrow.getStroke().setOpacity(0.5);
        
        Assert.assertEquals(arrow.getStroke().getJoinStyle(), JoinStyle.MITER);
        
        builder.insertNode(arrow);
        
        // Draw a thick black diagonal line with rounded ends
        Shape line = new Shape(doc, ShapeType.LINE);
        line.setTop(40.0);
        line.setWidth(200.0);
        line.setHeight(20.0);
        line.setStrokeWeight(5.0);
        line.getStroke().setEndCap(EndCap.ROUND);
        
        builder.insertNode(line);
        
        // Draw an arrow with a green fill
        Shape filledInArrow = new Shape(doc, ShapeType.ARROW);
        filledInArrow.setWidth(200.0);
        filledInArrow.setHeight(40.0);
        filledInArrow.setTop(100.0);
        filledInArrow.getFill().setColor(Color.GREEN);
        filledInArrow.getFill().setOn(true);
        
        builder.insertNode(filledInArrow);
        
        // Draw an arrow filled in with the Aspose logo and flip its orientation
        Shape filledInArrowImg = new Shape(doc, ShapeType.ARROW);
        filledInArrowImg.setWidth(200.0);
        filledInArrowImg.setHeight(40.0);
        filledInArrowImg.setTop(160.0);
        filledInArrowImg.setFlipOrientation(FlipOrientation.BOTH);
        
        BufferedImage image = ImageIO.read(getAsposelogoUri().toURL().openStream());
        Graphics2D graphics2D = image.createGraphics();
        
        // When we flipped the orientation of our arrow, the image content was flipped too
        // If we want it to be displayed the right side up, we have to reverse the arrow flip on the image
        AffineTransform at = new AffineTransform();
        at.concatenate(AffineTransform.getScaleInstance(1, -1));
        at.concatenate(AffineTransform.getTranslateInstance(0, -image.getHeight()));
        graphics2D.transform(at);
        graphics2D.drawImage(image, 0, 0, null);
        graphics2D.dispose();
        
        filledInArrowImg.getImageData().setImage(image);
        builder.insertNode(filledInArrowImg);
        
        filledInArrowImg.getStroke().setJoinStyle(JoinStyle.ROUND);
        
        doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
      • getOn/setOn

        public boolean getOn() / public void setOn(boolean value)
        
        Determines whether the shape will be filled.

        The default value is true.

        Example:

        Shows to create a variety of shapes.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Draw a dotted horizontal half-transparent red line with an arrow on the left end and a diamond on the other
        Shape arrow = new Shape(doc, ShapeType.LINE);
        arrow.setWidth(200.0);
        arrow.getStroke().setColor(Color.RED);
        arrow.getStroke().setStartArrowType(ArrowType.ARROW);
        arrow.getStroke().setStartArrowLength(ArrowLength.LONG);
        arrow.getStroke().setStartArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setEndArrowType(ArrowType.DIAMOND);
        arrow.getStroke().setEndArrowLength(ArrowLength.LONG);
        arrow.getStroke().setEndArrowWidth(ArrowWidth.WIDE);
        arrow.getStroke().setDashStyle(DashStyle.DASH);
        arrow.getStroke().setOpacity(0.5);
        
        Assert.assertEquals(arrow.getStroke().getJoinStyle(), JoinStyle.MITER);
        
        builder.insertNode(arrow);
        
        // Draw a thick black diagonal line with rounded ends
        Shape line = new Shape(doc, ShapeType.LINE);
        line.setTop(40.0);
        line.setWidth(200.0);
        line.setHeight(20.0);
        line.setStrokeWeight(5.0);
        line.getStroke().setEndCap(EndCap.ROUND);
        
        builder.insertNode(line);
        
        // Draw an arrow with a green fill
        Shape filledInArrow = new Shape(doc, ShapeType.ARROW);
        filledInArrow.setWidth(200.0);
        filledInArrow.setHeight(40.0);
        filledInArrow.setTop(100.0);
        filledInArrow.getFill().setColor(Color.GREEN);
        filledInArrow.getFill().setOn(true);
        
        builder.insertNode(filledInArrow);
        
        // Draw an arrow filled in with the Aspose logo and flip its orientation
        Shape filledInArrowImg = new Shape(doc, ShapeType.ARROW);
        filledInArrowImg.setWidth(200.0);
        filledInArrowImg.setHeight(40.0);
        filledInArrowImg.setTop(160.0);
        filledInArrowImg.setFlipOrientation(FlipOrientation.BOTH);
        
        BufferedImage image = ImageIO.read(getAsposelogoUri().toURL().openStream());
        Graphics2D graphics2D = image.createGraphics();
        
        // When we flipped the orientation of our arrow, the image content was flipped too
        // If we want it to be displayed the right side up, we have to reverse the arrow flip on the image
        AffineTransform at = new AffineTransform();
        at.concatenate(AffineTransform.getScaleInstance(1, -1));
        at.concatenate(AffineTransform.getTranslateInstance(0, -image.getHeight()));
        graphics2D.transform(at);
        graphics2D.drawImage(image, 0, 0, null);
        graphics2D.dispose();
        
        filledInArrowImg.getImageData().setImage(image);
        builder.insertNode(filledInArrowImg);
        
        filledInArrowImg.getStroke().setJoinStyle(JoinStyle.ROUND);
        
        doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
      • getOpacity/setOpacity

        public double getOpacity() / public void setOpacity(double value)
        
        Defines the transparency of a fill. Valid range from 0 to 1, where 0 is fully transparent and 1 is fully opaque.

        The default value is 1.

        Example:

        Demonstrates how to create shapes with fill.
        DocumentBuilder builder = new DocumentBuilder();
        
        builder.writeln();
        builder.writeln();
        builder.writeln();
        builder.write("Some text under the shape.");
        
        // Create a red balloon, semitransparent.
        // The shape is floating and its coordinates are (0,0) by default, relative to the current paragraph.
        Shape shape = new Shape(builder.getDocument(), ShapeType.BALLOON);
        shape.setFillColor(Color.RED);
        shape.getFill().setOpacity(0.3);
        shape.setWidth(100);
        shape.setHeight(100);
        shape.setTop(-100);
        builder.insertNode(shape);
        
        builder.getDocument().save(getArtifactsDir() + "Shape.Fill.doc");