Fill
Inheritance: java.lang.Object
public class Fill
Represents fill formatting for an object.
To learn more, visit the Working with Graphic Elements documentation article.
Remarks:
Use the ShapeBase.getFill() or Font.getFill() property to access fill properties of an object. You do not create instances of the Fill class directly.
Examples:
Shows how to fill a shape with a solid color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Write some text, and then cover it with a floating shape.
builder.getFont().setSize(32.0);
builder.writeln("Hello world!");
Shape shape = builder.insertShape(ShapeType.CLOUD_CALLOUT, RelativeHorizontalPosition.LEFT_MARGIN, 25.0,
RelativeVerticalPosition.TOP_MARGIN, 25.0, 250.0, 150.0, WrapType.NONE);
// Use the "StrokeColor" property to set the color of the outline of the shape.
shape.setStrokeColor(Color.BLACK);
// Use the "FillColor" property to set the color of the inside area of the shape.
shape.setFillColor(Color.BLUE);
// The "Opacity" property determines how transparent the color is on a 0-1 scale,
// with 1 being fully opaque, and 0 being invisible.
// The shape fill by default is fully opaque, so we cannot see the text that this shape is on top of.
Assert.assertEquals(1.0d, shape.getFill().getOpacity());
// Set the shape fill color's opacity to a lower value so that we can see the text underneath it.
shape.getFill().setOpacity(0.3);
doc.save(getArtifactsDir() + "Shape.Fill.docx");
Methods
Method | Description |
---|---|
getBackColor() | Gets a Color object that represents the background color for the fill. |
getBackThemeColor() | Gets a ThemeColor object that represents the background color for the fill. |
getBackTintAndShade() | Gets a double value that lightens or darkens the background color. |
getBaseForeColor() | Gets a Color object that represents the base foreground color for the fill without any modifiers. |
getColor() | Gets a Color object that represents the foreground color for the fill. |
getFillType() | Gets a fill type. |
getForeColor() | Gets a Color object that represents the foreground color for the fill. |
getForeThemeColor() | Gets a ThemeColor object that represents the foreground color for the fill. |
getForeTintAndShade() | Gets a double value that lightens or darkens the foreground color. |
getGradientAngle() | Gets the angle of the gradient fill. |
getGradientStops() | Gets a collection of GradientStop objects for the fill. |
getGradientStyle() | Gets the gradient style GradientStyle for the fill. |
getGradientVariant() | Gets the gradient variant GradientVariant for the fill. |
getImageBytes() | Gets the raw bytes of the fill texture or pattern. |
getOpacity() | Gets the degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque). |
getPattern() | Gets a PatternType for the fill. |
getPresetTexture() | Gets a PresetTexture for the fill. |
getRotateWithObject() | Gets whether the fill rotates with the specified object. |
getTextureAlignment() | Gets the alignment for tile texture fill. |
getTransparency() | Gets the degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear). |
getVisible() | Gets value that is true if the formatting applied to this instance, is visible. |
oneColorGradient(int style, int variant, double degree) | |
oneColorGradient(Color color, int style, int variant, double degree) | |
patterned(int patternType) | |
patterned(int patternType, Color foreColor, Color backColor) | |
presetTextured(int presetTexture) | |
setBackColor(Color value) | Sets a Color object that represents the background color for the fill. |
setBackThemeColor(int value) | Sets a ThemeColor object that represents the background color for the fill. |
setBackTintAndShade(double value) | Sets a double value that lightens or darkens the background color. |
setColor(Color value) | Sets a Color object that represents the foreground color for the fill. |
setForeColor(Color value) | Sets a Color object that represents the foreground color for the fill. |
setForeThemeColor(int value) | Sets a ThemeColor object that represents the foreground color for the fill. |
setForeTintAndShade(double value) | Sets a double value that lightens or darkens the foreground color. |
setGradientAngle(double value) | Sets the angle of the gradient fill. |
setImage(byte[] imageBytes) | Changes the fill type to single image. |
setImage(InputStream stream) | |
setImage(String fileName) | Changes the fill type to single image. |
setOpacity(double value) | Sets the degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque). |
setRotateWithObject(boolean value) | Sets whether the fill rotates with the specified object. |
setTextureAlignment(int value) | Sets the alignment for tile texture fill. |
setTransparency(double value) | Sets the degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear). |
setVisible(boolean value) | Sets value that is true if the formatting applied to this instance, is visible. |
solid() | Sets the fill to a uniform color. |
solid(Color color) | Sets the fill to a specified uniform color. |
twoColorGradient(int style, int variant) | |
twoColorGradient(Color color1, Color color2, int style, int variant) |
getBackColor()
public Color getBackColor()
Gets a Color object that represents the background color for the fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Returns: java.awt.Color - A Color object that represents the background color for the fill.
getBackThemeColor()
public int getBackThemeColor()
Gets a ThemeColor object that represents the background color for the fill.
Examples:
Shows how to set theme color for foreground/background shape color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.ROUND_RECTANGLE, 80.0, 80.0);
Fill fill = shape.getFill();
fill.setForeThemeColor(ThemeColor.DARK_1);
fill.setBackThemeColor(ThemeColor.BACKGROUND_2);
// Note: do not use "BackThemeColor" and "BackTintAndShade" for font fill.
if (fill.getBackTintAndShade() == 0)
fill.setBackTintAndShade(0.2);
doc.save(getArtifactsDir() + "Shape.FillThemeColor.docx");
Returns: int - A ThemeColor object that represents the background color for the fill. The returned value is one of ThemeColor constants.
getBackTintAndShade()
public double getBackTintAndShade()
Gets a double value that lightens or darkens the background color.
Returns: double - A double value that lightens or darkens the background color.
getBaseForeColor()
public Color getBaseForeColor()
Gets a Color object that represents the base foreground color for the fill without any modifiers.
Examples:
Shows how to get foreground color without modifiers.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder();
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
shape.getFill().setForeColor(Color.RED);
shape.getFill().setForeTintAndShade(0.5);
shape.getStroke().getFill().setForeColor(Color.GREEN);
shape.getStroke().getFill().setTransparency(0.5);
Assert.assertEquals(new Color((255), (188), (188), (255)).getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getBaseForeColor().getRGB());
Assert.assertEquals(new Color((0), (255), (0), (128)).getRGB(), shape.getStroke().getForeColor().getRGB());
Assert.assertEquals(Color.GREEN.getRGB(), shape.getStroke().getBaseForeColor().getRGB());
Assert.assertEquals(Color.GREEN.getRGB(), shape.getStroke().getFill().getForeColor().getRGB());
Assert.assertEquals(Color.GREEN.getRGB(), shape.getStroke().getFill().getBaseForeColor().getRGB());
Returns: java.awt.Color - A Color object that represents the base foreground color for the fill without any modifiers.
getColor()
public Color getColor()
Gets a Color object that represents the foreground color for the fill.
Remarks:
This property preserves the alpha component of the java.awt.Color, unlike the getForeColor() / setForeColor(java.awt.Color) property, which resets it to fully opaque color.
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
Returns: java.awt.Color - A Color object that represents the foreground color for the fill.
getFillType()
public int getFillType()
Gets a fill type.
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
Returns: int - A fill type. The returned value is one of FillType constants.
getForeColor()
public Color getForeColor()
Gets a Color object that represents the foreground color for the fill.
Remarks:
This property resets the alpha component of the java.awt.Color to fully opaque color unlike the getColor() / setColor(java.awt.Color) property, which preserves it.
Examples:
Shows to create a variety of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are four examples of shapes that we can insert into our documents.
// 1 - Dotted, horizontal, half-transparent red line
// with an arrow on the left end and a diamond on the right end:
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);
// 2 - 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);
// 3 - 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().setForeColor(Color.GREEN);
filledInArrow.getFill().setVisible(true);
builder.insertNode(filledInArrow);
// 4 - Arrow with a flipped orientation filled in with the Aspose logo:
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 flip the orientation of our arrow, we also flip the image that the arrow contains.
// Flip the image the other way to cancel this out before getting the shape to display it.
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);
doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
Returns: java.awt.Color - A Color object that represents the foreground color for the fill.
getForeThemeColor()
public int getForeThemeColor()
Gets a ThemeColor object that represents the foreground color for the fill.
Examples:
Shows how to set theme color for foreground/background shape color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.ROUND_RECTANGLE, 80.0, 80.0);
Fill fill = shape.getFill();
fill.setForeThemeColor(ThemeColor.DARK_1);
fill.setBackThemeColor(ThemeColor.BACKGROUND_2);
// Note: do not use "BackThemeColor" and "BackTintAndShade" for font fill.
if (fill.getBackTintAndShade() == 0)
fill.setBackTintAndShade(0.2);
doc.save(getArtifactsDir() + "Shape.FillThemeColor.docx");
Returns: int - A ThemeColor object that represents the foreground color for the fill. The returned value is one of ThemeColor constants.
getForeTintAndShade()
public double getForeTintAndShade()
Gets a double value that lightens or darkens the foreground color.
Returns: double - A double value that lightens or darkens the foreground color.
getGradientAngle()
public double getGradientAngle()
Gets the angle of the gradient fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Returns: double - The angle of the gradient fill.
getGradientStops()
public GradientStopCollection getGradientStops()
Gets a collection of GradientStop objects for the fill.
Examples:
Shows how to add gradient stops to the gradient fill.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
shape.getFill().twoColorGradient(Color.green, Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2);
// Get gradient stops collection.
GradientStopCollection gradientStops = shape.getFill().getGradientStops();
// Change first gradient stop.
gradientStops.get(0).setColor(Color.yellow);
gradientStops.get(0).setPosition(0.1);
gradientStops.get(0).setTransparency(0.25);
// Add new gradient stop to the end of collection.
GradientStop gradientStop = new GradientStop(Color.blue, 0.5);
gradientStops.add(gradientStop);
// Remove gradient stop at index 1.
gradientStops.removeAt(1);
// And insert new gradient stop at the same index 1.
gradientStops.insert(1, new GradientStop(Color.pink, 0.75, 0.3));
// Remove last gradient stop in the collection.
gradientStop = gradientStops.get(2);
gradientStops.remove(gradientStop);
Assert.assertEquals(2, gradientStops.getCount());
Assert.assertEquals(new Color((255), (255), (0)), gradientStops.get(0).getBaseColor());
Assert.assertEquals(Color.yellow.getRGB(), gradientStops.get(0).getColor().getRGB());
Assert.assertEquals(0.1d, gradientStops.get(0).getPosition(), 0.01d);
Assert.assertEquals(0.25d, gradientStops.get(0).getTransparency(), 0.01d);
Assert.assertEquals(Color.pink.getRGB(), gradientStops.get(1).getColor().getRGB());
Assert.assertEquals(0.75d, gradientStops.get(1).getPosition(), 0.01d);
Assert.assertEquals(0.3d, gradientStops.get(1).getTransparency(), 0.01d);
// Use the compliance option to define the shape using DML
// if you want to get "GradientStops" property after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientStops.docx", saveOptions);
Returns: GradientStopCollection - A collection of GradientStop objects for the fill.
getGradientStyle()
public int getGradientStyle()
Gets the gradient style GradientStyle for the fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Returns: int - The gradient style GradientStyle for the fill. The returned value is one of GradientStyle constants.
getGradientVariant()
public int getGradientVariant()
Gets the gradient variant GradientVariant for the fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Returns: int - The gradient variant GradientVariant for the fill. The returned value is one of GradientVariant constants.
getImageBytes()
public byte[] getImageBytes()
Gets the raw bytes of the fill texture or pattern.
Remarks:
The default value is null .
Examples:
Shows to create a variety of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are four examples of shapes that we can insert into our documents.
// 1 - Dotted, horizontal, half-transparent red line
// with an arrow on the left end and a diamond on the right end:
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);
// 2 - 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);
// 3 - 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().setForeColor(Color.GREEN);
filledInArrow.getFill().setVisible(true);
builder.insertNode(filledInArrow);
// 4 - Arrow with a flipped orientation filled in with the Aspose logo:
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 flip the orientation of our arrow, we also flip the image that the arrow contains.
// Flip the image the other way to cancel this out before getting the shape to display it.
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);
doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
Returns: byte[] - The raw bytes of the fill texture or pattern.
getOpacity()
public double getOpacity()
Gets the degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque).
Remarks:
This property is the opposite of property getTransparency() / setTransparency(double).
Examples:
Shows how to fill a shape with a solid color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Write some text, and then cover it with a floating shape.
builder.getFont().setSize(32.0);
builder.writeln("Hello world!");
Shape shape = builder.insertShape(ShapeType.CLOUD_CALLOUT, RelativeHorizontalPosition.LEFT_MARGIN, 25.0,
RelativeVerticalPosition.TOP_MARGIN, 25.0, 250.0, 150.0, WrapType.NONE);
// Use the "StrokeColor" property to set the color of the outline of the shape.
shape.setStrokeColor(Color.BLACK);
// Use the "FillColor" property to set the color of the inside area of the shape.
shape.setFillColor(Color.BLUE);
// The "Opacity" property determines how transparent the color is on a 0-1 scale,
// with 1 being fully opaque, and 0 being invisible.
// The shape fill by default is fully opaque, so we cannot see the text that this shape is on top of.
Assert.assertEquals(1.0d, shape.getFill().getOpacity());
// Set the shape fill color's opacity to a lower value so that we can see the text underneath it.
shape.getFill().setOpacity(0.3);
doc.save(getArtifactsDir() + "Shape.Fill.docx");
Returns: double - The degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque).
getPattern()
public int getPattern()
Gets a PatternType for the fill.
Examples:
Shows how to set pattern for a shape.
Document doc = new Document(getMyDir() + "Shape stroke pattern border.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
Fill fill = shape.getFill();
System.out.println(MessageFormat.format("Pattern value is: {0}",fill.getPattern()));
// There are several ways specified fill to a pattern.
// 1 - Apply pattern to the shape fill:
fill.patterned(PatternType.DIAGONAL_BRICK);
// 2 - Apply pattern with foreground and background colors to the shape fill:
fill.patterned(PatternType.DIAGONAL_BRICK, Color.yellow, Color.blue);
doc.save(getArtifactsDir() + "Shape.FillPattern.docx");
Returns: int - A PatternType for the fill. The returned value is one of PatternType constants.
getPresetTexture()
public int getPresetTexture()
Gets a PresetTexture for the fill.
Examples:
Shows how to fill and tiling the texture inside the shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply texture alignment to the shape fill.
shape.getFill().presetTextured(PresetTexture.CANVAS);
shape.getFill().setTextureAlignment(TextureAlignment.TOP_RIGHT);
// Use the compliance option to define the shape using DML if you want to get "TextureAlignment"
// property after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.TextureFill.docx", saveOptions);
Returns: int - A PresetTexture for the fill. The returned value is one of PresetTexture constants.
getRotateWithObject()
public boolean getRotateWithObject()
Gets whether the fill rotates with the specified object.
Returns: boolean - Whether the fill rotates with the specified object.
getTextureAlignment()
public int getTextureAlignment()
Gets the alignment for tile texture fill.
Examples:
Shows how to fill and tiling the texture inside the shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply texture alignment to the shape fill.
shape.getFill().presetTextured(PresetTexture.CANVAS);
shape.getFill().setTextureAlignment(TextureAlignment.TOP_RIGHT);
// Use the compliance option to define the shape using DML if you want to get "TextureAlignment"
// property after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.TextureFill.docx", saveOptions);
Returns: int - The alignment for tile texture fill. The returned value is one of TextureAlignment constants.
getTransparency()
public double getTransparency()
Gets the degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear).
Remarks:
This property is the opposite of property getOpacity() / setOpacity(double).
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
Returns: double - The degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear).
getVisible()
public boolean getVisible()
Gets value that is true if the formatting applied to this instance, is visible.
Examples:
Shows to create a variety of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are four examples of shapes that we can insert into our documents.
// 1 - Dotted, horizontal, half-transparent red line
// with an arrow on the left end and a diamond on the right end:
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);
// 2 - 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);
// 3 - 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().setForeColor(Color.GREEN);
filledInArrow.getFill().setVisible(true);
builder.insertNode(filledInArrow);
// 4 - Arrow with a flipped orientation filled in with the Aspose logo:
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 flip the orientation of our arrow, we also flip the image that the arrow contains.
// Flip the image the other way to cancel this out before getting the shape to display it.
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);
doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
Returns: boolean - Value that is true if the formatting applied to this instance, is visible.
oneColorGradient(int style, int variant, double degree)
public void oneColorGradient(int style, int variant, double degree)
Parameters:
Parameter | Type | Description |
---|---|---|
style | int | |
variant | int | |
degree | double |
oneColorGradient(Color color, int style, int variant, double degree)
public void oneColorGradient(Color color, int style, int variant, double degree)
Parameters:
Parameter | Type | Description |
---|---|---|
color | java.awt.Color | |
style | int | |
variant | int | |
degree | double |
patterned(int patternType)
public void patterned(int patternType)
Parameters:
Parameter | Type | Description |
---|---|---|
patternType | int |
patterned(int patternType, Color foreColor, Color backColor)
public void patterned(int patternType, Color foreColor, Color backColor)
Parameters:
Parameter | Type | Description |
---|---|---|
patternType | int | |
foreColor | java.awt.Color | |
backColor | java.awt.Color |
presetTextured(int presetTexture)
public void presetTextured(int presetTexture)
Parameters:
Parameter | Type | Description |
---|---|---|
presetTexture | int |
setBackColor(Color value)
public void setBackColor(Color value)
Sets a Color object that represents the background color for the fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.awt.Color | A Color object that represents the background color for the fill. |
setBackThemeColor(int value)
public void setBackThemeColor(int value)
Sets a ThemeColor object that represents the background color for the fill.
Examples:
Shows how to set theme color for foreground/background shape color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.ROUND_RECTANGLE, 80.0, 80.0);
Fill fill = shape.getFill();
fill.setForeThemeColor(ThemeColor.DARK_1);
fill.setBackThemeColor(ThemeColor.BACKGROUND_2);
// Note: do not use "BackThemeColor" and "BackTintAndShade" for font fill.
if (fill.getBackTintAndShade() == 0)
fill.setBackTintAndShade(0.2);
doc.save(getArtifactsDir() + "Shape.FillThemeColor.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | A ThemeColor object that represents the background color for the fill. The value must be one of ThemeColor constants. |
setBackTintAndShade(double value)
public void setBackTintAndShade(double value)
Sets a double value that lightens or darkens the background color.
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | A double value that lightens or darkens the background color. |
setColor(Color value)
public void setColor(Color value)
Sets a Color object that represents the foreground color for the fill.
Remarks:
This property preserves the alpha component of the java.awt.Color, unlike the getForeColor() / setForeColor(java.awt.Color) property, which resets it to fully opaque color.
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.awt.Color | A Color object that represents the foreground color for the fill. |
setForeColor(Color value)
public void setForeColor(Color value)
Sets a Color object that represents the foreground color for the fill.
Remarks:
This property resets the alpha component of the java.awt.Color to fully opaque color unlike the getColor() / setColor(java.awt.Color) property, which preserves it.
Examples:
Shows to create a variety of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are four examples of shapes that we can insert into our documents.
// 1 - Dotted, horizontal, half-transparent red line
// with an arrow on the left end and a diamond on the right end:
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);
// 2 - 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);
// 3 - 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().setForeColor(Color.GREEN);
filledInArrow.getFill().setVisible(true);
builder.insertNode(filledInArrow);
// 4 - Arrow with a flipped orientation filled in with the Aspose logo:
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 flip the orientation of our arrow, we also flip the image that the arrow contains.
// Flip the image the other way to cancel this out before getting the shape to display it.
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);
doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.awt.Color | A Color object that represents the foreground color for the fill. |
setForeThemeColor(int value)
public void setForeThemeColor(int value)
Sets a ThemeColor object that represents the foreground color for the fill.
Examples:
Shows how to set theme color for foreground/background shape color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.ROUND_RECTANGLE, 80.0, 80.0);
Fill fill = shape.getFill();
fill.setForeThemeColor(ThemeColor.DARK_1);
fill.setBackThemeColor(ThemeColor.BACKGROUND_2);
// Note: do not use "BackThemeColor" and "BackTintAndShade" for font fill.
if (fill.getBackTintAndShade() == 0)
fill.setBackTintAndShade(0.2);
doc.save(getArtifactsDir() + "Shape.FillThemeColor.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | A ThemeColor object that represents the foreground color for the fill. The value must be one of ThemeColor constants. |
setForeTintAndShade(double value)
public void setForeTintAndShade(double value)
Sets a double value that lightens or darkens the foreground color.
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | A double value that lightens or darkens the foreground color. |
setGradientAngle(double value)
public void setGradientAngle(double value)
Sets the angle of the gradient fill.
Examples:
Shows how to fill a shape with a gradients.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply One-color gradient fill to the shape with ForeColor of gradient fill.
shape.getFill().oneColorGradient(Color.RED, GradientStyle.HORIZONTAL, GradientVariant.VARIANT_2, 0.1);
Assert.assertEquals(Color.RED.getRGB(), shape.getFill().getForeColor().getRGB());
Assert.assertEquals(GradientStyle.HORIZONTAL, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_2, shape.getFill().getGradientVariant());
Assert.assertEquals(270, shape.getFill().getGradientAngle());
shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply Two-color gradient fill to the shape.
shape.getFill().twoColorGradient(GradientStyle.FROM_CORNER, GradientVariant.VARIANT_4);
// Change BackColor of gradient fill.
shape.getFill().setBackColor(Color.YELLOW);
// Note that changes "GradientAngle" for "GradientStyle.FromCorner/GradientStyle.FromCenter"
// gradient fill don't get any effect, it will work only for linear gradient.
shape.getFill().setGradientAngle(15.0);
Assert.assertEquals(Color.YELLOW.getRGB(), shape.getFill().getBackColor().getRGB());
Assert.assertEquals(GradientStyle.FROM_CORNER, shape.getFill().getGradientStyle());
Assert.assertEquals(GradientVariant.VARIANT_4, shape.getFill().getGradientVariant());
Assert.assertEquals(0, shape.getFill().getGradientAngle());
// Use the compliance option to define the shape using DML if you want to get "GradientStyle",
// "GradientVariant" and "GradientAngle" properties after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.GradientFill.docx", saveOptions);
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | The angle of the gradient fill. |
setImage(byte[] imageBytes)
public void setImage(byte[] imageBytes)
Changes the fill type to single image.
Examples:
Shows how to set shape fill type as image.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// There are several ways of setting image.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// 1 - Using a local system filename:
shape.getFill().setImage(getImageDir() + "Logo.jpg");
doc.save(getArtifactsDir() + "Shape.FillImage.FileName.docx");
// 2 - Load a file into a byte array:
shape.getFill().setImage(Files.readAllBytes(Paths.get(getImageDir() + "Logo.jpg")));
doc.save(getArtifactsDir() + "Shape.FillImage.ByteArray.docx");
// 3 - From a stream:
FileInputStream stream = new FileInputStream(getImageDir() + "Logo.jpg");
try
{
shape.getFill().setImage(stream);
}
finally { if (stream != null) stream.close(); }
doc.save(getArtifactsDir() + "Shape.FillImage.Stream.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
imageBytes | byte[] | The image bytes array. |
setImage(InputStream stream)
public void setImage(InputStream stream)
Parameters:
Parameter | Type | Description |
---|---|---|
stream | java.io.InputStream |
setImage(String fileName)
public void setImage(String fileName)
Changes the fill type to single image.
Examples:
Shows how to set shape fill type as image.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// There are several ways of setting image.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// 1 - Using a local system filename:
shape.getFill().setImage(getImageDir() + "Logo.jpg");
doc.save(getArtifactsDir() + "Shape.FillImage.FileName.docx");
// 2 - Load a file into a byte array:
shape.getFill().setImage(Files.readAllBytes(Paths.get(getImageDir() + "Logo.jpg")));
doc.save(getArtifactsDir() + "Shape.FillImage.ByteArray.docx");
// 3 - From a stream:
FileInputStream stream = new FileInputStream(getImageDir() + "Logo.jpg");
try
{
shape.getFill().setImage(stream);
}
finally { if (stream != null) stream.close(); }
doc.save(getArtifactsDir() + "Shape.FillImage.Stream.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
fileName | java.lang.String | The path to the image file. |
setOpacity(double value)
public void setOpacity(double value)
Sets the degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque).
Remarks:
This property is the opposite of property getTransparency() / setTransparency(double).
Examples:
Shows how to fill a shape with a solid color.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Write some text, and then cover it with a floating shape.
builder.getFont().setSize(32.0);
builder.writeln("Hello world!");
Shape shape = builder.insertShape(ShapeType.CLOUD_CALLOUT, RelativeHorizontalPosition.LEFT_MARGIN, 25.0,
RelativeVerticalPosition.TOP_MARGIN, 25.0, 250.0, 150.0, WrapType.NONE);
// Use the "StrokeColor" property to set the color of the outline of the shape.
shape.setStrokeColor(Color.BLACK);
// Use the "FillColor" property to set the color of the inside area of the shape.
shape.setFillColor(Color.BLUE);
// The "Opacity" property determines how transparent the color is on a 0-1 scale,
// with 1 being fully opaque, and 0 being invisible.
// The shape fill by default is fully opaque, so we cannot see the text that this shape is on top of.
Assert.assertEquals(1.0d, shape.getFill().getOpacity());
// Set the shape fill color's opacity to a lower value so that we can see the text underneath it.
shape.getFill().setOpacity(0.3);
doc.save(getArtifactsDir() + "Shape.Fill.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | The degree of opacity of the specified fill as a value between 0.0 (clear) and 1.0 (opaque). |
setRotateWithObject(boolean value)
public void setRotateWithObject(boolean value)
Sets whether the fill rotates with the specified object.
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether the fill rotates with the specified object. |
setTextureAlignment(int value)
public void setTextureAlignment(int value)
Sets the alignment for tile texture fill.
Examples:
Shows how to fill and tiling the texture inside the shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 80.0, 80.0);
// Apply texture alignment to the shape fill.
shape.getFill().presetTextured(PresetTexture.CANVAS);
shape.getFill().setTextureAlignment(TextureAlignment.TOP_RIGHT);
// Use the compliance option to define the shape using DML if you want to get "TextureAlignment"
// property after the document saves.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.save(getArtifactsDir() + "Shape.TextureFill.docx", saveOptions);
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The alignment for tile texture fill. The value must be one of TextureAlignment constants. |
setTransparency(double value)
public void setTransparency(double value)
Sets the degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear).
Remarks:
This property is the opposite of property getOpacity() / setOpacity(double).
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | double | The degree of transparency of the specified fill as a value between 0.0 (opaque) and 1.0 (clear). |
setVisible(boolean value)
public void setVisible(boolean value)
Sets value that is true if the formatting applied to this instance, is visible.
Examples:
Shows to create a variety of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are four examples of shapes that we can insert into our documents.
// 1 - Dotted, horizontal, half-transparent red line
// with an arrow on the left end and a diamond on the right end:
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);
// 2 - 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);
// 3 - 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().setForeColor(Color.GREEN);
filledInArrow.getFill().setVisible(true);
builder.insertNode(filledInArrow);
// 4 - Arrow with a flipped orientation filled in with the Aspose logo:
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 flip the orientation of our arrow, we also flip the image that the arrow contains.
// Flip the image the other way to cancel this out before getting the shape to display it.
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);
doc.save(getArtifactsDir() + "Drawing.VariousShapes.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Value that is true if the formatting applied to this instance, is visible. |
solid()
public void solid()
Sets the fill to a uniform color.
Remarks:
Use this method to convert any of the fills back to solid fill.
Examples:
Shows how to convert any of the fills back to solid fill.
Document doc = new Document(getMyDir() + "Two color gradient.docx");
// Get Fill object for Font of the first Run.
Fill fill = doc.getFirstSection().getBody().getParagraphs().get(0).getRuns().get(0).getFont().getFill();
// Check Fill properties of the Font.
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill is transparent at {0}%",fill.getTransparency() * 100.0));
// Change type of the fill to Solid with uniform green color.
fill.solid(Color.GREEN);
System.out.println("\nThe fill is changed:");
System.out.println(MessageFormat.format("The type of the fill is: {0}",fill.getFillType()));
System.out.println(MessageFormat.format("The foreground color of the fill is: {0}",fill.getForeColor()));
System.out.println(MessageFormat.format("The fill transparency is {0}%",fill.getTransparency() * 100.0));
doc.save(getArtifactsDir() + "Drawing.FillSolid.docx");
solid(Color color)
public void solid(Color color)
Sets the fill to a specified uniform color.
Remarks:
Use this method to convert any of the fills back to solid fill.
Examples:
Shows how to use chart formating.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);
Chart chart = shape.getChart();
// Delete series generated by default.
ChartSeriesCollection series = chart.getSeries();
series.clear();
String[] categories = new String[] { "Category 1", "Category 2" };
series.add("Series 1", categories, new double[] { 1.0, 2.0 });
series.add("Series 2", categories, new double[] { 3.0, 4.0 });
// Format chart background.
chart.getFormat().getFill().solid(Color.darkGray);
// Hide axis tick labels.
chart.getAxisX().getTickLabels().setPosition(AxisTickLabelPosition.NONE);
chart.getAxisY().getTickLabels().setPosition(AxisTickLabelPosition.NONE);
// Format chart title.
chart.getTitle().getFormat().getFill().solid(Color.yellow);
// Format axis title.
chart.getAxisX().getTitle().setShow(true);
chart.getAxisX().getTitle().getFormat().getFill().solid(Color.yellow);
// Format legend.
chart.getLegend().getFormat().getFill().solid(Color.yellow);
doc.save(getArtifactsDir() + "Charts.ChartFormat.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
color | java.awt.Color |
twoColorGradient(int style, int variant)
public void twoColorGradient(int style, int variant)
Parameters:
Parameter | Type | Description |
---|---|---|
style | int | |
variant | int |
twoColorGradient(Color color1, Color color2, int style, int variant)
public void twoColorGradient(Color color1, Color color2, int style, int variant)
Parameters:
Parameter | Type | Description |
---|---|---|
color1 | java.awt.Color | |
color2 | java.awt.Color | |
style | int | |
variant | int |