Working with Shapes

Shapes in Aspose.Words

This topic discusses how to work programmatically with shapes using Aspose.Words. The shapes in Aspose.Words represent an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture. A Word document can contain one or more different shapes. Shapes of the document are represented by the Shape class.

Insert Shape Using Document Builder

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//Free-floating shape insertion.
Shape shape = builder.insertShape(ShapeType.TEXT_BOX,
RelativeHorizontalPosition.PAGE, 100,
RelativeVerticalPosition.PAGE, 100,
50, 50,
WrapType.NONE);
shape.setRotation(30.0);
builder.writeln();
//Inline shape insertion.
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50);
shape.setRotation(30.0);
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX);
// "Strict" or "Transitional" compliance allows to save shape as DML.
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx";
// Save the document to disk.
doc.save(dataDir, so);

Set Aspect Ratio Locked

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(true);
// Save the document to disk.
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc";
doc.save(dataDir);

Set Shape Layout In Cell

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "LayoutInCell.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell.
watermark.setWidth(300);
watermark.setHeight(70);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setRotation(-40);
watermark.getFill().setColor(Color.GRAY);
watermark.setStrokeColor(Color.GRAY);
watermark.getTextPath().setText("watermarkText");
watermark.getTextPath().setFontFamily("Arial");
watermark.setName("WaterMark_0");
watermark.setWrapType(WrapType.NONE);
Run run = (Run) doc.getChildNodes(NodeType.RUN, true).get(doc.getChildNodes(NodeType.RUN, true).getCount() - 1);
builder.moveTo(run);
builder.insertNode(watermark);
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010);
// Save the document to disk.
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx";
doc.save(dataDir);

Add Corners Snipped

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50);
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX);
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
dataDir = dataDir + "AddCornersSnipped_out.docx";
//Save the document to disk.
doc.save(dataDir, so);

Get Actual Shape Bounds Points

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(false);
System.out.print("\nGets the actual bounds of the shape in points. ");
System.out.println(shape.getShapeRenderer().getBoundsInPoints());

Specify Vertical Anchor

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "VerticalAnchor.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 0;
for (Shape textBoxShape : (Iterable<Shape>) shapes) {
if (textBoxShape != null) {
textBoxShape.getTextBox().setVerticalAnchor(TextBoxAnchor.BOTTOM);
}
}
doc.save(dataDir + "VerticalAnchor_out.docx");

Detect SmartArt Shape

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "input.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
int count = 0;
for (Shape textBoxShape : (Iterable<Shape>) shapes) {
if (textBoxShape.hasSmartArt()) {
count++;
}
}
System.out.println("The document has " + count + " shapes with SmartArt.");

Horizontal Rule Format

Aspose.Words API provides Shape.HorizontalRuleFormat property to access the properties of the horizontal rule shape. The HorizontalRuleFormat class exposes basic properties like Height, Color, NoShade etc. for the formatting of a horizontal rule. The following code example demonstrates how to set HorizontalRuleFormat.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
DocumentBuilder builder = new DocumentBuilder();
Shape shape = builder.insertHorizontalRule();
HorizontalRuleFormat horizontalRuleFormat = shape.getHorizontalRuleFormat();
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER);
horizontalRuleFormat.setWidthPercent(70);
horizontalRuleFormat.setHeight(3);
horizontalRuleFormat.setColor(Color.BLUE);
horizontalRuleFormat.setNoShade(true);
builder.getDocument().save("HorizontalRuleFormat.docx");

Insert OLE Object as an Icon

Aspose.Words API provides Shape.InsertOleObjectAsIcon function to insert an embedded or linked OLE object as an icon into the document. This function allows specifying the icon file and the caption. The OLE object type shall be detected using the file extension. The following code example demonstrates how to set insert OLE object as an Icon into the document.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObjectAsIcon(getMyDir() + "Presentation.pptx", false, getImagesDir() + "Logo icon.ico",
"My embedded file");
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIcon.docx");