[]
        
(Showing Draft Content)

Manage Shape Options

In DsExcel, you can customize the shape format in three different ways. This includes setting the fill format for the inserted shape using the properties and methods of the IFillFormat interface, configuring the shape's line using the properties and methods of the ILineFormat interface and applying 3D formatting to the shape using the properties and methods of the IThreeDFormat interface.

Solid Fill

To format the shape with Solid fill, first you need to use the Solid method of the IFillFormat interface to specify the fill format and then set the setRGB and setTransparency to set the shape's fill color and transparency degree respectively.

Refer to the following example code to fill the shape with solid fill.

// Solid fill
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Parallelogram, 1, 1, 200, 100);
shape.getFill().solid();
shape.getFill().getColor().setRGB(Color.GetRed());

Gradient Fill

With gradient fill, you can configure the shape fill to the gradient fill using the oneColorGradient method, twoColorGradient method or presetGradient method of the IFillFormat interface.

After setting the gradient fill, you can insert, delete or change gradient stops; configure the fill style rotation along with the shape and the angle of the gradient fill via the getGradientStops method, setRotateWithObject method and setGradientAngle method of the IFillFormat interface.

Four types of gradient fills, namely line, radial, rectangular and path are supported by DsExcel. By default, the 'Line' gradient fill is applied.

Refer to the following example code to fill the shape with gradient fill using presetGradient method.

// Gradient fill
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Heart, 1, 1, 100, 100);
shape.getFill().presetGradient(GradientStyle.Vertical, 3, PresetGradientType.Silver);
shape.getFill().setRotateWithObject(false);

Refer to the following example code to fill the shape with gradient fill using twoColorGradient method.

// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);

// Add a shape
IShape rectangle = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 20, 20, 300, 100);

// Init a two color gradient fill.          
rectangle.getFill().twoColorGradient(GradientStyle.Horizontal, 1);   

//save to an excel file
workbook.save("LineGradient.xlsx");

To set the radial, rectangular or path gradient fill, you also need to set the PathShapeType along with using the twoColorGradient method.

Refer to the following example code to fill the shape with 'Radial' gradient fill.

// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);

// Add a shape
IShape rectangle = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 20, 20, 300, 100);

// Init a two color gradient fill.          
rectangle.getFill().twoColorGradient(GradientStyle.FromCenter, 1);
       
rectangle.getFill().getGradientPathType().equals(PathShapeType.Radial);

//save to an excel file
workbook.save("RadialGradient.xlsx");

Pattern Fill

With pattern fill, you can set the shape fill to pattern fill using the patterned method of the IFillFormat interface.

Further, you can also configure the background color and the pattern color using setObjectThemeColor method of the IColorFormat interface and getPatternColor method of the IFillFormat interface.

In order to fill the shape with pattern fill, refer to the following example code.

// Pattern fill
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 1, 1, 100, 100);
shape.getFill().patterned(PatternType.Percent10);
shape.getFill().getColor().setObjectThemeColor(ThemeColor.Accent2);
shape.getFill().getPatternColor().setObjectThemeColor(ThemeColor.Accent6);

Picture Fill

In picture fill, you can use the addShape method of the IShapes interface to insert the shape that you want to fill with a picture.

Also, you can configure the picture format with characteristics like picture height, picture width, brightness, contrast ratio, re-coloring, x-axis and y-axis offset etc using the methods of the IPictureFormat interface.

In order to fill the shape with picture, refer to the following example code.

// Add shape of picture type
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 20, 20, 100, 100);
String path = "C:\\Users\\GPCTAdmin\\Pictures\\cat.jpg";

try {
FileInputStream stream = new FileInputStream(path);
shape.getFill().userPicture(stream, ImageType.JPG);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
// Recolor the picture
shape.getPictureFormat().setColorType(PictureColorType.Grayscale);

// Set picture brightness and contrast ratio
shape.getPictureFormat().setBrightness(0.6);
shape.getPictureFormat().setContrast(0.3);

// Set height, width, x-axis offset and y-axis offset of the specified picture
shape.getPictureFormat().getCrop().setPictureOffsetX(10);
shape.getPictureFormat().getCrop().setPictureOffsetY(-5);
shape.getPictureFormat().getCrop().setPictureWidth(120);
shape.getPictureFormat().getCrop().setPictureHeight(80);

Texture Fill

Using texture fill, you can fill the shape with texture of your choice using the presetTextured method of the IFillFormat interface.

Further, you can also configure the layout of the texture using the setTextureAlignment method, setTextureHorizontalScale method, setTextureOffsetX method, setTextureOffsetY method and setTextureVerticalScale method.

In order to fill the shape with texture fill, refer to the following example code.

// Texture Fill
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 1, 1, 100, 100);
shape.getFill().presetTextured(PresetTexture.Canvas);
shape.getFill().setTextureAlignment(TextureAlignment.Center);
shape.getFill().setTextureOffsetX(2.5);
shape.getFill().setTextureOffsetY(3.2);
shape.getFill().setTextureHorizontalScale(0.9);
shape.getFill().setTextureVerticalScale(0.2);
shape.getFill().setTransparency(0.5);

Line

Line is a kind of border around the shape. You can create lines around shapes inserted on cells of a spreadsheet using the properties and methods of ILineFormat interface.

Refer to the following example code to configure the line and line style for the shape.

// To set shape's line style.
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 1, 1, 100, 100);
shape.getLine().setDashStyle(LineDashStyle.Dash);
shape.getLine().setStyle(LineStyle.Single);
shape.getLine().setWeight(2);
shape.getLine().getColor().setObjectThemeColor(ThemeColor.Accent6);
shape.getLine().setTransparency(0.3);

Note: Shape's Line also supports solid fill, gradient fill and pattern fill and its usage is similar to the Shape Fill.

3D Formatting

DsExcel Java enables users to format the three-dimensional layout for the inserted shape via configuring its rotation degree around x, y and z axis. This can be done using the setRotationX method, the setRotationY method and the setRotationZ method of the IThreeDFormat interface.

In order to apply 3D formatting to the embedded shape, refer to the following example code.

// To set rotation degree for the shape arround x, y, z axis.
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 1, 1, 100, 100);
shape.getThreeD().setRotationX(50);
shape.getThreeD().setRotationY(20);
shape.getThreeD().setRotationZ(30);
shape.getThreeD().setDepth(7);
shape.getThreeD().setZ(20);