public abstract class ShapeBase extends CompositeNode
To learn more, visit the Working with Shapes documentation article.
Remarks:
This is an abstract class. The two derived classes that you can instantiate are Shape and GroupShape.
A shape is a node in the document tree.
If the shape is a child of a Paragraph object, then the shape is said to be "top-level". Top-level shapes are measured and positioned in points.
A shape can also occur as a child of a GroupShape object when several shapes are grouped. Child shapes of a group shape are positioned in the coordinate space and units defined by the getCoordSize() / setCoordSize(java.awt.Dimension) and getCoordOrigin() / setCoordOrigin(java.awt.Point) properties of the parent group shape.
A shape can be positioned inline with text or floating. The positioning method is controlled using the getWrapType() / setWrapType(int) property.
When a shape is floating, it is positioned relative to something (e.g the current paragraph, the margin or the page). The relative positioning of the shape is specified using the getRelativeHorizontalPosition() / setRelativeHorizontalPosition(int) and getRelativeVerticalPosition() / setRelativeVerticalPosition(int) properties.
A floating shape be positioned explicitly using the getLeft() / setLeft(double) and getTop() / setTop(double) properties or aligned relative to some other object using the getHorizontalAlignment() / setHorizontalAlignment(int) and getVerticalAlignment() / setVerticalAlignment(int) properties.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
Shape,
GroupShape| Modifier | Constructor and Description |
|---|---|
protected |
ShapeBase(DocumentBase doc,
byte markupLanguage)
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
java.awt.geom.Rectangle2D.Float |
adjustWithEffects(java.awt.geom.Rectangle2D.Float source)
Adds to the source rectangle values of the effect extent and returns the final rectangle.
|
boolean |
canHaveImage()
Returns
true if the shape type allows the shape to have an image. |
void |
clearRunAttrs() |
java.lang.Object |
fetchInheritedRunAttr(int fontAttr) |
java.lang.Object |
fetchInheritedShapeAttr(int key) |
java.lang.Object |
fetchShapeAttr(int key) |
boolean |
getAllowOverlap()
Gets a value that specifies whether this shape can overlap other shapes.
|
java.lang.String |
getAlternativeText()
Defines alternative text to be displayed instead of a graphic.
|
boolean |
getAnchorLocked()
Specifies whether the shape's anchor is locked.
|
boolean |
getAspectRatioLocked()
Specifies whether the shape's aspect ratio is locked.
|
boolean |
getBehindText()
Specifies whether the shape is below or above text.
|
double |
getBlur() |
double |
getBottom()
Gets the position of the bottom edge of the containing block of the shape.
|
java.awt.geom.Rectangle2D.Float |
getBounds()
Gets the location and size of the containing block of the shape.
|
java.awt.geom.Rectangle2D.Float |
getBoundsInPoints()
Gets the location and size of the containing block of the shape in points, relative to the anchor of the topmost shape.
|
java.awt.geom.Rectangle2D.Float |
getBoundsWithEffects()
Gets final extent that this shape object has after applying drawing effects.
|
java.awt.Color |
getColor() |
java.awt.Point |
getCoordOrigin()
The coordinates at the top-left corner of the containing block of this shape.
|
java.awt.Dimension |
getCoordSize()
The width and height of the coordinate space inside the containing block of this shape.
|
java.lang.Object |
getDirectRunAttr(int fontAttr) |
java.lang.Object |
getDirectRunAttr(int key,
int revisionsView) |
java.lang.Object |
getDirectShapeAttr(int key) |
double |
getDistance() |
double |
getDistanceBottom()
Gets the distance (in points) between the document text and the bottom edge of the shape.
|
double |
getDistanceLeft()
Gets the distance (in points) between the document text and the left edge of the shape.
|
double |
getDistanceRight()
Gets the distance (in points) between the document text and the right edge of the shape.
|
double |
getDistanceTop()
Gets the distance (in points) between the document text and the top edge of the shape.
|
DocumentBase |
getDocument_IInline() |
double |
getEdgeRadius() |
Fill |
getFill()
Gets fill formatting for the shape.
|
java.awt.Color |
getFillableBackColor() |
int |
getFillableBackThemeColor() |
double |
getFillableBackTintAndShade() |
java.awt.Color |
getFillableBaseForeColor() |
java.awt.Color |
getFillableForeColor() |
int |
getFillableForeThemeColor() |
double |
getFillableForeTintAndShade() |
byte[] |
getFillableImageBytes() |
double |
getFillableTransparency() |
boolean |
getFillableVisible() |
java.awt.Color |
getFilledColor() |
int |
getFillType() |
int |
getFlipOrientation()
Switches the orientation of a shape.
|
Font |
getFont()
Provides access to the font formatting of this object.
|
GlowFormat |
getGlow()
Gets glow formatting for the shape.
|
double |
getGradientAngle() |
GradientStopCollection |
getGradientStops() |
int |
getGradientStyle() |
int |
getGradientVariant() |
double |
getHeight()
Gets the height of the containing block of the shape.
|
float |
getHeightRelative()
Gets the value that represents the percentage of shape's relative height.
|
boolean |
getHidden()
Gets a boolean value indicating whether the shape is visible.
|
int |
getHorizontalAlignment()
Specifies how the shape is positioned horizontally.
|
java.lang.String |
getHRef()
Gets the full hyperlink address for a shape.
|
double |
getLeft()
Gets the position of the left edge of the containing block of the shape.
|
float |
getLeftRelative()
Gets the value that represents shape's relative left position in percent.
|
byte |
getMarkupLanguage()
Gets MarkupLanguage used for this graphic object.
|
java.lang.String |
getName()
Gets the optional shape name.
|
boolean |
getOldOn() |
double |
getOldOpacity() |
Paragraph |
getParentParagraph_IInline() |
Paragraph |
getParentParagraph()
Returns the immediate parent paragraph.
|
int |
getPatternType() |
int |
getPresetTexture() |
double |
getRadius() |
ReflectionFormat |
getReflection()
Gets reflection formatting for the shape.
|
double |
getReflectionSize() |
double |
getReflectionTransparency() |
int |
getRelativeHorizontalPosition()
Specifies relative to what the shape is positioned horizontally.
|
int |
getRelativeHorizontalSize()
Gets the value of shape's relative size in horizontal direction.
|
int |
getRelativeVerticalPosition()
Specifies relative to what the shape is positioned vertically.
|
int |
getRelativeVerticalSize()
Gets the value of shape's relative size in vertical direction.
|
double |
getRight()
Gets the position of the right edge of the containing block of the shape.
|
boolean |
getRotateWithObject() |
double |
getRotation()
Defines the angle (in degrees) that a shape is rotated.
|
java.lang.String |
getScreenTip()
Defines the text displayed when the mouse pointer moves over the shape.
|
java.awt.Color |
getShadowColors() |
ShadowFormat |
getShadowFormat()
Gets shadow formatting for the shape.
|
double |
getShadowTransparency() |
int |
getShadowType() |
ShapeRenderer |
getShapeRenderer()
Creates and returns an object that can be used to render this shape into an image.
|
int |
getShapeType()
Gets the shape type.
|
java.awt.geom.Point2D.Float |
getSizeInPoints()
Gets the size of the shape in points.
|
SoftEdgeFormat |
getSoftEdge()
Gets soft edge formatting for the shape.
|
java.lang.String |
getTarget()
Gets the target frame for the shape hyperlink.
|
int |
getTextureAlignment() |
java.lang.String |
getTitle()
Gets the title (caption) of the current shape object.
|
double |
getTop()
Gets the position of the top edge of the containing block of the shape.
|
float |
getTopRelative()
Gets the value that represents shape's relative top position in percent.
|
double |
getTransparency() |
int |
getVerticalAlignment()
Specifies how the shape is positioned vertically.
|
boolean |
getVisible() |
double |
getWidth()
Gets the width of the containing block of the shape.
|
float |
getWidthRelative()
Gets the value that represents the percentage of shape's relative width.
|
int |
getWrapSide()
Specifies how the text is wrapped around the shape.
|
int |
getWrapType()
Defines whether the shape is inline or floating.
|
int |
getZOrder_IShape() |
int |
getZOrder()
Determines the display order of overlapping shapes.
|
boolean |
isDecorative()
Gets the flag that specifies whether the shape is decorative in the document.
|
void |
isDecorative(boolean value)
Sets the flag that specifies whether the shape is decorative in the document.
|
boolean |
isDeleteRevision()
Returns true if this object was deleted in Microsoft Word while change tracking was enabled.
|
boolean |
isGroup()
Returns
true if this is a group shape. |
boolean |
isHorizontalRule()
Returns
true if this shape is a horizontal rule. |
boolean |
isImage()
Returns
true if this shape is an image shape. |
boolean |
isInline()
A quick way to determine if this shape is positioned inline with text.
|
boolean |
isInsertRevision()
Returns true if this object was inserted in Microsoft Word while change tracking was enabled.
|
boolean |
isLayoutInCell()
Gets a flag indicating whether the shape is displayed inside a table or outside of it.
|
void |
isLayoutInCell(boolean value)
Sets a flag indicating whether the shape is displayed inside a table or outside of it.
|
boolean |
isMoveFromRevision()
Returns
true if this object was moved (deleted) in Microsoft Word while change tracking was enabled. |
boolean |
isMoveToRevision()
Returns
true if this object was moved (inserted) in Microsoft Word while change tracking was enabled. |
boolean |
isSignatureLine()
Indicates that shape is a
SignatureLine. |
boolean |
isTopLevel()
Returns
true if this shape is not a child of a group shape. |
boolean |
isWordArt()
Returns
true if this shape is a WordArt object. |
java.awt.geom.Point2D.Float |
localToParent(java.awt.geom.Point2D.Float value)
Converts a value from the local coordinate space into the coordinate space of the parent shape.
|
void |
oneColorGradient(int style,
int variant,
double degree) |
void |
patterned(int patternType) |
void |
presetTextured(int presetTexture) |
void |
removeGlow() |
void |
removeMoveRevisions() |
void |
removeReflection() |
void |
removeRunAttr(int key) |
void |
removeShadow() |
void |
removeShapeAttr(int key) |
void |
removeSoftEdge() |
void |
setAllowOverlap(boolean value)
Sets a value that specifies whether this shape can overlap other shapes.
|
void |
setAlternativeText(java.lang.String value)
Defines alternative text to be displayed instead of a graphic.
|
void |
setAnchorLocked(boolean value)
Specifies whether the shape's anchor is locked.
|
void |
setAspectRatioLocked(boolean value)
Specifies whether the shape's aspect ratio is locked.
|
void |
setBehindText(boolean value)
Specifies whether the shape is below or above text.
|
void |
setBlur(double value) |
void |
setBounds(java.awt.geom.Rectangle2D.Float value)
Sets the location and size of the containing block of the shape.
|
void |
setColor(java.awt.Color value) |
void |
setCoordOrigin(java.awt.Point value)
The coordinates at the top-left corner of the containing block of this shape.
|
void |
setCoordSize(java.awt.Dimension value)
The width and height of the coordinate space inside the containing block of this shape.
|
void |
setDistance(double value) |
void |
setDistanceBottom(double value)
Sets the distance (in points) between the document text and the bottom edge of the shape.
|
void |
setDistanceLeft(double value)
Sets the distance (in points) between the document text and the left edge of the shape.
|
void |
setDistanceRight(double value)
Sets the distance (in points) between the document text and the right edge of the shape.
|
void |
setDistanceTop(double value)
Sets the distance (in points) between the document text and the top edge of the shape.
|
void |
setEdgeRadius(double value) |
void |
setFillableBackColor(java.awt.Color value) |
void |
setFillableBackThemeColor(int value) |
void |
setFillableBackTintAndShade(double value) |
void |
setFillableForeColor(java.awt.Color value) |
void |
setFillableForeThemeColor(int value) |
void |
setFillableForeTintAndShade(double value) |
void |
setFillableTransparency(double value) |
void |
setFillableVisible(boolean value) |
void |
setFilledColor(java.awt.Color value) |
void |
setFlipOrientation(int value)
Switches the orientation of a shape.
|
void |
setGradientAngle(double value) |
void |
setHeight(double value)
Sets the height of the containing block of the shape.
|
void |
setHeightRelative(float value)
Sets the value that represents the percentage of shape's relative height.
|
void |
setHidden(boolean value)
Sets a boolean value indicating whether the shape is visible.
|
void |
setHorizontalAlignment(int value)
Specifies how the shape is positioned horizontally.
|
void |
setHRef(java.lang.String value)
Sets the full hyperlink address for a shape.
|
void |
setImage(byte[] imageBytes) |
void |
setLeft(double value)
Sets the position of the left edge of the containing block of the shape.
|
void |
setLeftRelative(float value)
Sets the value that represents shape's relative left position in percent.
|
void |
setName(java.lang.String value)
Sets the optional shape name.
|
void |
setOldOn(boolean value) |
void |
setOldOpacity(double value) |
void |
setRadius(double value) |
void |
setReflectionSize(double value) |
void |
setReflectionTransparency(double value) |
void |
setRelativeHorizontalPosition(int value)
Specifies relative to what the shape is positioned horizontally.
|
void |
setRelativeHorizontalSize(int value)
Sets the value of shape's relative size in horizontal direction.
|
void |
setRelativeVerticalPosition(int value)
Specifies relative to what the shape is positioned vertically.
|
void |
setRelativeVerticalSize(int value)
Sets the value of shape's relative size in vertical direction.
|
void |
setRotateWithObject(boolean value) |
void |
setRotation(double value)
Defines the angle (in degrees) that a shape is rotated.
|
void |
setRunAttr(int fontAttr,
java.lang.Object value) |
void |
setScreenTip(java.lang.String value)
Defines the text displayed when the mouse pointer moves over the shape.
|
void |
setShadowColors(java.awt.Color value) |
void |
setShadowTransparency(double value) |
void |
setShadowType(int value) |
void |
setShapeAttr(int key,
java.lang.Object value) |
void |
setTarget(java.lang.String value)
Sets the target frame for the shape hyperlink.
|
void |
setTextureAlignment(int value) |
void |
setTitle(java.lang.String value)
Sets the title (caption) of the current shape object.
|
void |
setTop(double value)
Sets the position of the top edge of the containing block of the shape.
|
void |
setTopRelative(float value)
Sets the value that represents shape's relative top position in percent.
|
void |
setTransparency(double value) |
void |
setVerticalAlignment(int value)
Specifies how the shape is positioned vertically.
|
void |
setWidth(double value)
Sets the width of the containing block of the shape.
|
void |
setWidthRelative(float value)
Sets the value that represents the percentage of shape's relative width.
|
void |
setWrapSide(int value)
Specifies how the text is wrapped around the shape.
|
void |
setWrapType(int value)
Defines whether the shape is inline or floating.
|
void |
setZOrder_IShape(int value) |
void |
setZOrder(int value)
Determines the display order of overlapping shapes.
|
void |
solid() |
void |
twoColorGradient(int style,
int variant) |
acceptChildren, acceptCore, acceptEnd, acceptStart, appendChild, coreRemoveSelfOnly, getChild, getChildNodes, getContainer, getCount, getCurrentNode, getFirstChild, getLastChild, getNextMatchingNode, getText, hasChildNodes, indexOf, insertAfter, insertBefore, isComposite, iterator, prependChild, removeAllChildren, removeChild, removeSmartTags, selectNodes, selectSingleNodeaccept, deepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getNodeType, getParentNode, getPreviousSibling, getRange, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolprotected ShapeBase(DocumentBase doc, byte markupLanguage)
public ShapeRenderer getShapeRenderer() throws java.lang.Exception
Remarks:
This method just invokes the ShapeRenderer constructor and passes this object as a parameter.
Examples:
Shows how to use a shape renderer to export shapes to files in the local file system.
Document doc = new Document(getMyDir() + "Various shapes.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
Assert.assertEquals(7, shapes.getCount());
// There are 7 shapes in the document, including one group shape with 2 child shapes.
// We will render every shape to an image file in the local file system
// while ignoring the group shapes since they have no appearance.
// This will produce 6 image files.
for (Shape shape : (Iterable<Shape>) doc.getChildNodes(NodeType.SHAPE, true)) {
ShapeRenderer renderer = shape.getShapeRenderer();
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
renderer.save(getArtifactsDir() + MessageFormat.format("Shape.RenderAllShapes.{0}.png", shape.getName()), options);
}
java.lang.Exceptionpublic Paragraph getParentParagraph_IInline()
public DocumentBase getDocument_IInline()
public int getZOrder_IShape()
public void setZOrder_IShape(int value)
public java.lang.Object getDirectShapeAttr(int key)
public java.lang.Object fetchInheritedShapeAttr(int key)
public java.lang.Object fetchShapeAttr(int key)
public void setShapeAttr(int key,
java.lang.Object value)
public void removeShapeAttr(int key)
public void removeMoveRevisions()
public java.lang.Object getDirectRunAttr(int fontAttr)
public java.lang.Object getDirectRunAttr(int key,
int revisionsView)
public java.lang.Object fetchInheritedRunAttr(int fontAttr)
public void setRunAttr(int fontAttr,
java.lang.Object value)
public void removeRunAttr(int key)
public void clearRunAttrs()
public void solid()
public int getPresetTexture()
throws java.lang.Exception
java.lang.Exceptionpublic int getPatternType()
throws java.lang.Exception
java.lang.Exceptionpublic void presetTextured(int presetTexture)
public void patterned(int patternType)
public void twoColorGradient(int style,
int variant)
public void oneColorGradient(int style,
int variant,
double degree)
public void setImage(byte[] imageBytes)
public java.awt.Color getFilledColor()
public void setFilledColor(java.awt.Color value)
public boolean getOldOn()
public void setOldOn(boolean value)
public double getOldOpacity()
public void setOldOpacity(double value)
public byte[] getFillableImageBytes()
throws java.lang.Exception
java.lang.Exceptionpublic java.awt.Color getFillableForeColor()
public void setFillableForeColor(java.awt.Color value)
public java.awt.Color getFillableBaseForeColor()
public java.awt.Color getFillableBackColor()
public void setFillableBackColor(java.awt.Color value)
public int getFillableForeThemeColor()
throws java.lang.Exception
java.lang.Exceptionpublic void setFillableForeThemeColor(int value)
throws java.lang.Exception
java.lang.Exceptionpublic int getFillableBackThemeColor()
throws java.lang.Exception
java.lang.Exceptionpublic void setFillableBackThemeColor(int value)
throws java.lang.Exception
java.lang.Exceptionpublic double getFillableForeTintAndShade()
public void setFillableForeTintAndShade(double value)
public double getFillableBackTintAndShade()
public void setFillableBackTintAndShade(double value)
public boolean getFillableVisible()
public void setFillableVisible(boolean value)
public double getFillableTransparency()
public void setFillableTransparency(double value)
public boolean getRotateWithObject()
public void setRotateWithObject(boolean value)
public int getFillType()
public int getTextureAlignment()
public void setTextureAlignment(int value)
public double getGradientAngle()
public void setGradientAngle(double value)
public int getGradientVariant()
public int getGradientStyle()
public GradientStopCollection getGradientStops()
public void removeGlow()
public java.awt.Color getColor()
public void setColor(java.awt.Color value)
public double getTransparency()
public void setTransparency(double value)
public double getRadius()
public void setRadius(double value)
public void removeReflection()
public double getBlur()
public void setBlur(double value)
public double getDistance()
public void setDistance(double value)
public double getReflectionSize()
public void setReflectionSize(double value)
public double getReflectionTransparency()
public void setReflectionTransparency(double value)
public void removeSoftEdge()
public double getEdgeRadius()
public void setEdgeRadius(double value)
public void removeShadow()
public int getShadowType()
public void setShadowType(int value)
public boolean getVisible()
public java.awt.Color getShadowColors()
public void setShadowColors(java.awt.Color value)
public double getShadowTransparency()
public void setShadowTransparency(double value)
public java.awt.geom.Point2D.Float localToParent(java.awt.geom.Point2D.Float value)
Examples:
Shows how to translate the x and y coordinate location on a shape's coordinate plane to a location on the parent shape's coordinate plane.
Document doc = new Document();
// Insert a group shape, and place it 100 points below and to the right of
// the document's x and Y coordinate origin point.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(100f, 100f, 500f, 500f));
// Use the "LocalToParent" method to determine that (0, 0) on the group's internal x and y coordinates
// lies on (100, 100) of its parent shape's coordinate system. The group shape's parent is the document itself.
Assert.assertEquals(new Point2D.Float(100f, 100f), group.localToParent(new Point2D.Float(0f, 0f)));
// By default, a shape's internal coordinate plane has the top left corner at (0, 0),
// and the bottom right corner at (1000, 1000). Due to its size, our group shape covers an area of 500pt x 500pt
// in the document's plane. This means that a movement of 1pt on the document's coordinate plane will translate
// to a movement of 2pts on the group shape's coordinate plane.
Assert.assertEquals(new Point2D.Float(150f, 150f), group.localToParent(new Point2D.Float(100f, 100f)));
Assert.assertEquals(new Point2D.Float(200f, 200f), group.localToParent(new Point2D.Float(200f, 200f)));
Assert.assertEquals(new Point2D.Float(250f, 250f), group.localToParent(new Point2D.Float(300f, 300f)));
// Move the group shape's x and y axis origin from the top left corner to the center.
// This will offset the group's internal coordinates relative to the document's coordinates even further.
group.setCoordOrigin(new Point(-250, -250));
Assert.assertEquals(new Point2D.Float(375f, 375f), group.localToParent(new Point2D.Float(300f, 300f)));
// Changing the scale of the coordinate plane will also affect relative locations.
group.setCoordSize(new Dimension(500, 500));
Assert.assertEquals(new Point2D.Float(650f, 650f), group.localToParent(new Point2D.Float(300f, 300f)));
// If we wish to add a shape to this group while defining its location based on a location in the document,
// we will need to first confirm a location in the group shape that will match the document's location.
Assert.assertEquals(new Point2D.Float(700f, 700f), group.localToParent(new Point2D.Float(350f, 350f)));
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
group.appendChild(shape);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
doc.save(getArtifactsDir() + "Shape.LocalToParent.docx");
public Fill getFill()
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");
public ShadowFormat getShadowFormat()
Examples:
Shows how to get shadow color.
Document doc = new Document(getMyDir() + "Shadow color.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
ShadowFormat shadowFormat = shape.getShadowFormat();
Assert.assertEquals(Color.RED.getRGB(), shadowFormat.getColor().getRGB());
Assert.assertEquals(ShadowType.SHADOW_MIXED, shadowFormat.getType());
public GlowFormat getGlow()
Examples:
Shows how to interact with glow shape effect.
Document doc = new Document(getMyDir() + "Various shapes.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
// Apply glow effect to the shape.
shape.getGlow().setColor(new Color(0xFFFA8072));
shape.getGlow().setRadius(30.0);
shape.getGlow().setTransparency(0.15);
doc.save(getArtifactsDir() + "Shape.Glow.docx");
doc = new Document(getArtifactsDir() + "Shape.Glow.docx");
shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
// Check glow effect attributes.
Assert.assertEquals(new Color((250), (128), (114), (217)).getRGB(), shape.getGlow().getColor().getRGB());
Assert.assertEquals(30, shape.getGlow().getRadius());
Assert.assertEquals(0.15d, shape.getGlow().getTransparency(), 0.01d);
// Remove glow effect from the shape.
shape.getGlow().remove();
Assert.assertEquals(Color.BLACK.getRGB(), shape.getGlow().getColor().getRGB());
Assert.assertEquals(0, shape.getGlow().getRadius());
Assert.assertEquals(0, shape.getGlow().getTransparency());
public ReflectionFormat getReflection()
Examples:
Shows how to interact with reflection shape effect.
Document doc = new Document(getMyDir() + "Various shapes.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
// Apply reflection effect to the shape.
shape.getReflection().setTransparency(0.37);
shape.getReflection().setSize(0.48);
shape.getReflection().setBlur(17.5);
shape.getReflection().setDistance(9.2);
doc.save(getArtifactsDir() + "Shape.Reflection.docx");
doc = new Document(getArtifactsDir() + "Shape.Reflection.docx");
shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
// Check reflection effect attributes.
Assert.assertEquals(0.37d, shape.getReflection().getTransparency(), 0.01d);
Assert.assertEquals(0.48d, shape.getReflection().getSize(), 0.01d);
Assert.assertEquals(17.5d, shape.getReflection().getBlur(), 0.01d);
Assert.assertEquals(9.2d, shape.getReflection().getDistance(), 0.01d);
// Remove reflection effect from the shape.
shape.getReflection().remove();
Assert.assertEquals(0, shape.getReflection().getTransparency());
Assert.assertEquals(0, shape.getReflection().getSize());
Assert.assertEquals(0, shape.getReflection().getBlur());
Assert.assertEquals(0, shape.getReflection().getDistance());
public SoftEdgeFormat getSoftEdge()
Examples:
Shows how to set limit for image resolution.
Document doc = new Document(getMyDir() + "Rendering.docx");
SvgSaveOptions saveOptions = new SvgSaveOptions();
saveOptions.setMaxImageResolution(72);
doc.save(getArtifactsDir() + "SvgSaveOptions.MaxImageResolution.svg", saveOptions);
Shows how to work with soft edge formatting.
DocumentBuilder builder = new DocumentBuilder();
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 200.0, 200.0);
// Apply soft edge to the shape.
shape.getSoftEdge().setRadius(30.0);
builder.getDocument().save(getArtifactsDir() + "Shape.SoftEdge.docx");
// Load document with rectangle shape with soft edge.
Document doc = new Document(getArtifactsDir() + "Shape.SoftEdge.docx");
shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
// Check soft edge radius.
Assert.assertEquals(30, shape.getSoftEdge().getRadius());
// Remove soft edge from the shape.
shape.getSoftEdge().remove();
// Check radius of the removed soft edge.
Assert.assertEquals(0, shape.getSoftEdge().getRadius());
public java.lang.String getScreenTip()
Remarks:
The default value is an empty string.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
String value.public void setScreenTip(java.lang.String value)
Remarks:
The default value is an empty string.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
value - The corresponding String value.public java.lang.String getHRef()
Remarks:
The default value is an empty string.
Below are examples of valid values for this property:
Full URI: https://www.aspose.com/.
Full file name: C:\\My Documents\\SalesReport.doc.
Relative URI: ../../../resource.txt
Relative file name: ..\\My Documents\\SalesReport.doc.
Bookmark within another document: https://www.aspose.com/Products/Default.aspx#Suites
Bookmark within this document: #BookmakName.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
public void setHRef(java.lang.String value)
Remarks:
The default value is an empty string.
Below are examples of valid values for this property:
Full URI: https://www.aspose.com/.
Full file name: C:\\My Documents\\SalesReport.doc.
Relative URI: ../../../resource.txt
Relative file name: ..\\My Documents\\SalesReport.doc.
Bookmark within another document: https://www.aspose.com/Products/Default.aspx#Suites
Bookmark within this document: #BookmakName.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
value - The full hyperlink address for a shape.public java.lang.String getTarget()
Remarks:
The default value is an empty string.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
public void setTarget(java.lang.String value)
Remarks:
The default value is an empty string.
Examples:
Shows how to insert a shape which contains an image, and is also a hyperlink.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setHRef("https://forum.aspose.com/");
shape.setTarget("New Window");
shape.setScreenTip("Aspose.Words Support Forums");
// Ctrl + left-clicking the shape in Microsoft Word will open a new web browser window
// and take us to the hyperlink in the "HRef" property.
doc.save(getArtifactsDir() + "Image.InsertImageWithHyperlink.docx");
value - The target frame for the shape hyperlink.public java.lang.String getAlternativeText()
Remarks:
The default value is an empty string.
Examples:
Shows how to use a shape's alternative text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.CUBE, 150.0, 150.0);
shape.setName("MyCube");
shape.setAlternativeText("Alt text for MyCube.");
// We can access the alternative text of a shape by right-clicking it, and then via "Format AutoShape" -> "Alt Text".
doc.save(getArtifactsDir() + "Shape.AltText.docx");
// Save the document to HTML, and then delete the linked image that belongs to our shape.
// The browser that is reading our HTML will display the alt text in place of the missing image.
doc.save(getArtifactsDir() + "Shape.AltText.html");
new File(getArtifactsDir() + "Shape.AltText.001.png").delete();
String value.public void setAlternativeText(java.lang.String value)
Remarks:
The default value is an empty string.
Examples:
Shows how to use a shape's alternative text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.CUBE, 150.0, 150.0);
shape.setName("MyCube");
shape.setAlternativeText("Alt text for MyCube.");
// We can access the alternative text of a shape by right-clicking it, and then via "Format AutoShape" -> "Alt Text".
doc.save(getArtifactsDir() + "Shape.AltText.docx");
// Save the document to HTML, and then delete the linked image that belongs to our shape.
// The browser that is reading our HTML will display the alt text in place of the missing image.
doc.save(getArtifactsDir() + "Shape.AltText.html");
new File(getArtifactsDir() + "Shape.AltText.001.png").delete();
value - The corresponding String value.public boolean isDecorative()
Remarks:
Note that shape having not empty getAlternativeText() / setAlternativeText(java.lang.String) cannot be decorative.
Examples:
Shows how to set that the shape is decorative.
Document doc = new Document(getMyDir() + "Decorative shapes.docx");
Shape shape = (Shape) doc.getChildNodes(NodeType.SHAPE, true).get(0);
Assert.assertTrue(shape.isDecorative());
// If "AlternativeText" is not empty, the shape cannot be decorative.
// That's why our value has changed to 'false'.
shape.setAlternativeText("Alternative text.");
Assert.assertFalse(shape.isDecorative());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
// Create a new shape as decorative.
shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 100.0);
shape.isDecorative(true);
doc.save(getArtifactsDir() + "Shape.IsDecorative.docx");
public void isDecorative(boolean value)
Remarks:
Note that shape having not empty getAlternativeText() / setAlternativeText(java.lang.String) cannot be decorative.
Examples:
Shows how to set that the shape is decorative.
Document doc = new Document(getMyDir() + "Decorative shapes.docx");
Shape shape = (Shape) doc.getChildNodes(NodeType.SHAPE, true).get(0);
Assert.assertTrue(shape.isDecorative());
// If "AlternativeText" is not empty, the shape cannot be decorative.
// That's why our value has changed to 'false'.
shape.setAlternativeText("Alternative text.");
Assert.assertFalse(shape.isDecorative());
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
// Create a new shape as decorative.
shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 100.0);
shape.isDecorative(true);
doc.save(getArtifactsDir() + "Shape.IsDecorative.docx");
value - The flag that specifies whether the shape is decorative in the document.public java.lang.String getTitle()
Remarks:
Default is empty string.
Cannot be null, but can be an empty string.
Examples:
Shows how to set the title of a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a shape, give it a title, and then add it to the document.
Shape shape = new Shape(doc, ShapeType.CUBE);
shape.setWidth(200.0);
shape.setHeight(200.0);
shape.setTitle("My cube");
builder.insertNode(shape);
// When we save a document with a shape that has a title,
// Aspose.Words will store that title in the shape's Alt Text.
doc.save(getArtifactsDir() + "Shape.Title.docx");
doc = new Document(getArtifactsDir() + "Shape.Title.docx");
shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
Assert.assertEquals("", shape.getTitle());
Assert.assertEquals("Title: My cube", shape.getAlternativeText());
public void setTitle(java.lang.String value)
Remarks:
Default is empty string.
Cannot be null, but can be an empty string.
Examples:
Shows how to set the title of a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a shape, give it a title, and then add it to the document.
Shape shape = new Shape(doc, ShapeType.CUBE);
shape.setWidth(200.0);
shape.setHeight(200.0);
shape.setTitle("My cube");
builder.insertNode(shape);
// When we save a document with a shape that has a title,
// Aspose.Words will store that title in the shape's Alt Text.
doc.save(getArtifactsDir() + "Shape.Title.docx");
doc = new Document(getArtifactsDir() + "Shape.Title.docx");
shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
Assert.assertEquals("", shape.getTitle());
Assert.assertEquals("Title: My cube", shape.getAlternativeText());
value - The title (caption) of the current shape object.public java.lang.String getName()
Remarks:
Default is empty string.
Cannot be null, but can be an empty string.
Examples:
Shows how to use a shape's alternative text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.CUBE, 150.0, 150.0);
shape.setName("MyCube");
shape.setAlternativeText("Alt text for MyCube.");
// We can access the alternative text of a shape by right-clicking it, and then via "Format AutoShape" -> "Alt Text".
doc.save(getArtifactsDir() + "Shape.AltText.docx");
// Save the document to HTML, and then delete the linked image that belongs to our shape.
// The browser that is reading our HTML will display the alt text in place of the missing image.
doc.save(getArtifactsDir() + "Shape.AltText.html");
new File(getArtifactsDir() + "Shape.AltText.001.png").delete();
public void setName(java.lang.String value)
Remarks:
Default is empty string.
Cannot be null, but can be an empty string.
Examples:
Shows how to use a shape's alternative text.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.CUBE, 150.0, 150.0);
shape.setName("MyCube");
shape.setAlternativeText("Alt text for MyCube.");
// We can access the alternative text of a shape by right-clicking it, and then via "Format AutoShape" -> "Alt Text".
doc.save(getArtifactsDir() + "Shape.AltText.docx");
// Save the document to HTML, and then delete the linked image that belongs to our shape.
// The browser that is reading our HTML will display the alt text in place of the missing image.
doc.save(getArtifactsDir() + "Shape.AltText.html");
new File(getArtifactsDir() + "Shape.AltText.001.png").delete();
value - The optional shape name.public boolean isInsertRevision()
Examples:
Shows how to work with revision shapes.
Document doc = new Document();
Assert.assertFalse(doc.getTrackRevisions());
// Insert an inline shape without tracking revisions, which will make this shape not a revision of any kind.
Shape shape = new Shape(doc, ShapeType.CUBE);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
// Start tracking revisions and then insert another shape, which will be a revision.
doc.startTrackRevisions("John Doe");
shape = new Shape(doc, ShapeType.SUN);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
firstShape.remove();
// Since we removed that shape while we were tracking changes,
// the shape persists in the document and counts as a delete revision.
// Accepting this revision will remove the shape permanently, and rejecting it will keep it in the document.
Assert.assertEquals(ShapeType.CUBE, shapeList.get(0).getShapeType());
Assert.assertTrue(shapeList.get(0).isDeleteRevision());
// And we inserted another shape while tracking changes, so that shape will count as an insert revision.
// Accepting this revision will assimilate this shape into the document as a non-revision,
// and rejecting the revision will remove this shape permanently.
Assert.assertEquals(ShapeType.SUN, shapeList.get(1).getShapeType());
Assert.assertTrue(shapeList.get(1).isInsertRevision());
public boolean isDeleteRevision()
Examples:
Shows how to work with revision shapes.
Document doc = new Document();
Assert.assertFalse(doc.getTrackRevisions());
// Insert an inline shape without tracking revisions, which will make this shape not a revision of any kind.
Shape shape = new Shape(doc, ShapeType.CUBE);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
// Start tracking revisions and then insert another shape, which will be a revision.
doc.startTrackRevisions("John Doe");
shape = new Shape(doc, ShapeType.SUN);
shape.setWrapType(WrapType.INLINE);
shape.setWidth(100.0);
shape.setHeight(100.0);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(shape);
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
firstShape.remove();
// Since we removed that shape while we were tracking changes,
// the shape persists in the document and counts as a delete revision.
// Accepting this revision will remove the shape permanently, and rejecting it will keep it in the document.
Assert.assertEquals(ShapeType.CUBE, shapeList.get(0).getShapeType());
Assert.assertTrue(shapeList.get(0).isDeleteRevision());
// And we inserted another shape while tracking changes, so that shape will count as an insert revision.
// Accepting this revision will assimilate this shape into the document as a non-revision,
// and rejecting the revision will remove this shape permanently.
Assert.assertEquals(ShapeType.SUN, shapeList.get(1).getShapeType());
Assert.assertTrue(shapeList.get(1).isInsertRevision());
public boolean isMoveFromRevision()
true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
Examples:
Shows how to identify move revision shapes.
// A move revision is when we move an element in the document body by cut-and-pasting it in Microsoft Word while
// tracking changes. If we involve an inline shape in such a text movement, that shape will also be a revision.
// Copying-and-pasting or moving floating shapes do not create move revisions.
Document doc = new Document(getMyDir() + "Revision shape.docx");
// Move revisions consist of pairs of "Move from", and "Move to" revisions. We moved in this document in one shape,
// but until we accept or reject the move revision, there will be two instances of that shape.
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
// This is the "Move to" revision, which is the shape at its arrival destination.
// If we accept the revision, this "Move to" revision shape will disappear,
// and the "Move from" revision shape will remain.
Assert.assertFalse(shapeList.get(0).isMoveFromRevision());
Assert.assertTrue(shapeList.get(0).isMoveToRevision());
// This is the "Move from" revision, which is the shape at its original location.
// If we accept the revision, this "Move from" revision shape will disappear,
// and the "Move to" revision shape will remain.
Assert.assertTrue(shapeList.get(1).isMoveFromRevision());
Assert.assertFalse(shapeList.get(1).isMoveToRevision());
true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.public boolean isMoveToRevision()
true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
Examples:
Shows how to identify move revision shapes.
// A move revision is when we move an element in the document body by cut-and-pasting it in Microsoft Word while
// tracking changes. If we involve an inline shape in such a text movement, that shape will also be a revision.
// Copying-and-pasting or moving floating shapes do not create move revisions.
Document doc = new Document(getMyDir() + "Revision shape.docx");
// Move revisions consist of pairs of "Move from", and "Move to" revisions. We moved in this document in one shape,
// but until we accept or reject the move revision, there will be two instances of that shape.
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
// This is the "Move to" revision, which is the shape at its arrival destination.
// If we accept the revision, this "Move to" revision shape will disappear,
// and the "Move from" revision shape will remain.
Assert.assertFalse(shapeList.get(0).isMoveFromRevision());
Assert.assertTrue(shapeList.get(0).isMoveToRevision());
// This is the "Move from" revision, which is the shape at its original location.
// If we accept the revision, this "Move from" revision shape will disappear,
// and the "Move to" revision shape will remain.
Assert.assertTrue(shapeList.get(1).isMoveFromRevision());
Assert.assertFalse(shapeList.get(1).isMoveToRevision());
true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.public boolean isTopLevel()
true if this shape is not a child of a group shape.
Examples:
Shows how to tell whether a shape is a part of a group shape.
Document doc = new Document();
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
shape.setWidth(200.0);
shape.setHeight(200.0);
shape.setWrapType(WrapType.NONE);
// A shape by default is not part of any group shape, and therefore has the "IsTopLevel" property set to "true".
Assert.assertTrue(shape.isTopLevel());
GroupShape group = new GroupShape(doc);
group.appendChild(shape);
// Once we assimilate a shape into a group shape, the "IsTopLevel" property changes to "false".
Assert.assertFalse(shape.isTopLevel());
true if this shape is not a child of a group shape.public boolean isGroup()
true if this is a group shape.
Examples:
Shows how to create a group of shapes, and print its contents using a document visitor.
public void groupOfShapes() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// If you need to create "NonPrimitive" shapes, such as SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped,
// TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, DiagonalCornersRounded
// please use DocumentBuilder.InsertShape methods.
Shape balloon = new Shape(doc, ShapeType.BALLOON);
balloon.setWidth(200.0);
balloon.setHeight(200.0);
balloon.setStrokeColor(Color.RED);
Shape cube = new Shape(doc, ShapeType.CUBE);
cube.setWidth(100.0);
cube.setHeight(100.0);
cube.setStrokeColor(Color.BLUE);
GroupShape group = new GroupShape(doc);
group.appendChild(balloon);
group.appendChild(cube);
Assert.assertTrue(group.isGroup());
builder.insertNode(group);
ShapeInfoPrinter printer = new ShapeInfoPrinter();
group.accept(printer);
System.out.println(printer.getText());
}
/// <summary>
/// Prints the contents of a visited shape group to the console.
/// </summary>
public static class ShapeInfoPrinter extends DocumentVisitor {
public ShapeInfoPrinter() {
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public int visitGroupShapeStart(final GroupShape groupShape) {
mBuilder.append("Shape group started:\r\n");
return VisitorAction.CONTINUE;
}
public int visitGroupShapeEnd(final GroupShape groupShape) {
mBuilder.append("End of shape group\r\n");
return VisitorAction.CONTINUE;
}
public int visitShapeStart(final Shape shape) {
mBuilder.append("\tShape - " + shape.getShapeType() + ":\r\n");
mBuilder.append("\t\tWidth: " + shape.getWidth() + "\r\n");
mBuilder.append("\t\tHeight: " + shape.getHeight() + "\r\n");
mBuilder.append("\t\tStroke color: " + shape.getStroke().getColor() + "\r\n");
mBuilder.append("\t\tFill color: " + shape.getFill().getForeColor() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitShapeEnd(final Shape shape) {
mBuilder.append("\tEnd of shape\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
}
true if this is a group shape.public boolean isImage()
true if this shape is an image shape.
Examples:
Shows how to open an HTML document with images from a stream using a base URI.
InputStream stream = new FileInputStream(getMyDir() + "Document.html");
try {
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setBaseUri(getImageDir());
Document doc = new Document(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
Assert.assertTrue(shape.isImage());
Assert.assertNotNull(shape.getImageData().getImageBytes());
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getWidth()), 0.01);
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getHeight()), 0.01);
} finally {
if (stream != null) stream.close();
}
true if this shape is an image shape.public boolean isHorizontalRule()
true if this shape is a horizontal rule.
Examples:
Shows how to insert a horizontal rule shape, and customize its formatting.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertHorizontalRule();
HorizontalRuleFormat horizontalRuleFormat = shape.getHorizontalRuleFormat();
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER);
horizontalRuleFormat.setWidthPercent(70.0);
horizontalRuleFormat.setHeight(3.0);
horizontalRuleFormat.setColor(Color.BLUE);
horizontalRuleFormat.setNoShade(true);
Assert.assertTrue(shape.isHorizontalRule());
Assert.assertTrue(shape.getHorizontalRuleFormat().getNoShade());
true if this shape is a horizontal rule.public boolean isWordArt()
true if this shape is a WordArt object.
Remarks:
Works till 2007 compatibility mode. In 2010 and higher compatibility mode WordArt is just a TextBox with fancy fonts.
Examples:
Shows how to work with WordArt.
public void insertTextPaths() throws Exception {
Document doc = new Document();
// Insert a WordArt object to display text in a shape that we can re-size and move by using the mouse in Microsoft Word.
// Provide a "ShapeType" as an argument to set a shape for the WordArt.
Shape shape = appendWordArt(doc, "Hello World! This text is bold, and italic.",
"Arial", 480.0, 24.0, Color.WHITE, Color.BLACK, ShapeType.TEXT_PLAIN_TEXT);
// Apply the "Bold" and "Italic" formatting settings to the text using the respective properties.
shape.getTextPath().setBold(true);
shape.getTextPath().setItalic(true);
// Below are various other text formatting-related properties.
Assert.assertFalse(shape.getTextPath().getUnderline());
Assert.assertFalse(shape.getTextPath().getShadow());
Assert.assertFalse(shape.getTextPath().getStrikeThrough());
Assert.assertFalse(shape.getTextPath().getReverseRows());
Assert.assertFalse(shape.getTextPath().getXScale());
Assert.assertFalse(shape.getTextPath().getTrim());
Assert.assertFalse(shape.getTextPath().getSmallCaps());
Assert.assertEquals(36.0, shape.getTextPath().getSize());
Assert.assertEquals("Hello World! This text is bold, and italic.", shape.getTextPath().getText());
Assert.assertEquals(ShapeType.TEXT_PLAIN_TEXT, shape.getShapeType());
// Use the "On" property to show/hide the text.
shape = appendWordArt(doc, "On set to \"true\"", "Calibri", 150.0, 24.0, Color.YELLOW, Color.RED, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setOn(true);
shape = appendWordArt(doc, "On set to \"false\"", "Calibri", 150.0, 24.0, Color.YELLOW, Color.pink, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setOn(false);
// Use the "Kerning" property to enable/disable kerning spacing between certain characters.
shape = appendWordArt(doc, "Kerning: VAV", "Times New Roman", 90.0, 24.0, Color.ORANGE, Color.RED, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setKerning(true);
shape = appendWordArt(doc, "No kerning: VAV", "Times New Roman", 100.0, 24.0, Color.ORANGE, Color.RED, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setKerning(false);
// Use the "Spacing" property to set the custom spacing between characters on a scale from 0.0 (none) to 1.0 (default).
shape = appendWordArt(doc, "Spacing set to 0.1", "Calibri", 120.0, 24.0, Color.BLUE, Color.BLUE, ShapeType.TEXT_CASCADE_DOWN);
shape.getTextPath().setSpacing(0.1);
// Set the "RotateLetters" property to "true" to rotate each character 90 degrees counterclockwise.
shape = appendWordArt(doc, "RotateLetters", "Calibri", 200.0, 36.0, Color.YELLOW, Color.GREEN, ShapeType.TEXT_WAVE);
shape.getTextPath().setRotateLetters(true);
// Set the "SameLetterHeights" property to "true" to get the x-height of each character to equal the cap height.
shape = appendWordArt(doc, "Same character height for lower and UPPER case", "Calibri", 300.0, 24.0, Color.BLUE, Color.BLUE, ShapeType.TEXT_SLANT_UP);
shape.getTextPath().setSameLetterHeights(true);
// By default, the text's size will always scale to fit the containing shape's size, overriding the text size setting.
shape = appendWordArt(doc, "FitShape on", "Calibri", 160.0, 24.0, Color.BLUE, Color.BLUE, ShapeType.TEXT_PLAIN_TEXT);
Assert.assertTrue(shape.getTextPath().getFitShape());
shape.getTextPath().setSize(24.0);
// If we set the "FitShape: property to "false", the text will keep the size
// which the "Size" property specifies regardless of the size of the shape.
// Use the "TextPathAlignment" property also to align the text to a side of the shape.
shape = appendWordArt(doc, "FitShape off", "Calibri", 160.0, 24.0, Color.BLUE, Color.BLUE, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setFitShape(false);
shape.getTextPath().setSize(24.0);
shape.getTextPath().setTextPathAlignment(TextPathAlignment.RIGHT);
doc.save(getArtifactsDir() + "Shape.InsertTextPaths.docx");
}
/// <summary>
/// Insert a new paragraph with a WordArt shape inside it.
/// </summary>
private static Shape appendWordArt(Document doc, String text, String textFontFamily, double shapeWidth, double shapeHeight, Color wordArtFill, Color line, int wordArtShapeType) throws Exception {
// Create an inline Shape, which will serve as a container for our WordArt.
// The shape can only be a valid WordArt shape if we assign a WordArt-designated ShapeType to it.
// These types will have "WordArt object" in the description,
// and their enumerator constant names will all start with "Text".
Shape shape = new Shape(doc, wordArtShapeType);
{
shape.setWrapType(WrapType.INLINE);
shape.setWidth(shapeWidth);
shape.setHeight(shapeHeight);
shape.setFillColor(wordArtFill);
shape.setStrokeColor(line);
}
shape.getTextPath().setText(text);
shape.getTextPath().setFontFamily(textFontFamily);
Paragraph para = (Paragraph) doc.getFirstSection().getBody().appendChild(new Paragraph(doc));
para.appendChild(shape);
return shape;
}
true if this shape is a WordArt object.public boolean canHaveImage()
true if the shape type allows the shape to have an image.
Remarks:
Although Microsoft Word has a special shape type for images, it appears that in Microsoft Word documents any shape except a group shape can have an image, therefore this property returns true for all shapes except GroupShape.
Examples:
Shows how to insert and rotate an image.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape with an image.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
Assert.assertTrue(shape.canHaveImage());
Assert.assertTrue(shape.hasImage());
// Rotate the image 45 degrees clockwise.
shape.setRotation(45.0);
doc.save(getArtifactsDir() + "Shape.Rotate.docx");
true if the shape type allows the shape to have an image.public boolean getAnchorLocked()
Remarks:
The default value is false.
Has effect only for top level shapes.
This property affects behavior of the shape's anchor in Microsoft Word. When the anchor is not locked, moving the shape in Microsoft Word can move the shape's anchor too.
Examples:
Shows how to lock or unlock a shape's paragraph anchor.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
builder.write("Our shape will have an anchor attached to this paragraph.");
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 200.0, 160.0);
shape.setWrapType(WrapType.NONE);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.writeln("Hello again!");
// Set the "AnchorLocked" property to "true" to prevent the shape's anchor
// from moving when moving the shape in Microsoft Word.
// Set the "AnchorLocked" property to "false" to allow any movement of the shape
// to also move its anchor to any other paragraph that the shape ends up close to.
shape.setAnchorLocked(anchorLocked);
// If the shape does not have a visible anchor symbol to its left,
// we will need to enable visible anchors via "Options" -> "Display" -> "Object Anchors".
doc.save(getArtifactsDir() + "Shape.AnchorLocked.docx");
boolean value.public void setAnchorLocked(boolean value)
Remarks:
The default value is false.
Has effect only for top level shapes.
This property affects behavior of the shape's anchor in Microsoft Word. When the anchor is not locked, moving the shape in Microsoft Word can move the shape's anchor too.
Examples:
Shows how to lock or unlock a shape's paragraph anchor.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
builder.write("Our shape will have an anchor attached to this paragraph.");
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 200.0, 160.0);
shape.setWrapType(WrapType.NONE);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.writeln("Hello again!");
// Set the "AnchorLocked" property to "true" to prevent the shape's anchor
// from moving when moving the shape in Microsoft Word.
// Set the "AnchorLocked" property to "false" to allow any movement of the shape
// to also move its anchor to any other paragraph that the shape ends up close to.
shape.setAnchorLocked(anchorLocked);
// If the shape does not have a visible anchor symbol to its left,
// we will need to enable visible anchors via "Options" -> "Display" -> "Object Anchors".
doc.save(getArtifactsDir() + "Shape.AnchorLocked.docx");
value - The corresponding boolean value.public boolean getAspectRatioLocked()
Remarks:
The default value depends on the ShapeType, for the ShapeType.IMAGE it is true but for the other shape types it is false.
Has effect for top level shapes only.
Examples:
Shows how to lock/unlock a shape's aspect ratio.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape. If we open this document in Microsoft Word, we can left click the shape to reveal
// eight sizing handles around its perimeter, which we can click and drag to change its size.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// Set the "AspectRatioLocked" property to "true" to preserve the shape's aspect ratio
// when using any of the four diagonal sizing handles, which change both the image's height and width.
// Using any orthogonal sizing handles that either change the height or width will still change the aspect ratio.
// Set the "AspectRatioLocked" property to "false" to allow us to
// freely change the image's aspect ratio with all sizing handles.
shape.setAspectRatioLocked(lockAspectRatio);
doc.save(getArtifactsDir() + "Shape.AspectRatio.docx");
boolean value.public void setAspectRatioLocked(boolean value)
Remarks:
The default value depends on the ShapeType, for the ShapeType.IMAGE it is true but for the other shape types it is false.
Has effect for top level shapes only.
Examples:
Shows how to lock/unlock a shape's aspect ratio.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape. If we open this document in Microsoft Word, we can left click the shape to reveal
// eight sizing handles around its perimeter, which we can click and drag to change its size.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// Set the "AspectRatioLocked" property to "true" to preserve the shape's aspect ratio
// when using any of the four diagonal sizing handles, which change both the image's height and width.
// Using any orthogonal sizing handles that either change the height or width will still change the aspect ratio.
// Set the "AspectRatioLocked" property to "false" to allow us to
// freely change the image's aspect ratio with all sizing handles.
shape.setAspectRatioLocked(lockAspectRatio);
doc.save(getArtifactsDir() + "Shape.AspectRatio.docx");
value - The corresponding boolean value.public boolean getAllowOverlap()
Remarks:
This property affects behavior of the shape in Microsoft Word. Aspose.Words ignores the value of this property.
This property is applicable only to top level shapes.
The default value is true.
Examples:
Shows how to work with floating tables properties.
Document doc = new Document(getMyDir() + "Table wrapped by text.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
if (table.getTextWrapping() == TextWrapping.AROUND) {
Assert.assertEquals(RelativeHorizontalPosition.MARGIN, table.getHorizontalAnchor());
Assert.assertEquals(RelativeVerticalPosition.PARAGRAPH, table.getVerticalAnchor());
Assert.assertEquals(false, table.getAllowOverlap());
// Only Margin, Page, Column available in RelativeHorizontalPosition for HorizontalAnchor setter.
// The ArgumentException will be thrown for any other values.
table.setHorizontalAnchor(RelativeHorizontalPosition.COLUMN);
// Only Margin, Page, Paragraph available in RelativeVerticalPosition for VerticalAnchor setter.
// The ArgumentException will be thrown for any other values.
table.setVerticalAnchor(RelativeVerticalPosition.PAGE);
}
public void setAllowOverlap(boolean value)
Remarks:
This property affects behavior of the shape in Microsoft Word. Aspose.Words ignores the value of this property.
This property is applicable only to top level shapes.
The default value is true.
Examples:
Shows how to work with floating tables properties.
Document doc = new Document(getMyDir() + "Table wrapped by text.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
if (table.getTextWrapping() == TextWrapping.AROUND) {
Assert.assertEquals(RelativeHorizontalPosition.MARGIN, table.getHorizontalAnchor());
Assert.assertEquals(RelativeVerticalPosition.PARAGRAPH, table.getVerticalAnchor());
Assert.assertEquals(false, table.getAllowOverlap());
// Only Margin, Page, Column available in RelativeHorizontalPosition for HorizontalAnchor setter.
// The ArgumentException will be thrown for any other values.
table.setHorizontalAnchor(RelativeHorizontalPosition.COLUMN);
// Only Margin, Page, Paragraph available in RelativeVerticalPosition for VerticalAnchor setter.
// The ArgumentException will be thrown for any other values.
table.setVerticalAnchor(RelativeVerticalPosition.PAGE);
}
value - A value that specifies whether this shape can overlap other shapes.public boolean getBehindText()
Remarks:
Has effect only for top level shapes.
The default value is false.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
boolean value.getZOrder(),
setZOrder(int)public void setBehindText(boolean value)
Remarks:
Has effect only for top level shapes.
The default value is false.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
value - The corresponding boolean value.getZOrder(),
setZOrder(int)public boolean isInline()
Remarks:
Has effect only for top level shapes.
Examples:
Shows how to determine whether a shape is inline or floating.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two wrapping types that shapes may have.
// 1 - Inline:
builder.write("Hello world! ");
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 100.0);
shape.setFillColor(Color.BLUE);
builder.write(" Hello again.");
// An inline shape sits inside a paragraph among other paragraph elements, such as runs of text.
// In Microsoft Word, we may click and drag the shape to any paragraph as if it is a character.
// If the shape is large, it will affect vertical paragraph spacing.
// We cannot move this shape to a place with no paragraph.
Assert.assertEquals(WrapType.INLINE, shape.getWrapType());
Assert.assertTrue(shape.isInline());
// 2 - Floating:
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 200.0,
RelativeVerticalPosition.TOP_MARGIN, 200.0, 100.0, 100.0, WrapType.NONE);
shape.setFillColor(Color.ORANGE);
// A floating shape belongs to the paragraph that we insert it into,
// which we can determine by an anchor symbol that appears when we click the shape.
// If the shape does not have a visible anchor symbol to its left,
// we will need to enable visible anchors via "Options" -> "Display" -> "Object Anchors".
// In Microsoft Word, we may left click and drag this shape freely to any location.
Assert.assertEquals(WrapType.NONE, shape.getWrapType());
Assert.assertFalse(shape.isInline());
doc.save(getArtifactsDir() + "Shape.IsInline.docx");
boolean value.public double getLeft()
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Has effect only for floating shapes.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
public void setLeft(double value)
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Has effect only for floating shapes.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
value - The position of the left edge of the containing block of the shape.public double getTop()
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Has effect only for floating shapes.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
public void setTop(double value)
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Has effect only for floating shapes.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
value - The position of the top edge of the containing block of the shape.public double getRight()
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
public double getBottom()
Remarks:
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
public double getWidth()
Remarks:
For a top-level shape, the value is in points.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
Shows how to resize a shape with an image.
// When we insert an image using the "InsertImage" method, the builder scales the shape that displays the image so that,
// when we view the document using 100% zoom in Microsoft Word, the shape displays the image in its actual size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// A 400x400 image will create an ImageData object with an image size of 300x300pt.
ImageSize imageSize = shape.getImageData().getImageSize();
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// If a shape's dimensions match the image data's dimensions,
// then the shape is displaying the image in its original size.
Assert.assertEquals(300.0d, shape.getWidth());
Assert.assertEquals(300.0d, shape.getHeight());
// Reduce the overall size of the shape by 50%.
shape.setWidth(shape.getWidth() * 0.5);
// Scaling factors apply to both the width and the height at the same time to preserve the shape's proportions.
Assert.assertEquals(150.0d, shape.getWidth());
Assert.assertEquals(150.0d, shape.getHeight());
// When we resize the shape, the size of the image data remains the same.
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// We can reference the image data dimensions to apply a scaling based on the size of the image.
shape.setWidth(imageSize.getWidthPoints() * 1.1);
Assert.assertEquals(330.0d, shape.getWidth());
Assert.assertEquals(330.0d, shape.getHeight());
doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
public void setWidth(double value)
throws java.lang.Exception
Remarks:
For a top-level shape, the value is in points.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
Shows how to resize a shape with an image.
// When we insert an image using the "InsertImage" method, the builder scales the shape that displays the image so that,
// when we view the document using 100% zoom in Microsoft Word, the shape displays the image in its actual size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// A 400x400 image will create an ImageData object with an image size of 300x300pt.
ImageSize imageSize = shape.getImageData().getImageSize();
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// If a shape's dimensions match the image data's dimensions,
// then the shape is displaying the image in its original size.
Assert.assertEquals(300.0d, shape.getWidth());
Assert.assertEquals(300.0d, shape.getHeight());
// Reduce the overall size of the shape by 50%.
shape.setWidth(shape.getWidth() * 0.5);
// Scaling factors apply to both the width and the height at the same time to preserve the shape's proportions.
Assert.assertEquals(150.0d, shape.getWidth());
Assert.assertEquals(150.0d, shape.getHeight());
// When we resize the shape, the size of the image data remains the same.
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// We can reference the image data dimensions to apply a scaling based on the size of the image.
shape.setWidth(imageSize.getWidthPoints() * 1.1);
Assert.assertEquals(330.0d, shape.getWidth());
Assert.assertEquals(330.0d, shape.getHeight());
doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
value - The width of the containing block of the shape.java.lang.Exceptionpublic double getHeight()
Remarks:
For a top-level shape, the value is in points.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
Shows how to resize a shape with an image.
// When we insert an image using the "InsertImage" method, the builder scales the shape that displays the image so that,
// when we view the document using 100% zoom in Microsoft Word, the shape displays the image in its actual size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// A 400x400 image will create an ImageData object with an image size of 300x300pt.
ImageSize imageSize = shape.getImageData().getImageSize();
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// If a shape's dimensions match the image data's dimensions,
// then the shape is displaying the image in its original size.
Assert.assertEquals(300.0d, shape.getWidth());
Assert.assertEquals(300.0d, shape.getHeight());
// Reduce the overall size of the shape by 50%.
shape.setWidth(shape.getWidth() * 0.5);
// Scaling factors apply to both the width and the height at the same time to preserve the shape's proportions.
Assert.assertEquals(150.0d, shape.getWidth());
Assert.assertEquals(150.0d, shape.getHeight());
// When we resize the shape, the size of the image data remains the same.
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// We can reference the image data dimensions to apply a scaling based on the size of the image.
shape.setWidth(imageSize.getWidthPoints() * 1.1);
Assert.assertEquals(330.0d, shape.getWidth());
Assert.assertEquals(330.0d, shape.getHeight());
doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
public void setHeight(double value)
throws java.lang.Exception
Remarks:
For a top-level shape, the value is in points.
For shapes in a group, the value is in the coordinate space and units of the parent group.
The default value is 0.
Examples:
Shows how to insert a floating image, and specify its position and size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
// Configure the shape's "RelativeHorizontalPosition" property to treat the value of the "Left" property
// as the shape's horizontal distance, in points, from the left side of the page.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
// Set the shape's horizontal distance from the left side of the page to 100.
shape.setLeft(100.0);
// Use the "RelativeVerticalPosition" property in a similar way to position the shape 80pt below the top of the page.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(80.0);
// Set the shape's height, which will automatically scale the width to preserve dimensions.
shape.setHeight(125.0);
Assert.assertEquals(125.0d, shape.getWidth());
// The "Bottom" and "Right" properties contain the bottom and right edges of the image.
Assert.assertEquals(shape.getTop() + shape.getHeight(), shape.getBottom());
Assert.assertEquals(shape.getLeft() + shape.getWidth(), shape.getRight());
doc.save(getArtifactsDir() + "Image.CreateFloatingPositionSize.docx");
Shows how to resize a shape with an image.
// When we insert an image using the "InsertImage" method, the builder scales the shape that displays the image so that,
// when we view the document using 100% zoom in Microsoft Word, the shape displays the image in its actual size.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
// A 400x400 image will create an ImageData object with an image size of 300x300pt.
ImageSize imageSize = shape.getImageData().getImageSize();
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// If a shape's dimensions match the image data's dimensions,
// then the shape is displaying the image in its original size.
Assert.assertEquals(300.0d, shape.getWidth());
Assert.assertEquals(300.0d, shape.getHeight());
// Reduce the overall size of the shape by 50%.
shape.setWidth(shape.getWidth() * 0.5);
// Scaling factors apply to both the width and the height at the same time to preserve the shape's proportions.
Assert.assertEquals(150.0d, shape.getWidth());
Assert.assertEquals(150.0d, shape.getHeight());
// When we resize the shape, the size of the image data remains the same.
Assert.assertEquals(300.0d, imageSize.getWidthPoints());
Assert.assertEquals(300.0d, imageSize.getHeightPoints());
// We can reference the image data dimensions to apply a scaling based on the size of the image.
shape.setWidth(imageSize.getWidthPoints() * 1.1);
Assert.assertEquals(330.0d, shape.getWidth());
Assert.assertEquals(330.0d, shape.getHeight());
doc.save(getArtifactsDir() + "Image.ScaleImage.docx");
value - The height of the containing block of the shape.java.lang.Exceptionpublic double getDistanceTop()
Remarks:
The default value is 0.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
public void setDistanceTop(double value)
Remarks:
The default value is 0.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
value - The distance (in points) between the document text and the top edge of the shape.public double getDistanceBottom()
Remarks:
The default value is 0.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
public void setDistanceBottom(double value)
Remarks:
The default value is 0.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
value - The distance (in points) between the document text and the bottom edge of the shape.public double getDistanceLeft()
Remarks:
The default value is 1/8 inch.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
public void setDistanceLeft(double value)
Remarks:
The default value is 1/8 inch.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
value - The distance (in points) between the document text and the left edge of the shape.public double getDistanceRight()
Remarks:
The default value is 1/8 inch.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
public void setDistanceRight(double value)
Remarks:
The default value is 1/8 inch.
Has effect only for top level shapes.
Examples:
Shows how to set the wrapping distance for a text that surrounds a shape.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a rectangle and, get the text to wrap tightly around its bounds.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 150.0, 150.0);
shape.setWrapType(WrapType.TIGHT);
// Set the minimum distance between the shape and surrounding text to 40pt from all sides.
shape.setDistanceTop(40.0);
shape.setDistanceBottom(40.0);
shape.setDistanceLeft(40.0);
shape.setDistanceRight(40.0);
// Move the shape closer to the center of the page, and then rotate the shape 60 degrees clockwise.
shape.setTop(75.0);
shape.setLeft(150.0);
shape.setRotation(60.0);
// Add text that will wrap around the shape.
builder.getFont().setSize(24.0d);
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
doc.save(getArtifactsDir() + "Shape.Coordinates.docx");
value - The distance (in points) between the document text and the right edge of the shape.public double getRotation()
Remarks:
The default value is 0.
Examples:
Shows how to insert and rotate an image.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape with an image.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
Assert.assertTrue(shape.canHaveImage());
Assert.assertTrue(shape.hasImage());
// Rotate the image 45 degrees clockwise.
shape.setRotation(45.0);
doc.save(getArtifactsDir() + "Shape.Rotate.docx");
double value.public void setRotation(double value)
Remarks:
The default value is 0.
Examples:
Shows how to insert and rotate an image.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape with an image.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
Assert.assertTrue(shape.canHaveImage());
Assert.assertTrue(shape.hasImage());
// Rotate the image 45 degrees clockwise.
shape.setRotation(45.0);
doc.save(getArtifactsDir() + "Shape.Rotate.docx");
value - The corresponding double value.public int getZOrder()
Remarks:
Has effect only for top level shapes.
The default value is 0.
The number represents the stacking precedence. A shape with a higher number will be displayed as if it were overlapping (in "front" of) a shape with a lower number.
The order of overlapping shapes is independent for shapes in the header and in the main text of the document.
The display order of child shapes in a group shape is determined by their order inside the group shape.
Examples:
Shows how to manipulate the order of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert three different colored rectangles that partially overlap each other.
// When we insert a shape that overlaps another shape, Aspose.Words places the newer shape on top of the old one.
// The light green rectangle will overlap the light blue rectangle and partially obscure it,
// and the light blue rectangle will obscure the orange rectangle.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.ORANGE);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 150.0,
RelativeVerticalPosition.TOP_MARGIN, 150.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.BLUE);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 200.0,
RelativeVerticalPosition.TOP_MARGIN, 200.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.GREEN);
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
// The "ZOrder" property of a shape determines its stacking priority among other overlapping shapes.
// If two overlapping shapes have different "ZOrder" values,
// Microsoft Word will place the shape with a higher value over the shape with the lower value.
// Set the "ZOrder" values of our shapes to place the first orange rectangle over the second light blue one
// and the second light blue rectangle over the third light green rectangle.
// This will reverse their original stacking order.
shapeList.get(0).setZOrder(3);
shapeList.get(1).setZOrder(2);
shapeList.get(2).setZOrder(1);
doc.save(getArtifactsDir() + "Shape.ZOrder.docx");
int value.getBehindText(),
setBehindText(boolean)public void setZOrder(int value)
Remarks:
Has effect only for top level shapes.
The default value is 0.
The number represents the stacking precedence. A shape with a higher number will be displayed as if it were overlapping (in "front" of) a shape with a lower number.
The order of overlapping shapes is independent for shapes in the header and in the main text of the document.
The display order of child shapes in a group shape is determined by their order inside the group shape.
Examples:
Shows how to manipulate the order of shapes.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert three different colored rectangles that partially overlap each other.
// When we insert a shape that overlaps another shape, Aspose.Words places the newer shape on top of the old one.
// The light green rectangle will overlap the light blue rectangle and partially obscure it,
// and the light blue rectangle will obscure the orange rectangle.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.ORANGE);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 150.0,
RelativeVerticalPosition.TOP_MARGIN, 150.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.BLUE);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 200.0,
RelativeVerticalPosition.TOP_MARGIN, 200.0, 200.0, 200.0, WrapType.NONE);
shape.setFillColor(Color.GREEN);
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
// The "ZOrder" property of a shape determines its stacking priority among other overlapping shapes.
// If two overlapping shapes have different "ZOrder" values,
// Microsoft Word will place the shape with a higher value over the shape with the lower value.
// Set the "ZOrder" values of our shapes to place the first orange rectangle over the second light blue one
// and the second light blue rectangle over the third light green rectangle.
// This will reverse their original stacking order.
shapeList.get(0).setZOrder(3);
shapeList.get(1).setZOrder(2);
shapeList.get(2).setZOrder(1);
doc.save(getArtifactsDir() + "Shape.ZOrder.docx");
value - The corresponding int value.getBehindText(),
setBehindText(boolean)public Paragraph getParentParagraph()
Remarks:
For child shapes of a group shape and child shapes of an Office Math object always returns null.
Examples:
Shows how to insert a text box, and set the font of its contents.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, 300.0, 50.0);
builder.moveTo(shape.getLastParagraph());
builder.write("This text is inside the text box.");
// Set the "Hidden" property of the shape's "Font" object to "true" to hide the text box from sight
// and collapse the space that it would normally occupy.
// Set the "Hidden" property of the shape's "Font" object to "false" to leave the text box visible.
shape.getFont().setHidden(hideShape);
// If the shape is visible, we will modify its appearance via the font object.
if (!hideShape) {
shape.getFont().setHighlightColor(Color.LIGHT_GRAY);
shape.getFont().setColor(Color.RED);
shape.getFont().setUnderline(Underline.DASH);
}
// Move the builder out of the text box back into the main document.
builder.moveTo(shape.getParentParagraph());
builder.writeln("\nThis text is outside the text box.");
doc.save(getArtifactsDir() + "Shape.Font.docx");
public java.awt.geom.Rectangle2D.Float getBounds()
Remarks:
Ignores aspect ratio lock upon setting.
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
Examples:
Shows how to verify shape containing block boundaries.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.LINE, RelativeHorizontalPosition.LEFT_MARGIN, 50.0,
RelativeVerticalPosition.TOP_MARGIN, 50.0, 100.0, 100.0, WrapType.NONE);
shape.setStrokeColor(Color.ORANGE);
// Even though the line itself takes up little space on the document page,
// it occupies a rectangular containing block, the size of which we can determine using the "Bounds" properties.
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBounds());
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBoundsInPoints());
// Create a group shape, and then set the size of its containing block using the "Bounds" property.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(0f, 100f, 250f, 250f));
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
// Create a rectangle, verify the size of its bounding block, and then add it to the group shape.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
Assert.assertEquals(new Rectangle2D.Float(700f, 700f, 100f, 100f), shape.getBoundsInPoints());
group.appendChild(shape);
// The group shape's coordinate plane has its origin on the top left-hand side corner of its containing block,
// and the x and y coordinates of (1000, 1000) on the bottom right-hand side corner.
// Our group shape is 250x250pt in size, so every 4pt on the group shape's coordinate plane
// translates to 1pt in the document body's coordinate plane.
// Every shape that we insert will also shrink in size by a factor of 4.
// The change in the shape's "BoundsInPoints" property will reflect this.
Assert.assertEquals(new Rectangle2D.Float(175f, 275f, 25f, 25f), shape.getBoundsInPoints());
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
// Insert a shape and place it outside of the bounds of the group shape's containing block.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(1000.0);
shape.setTop(1000.0);
}
group.appendChild(shape);
// The group shape's footprint in the document body has increased, but the containing block remains the same.
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
Assert.assertEquals(new Rectangle2D.Float(250f, 350f, 25f, 25f), shape.getBoundsInPoints());
doc.save(getArtifactsDir() + "Shape.Bounds.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
public void setBounds(java.awt.geom.Rectangle2D.Float value)
Remarks:
Ignores aspect ratio lock upon setting.
For a top-level shape, the value is in points and relative to the shape anchor.
For shapes in a group, the value is in the coordinate space and units of the parent group.
Examples:
Shows how to verify shape containing block boundaries.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.LINE, RelativeHorizontalPosition.LEFT_MARGIN, 50.0,
RelativeVerticalPosition.TOP_MARGIN, 50.0, 100.0, 100.0, WrapType.NONE);
shape.setStrokeColor(Color.ORANGE);
// Even though the line itself takes up little space on the document page,
// it occupies a rectangular containing block, the size of which we can determine using the "Bounds" properties.
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBounds());
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBoundsInPoints());
// Create a group shape, and then set the size of its containing block using the "Bounds" property.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(0f, 100f, 250f, 250f));
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
// Create a rectangle, verify the size of its bounding block, and then add it to the group shape.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
Assert.assertEquals(new Rectangle2D.Float(700f, 700f, 100f, 100f), shape.getBoundsInPoints());
group.appendChild(shape);
// The group shape's coordinate plane has its origin on the top left-hand side corner of its containing block,
// and the x and y coordinates of (1000, 1000) on the bottom right-hand side corner.
// Our group shape is 250x250pt in size, so every 4pt on the group shape's coordinate plane
// translates to 1pt in the document body's coordinate plane.
// Every shape that we insert will also shrink in size by a factor of 4.
// The change in the shape's "BoundsInPoints" property will reflect this.
Assert.assertEquals(new Rectangle2D.Float(175f, 275f, 25f, 25f), shape.getBoundsInPoints());
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
// Insert a shape and place it outside of the bounds of the group shape's containing block.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(1000.0);
shape.setTop(1000.0);
}
group.appendChild(shape);
// The group shape's footprint in the document body has increased, but the containing block remains the same.
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
Assert.assertEquals(new Rectangle2D.Float(250f, 350f, 25f, 25f), shape.getBoundsInPoints());
doc.save(getArtifactsDir() + "Shape.Bounds.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
value - The location and size of the containing block of the shape.public java.awt.geom.Rectangle2D.Float getBoundsInPoints()
Remarks:
The returned bounds do not include the rotation of this shape or the rotation of the parent group shape, if any.
Examples:
Shows how to verify shape containing block boundaries.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.LINE, RelativeHorizontalPosition.LEFT_MARGIN, 50.0,
RelativeVerticalPosition.TOP_MARGIN, 50.0, 100.0, 100.0, WrapType.NONE);
shape.setStrokeColor(Color.ORANGE);
// Even though the line itself takes up little space on the document page,
// it occupies a rectangular containing block, the size of which we can determine using the "Bounds" properties.
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBounds());
Assert.assertEquals(new Rectangle2D.Float(50f, 50f, 100f, 100f), shape.getBoundsInPoints());
// Create a group shape, and then set the size of its containing block using the "Bounds" property.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(0f, 100f, 250f, 250f));
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
// Create a rectangle, verify the size of its bounding block, and then add it to the group shape.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
Assert.assertEquals(new Rectangle2D.Float(700f, 700f, 100f, 100f), shape.getBoundsInPoints());
group.appendChild(shape);
// The group shape's coordinate plane has its origin on the top left-hand side corner of its containing block,
// and the x and y coordinates of (1000, 1000) on the bottom right-hand side corner.
// Our group shape is 250x250pt in size, so every 4pt on the group shape's coordinate plane
// translates to 1pt in the document body's coordinate plane.
// Every shape that we insert will also shrink in size by a factor of 4.
// The change in the shape's "BoundsInPoints" property will reflect this.
Assert.assertEquals(new Rectangle2D.Float(175f, 275f, 25f, 25f), shape.getBoundsInPoints());
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
// Insert a shape and place it outside of the bounds of the group shape's containing block.
shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(1000.0);
shape.setTop(1000.0);
}
group.appendChild(shape);
// The group shape's footprint in the document body has increased, but the containing block remains the same.
Assert.assertEquals(new Rectangle2D.Float(0f, 100f, 250f, 250f), group.getBoundsInPoints());
Assert.assertEquals(new Rectangle2D.Float(250f, 350f, 25f, 25f), shape.getBoundsInPoints());
doc.save(getArtifactsDir() + "Shape.Bounds.docx");
public java.awt.geom.Rectangle2D.Float getBoundsWithEffects()
Examples:
Shows how to check how a shape's bounds are affected by shape effects.
Document doc = new Document(getMyDir() + "Shape shadow effect.docx");
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
Shape secondShape = shapeList.get(1);
// The two shapes are identical in terms of dimensions and shape type.
Assert.assertEquals(firstShape.getWidth(), secondShape.getWidth());
Assert.assertEquals(firstShape.getHeight(), secondShape.getHeight());
Assert.assertEquals(firstShape.getShapeType(), secondShape.getShapeType());
// The first shape has no effects, and the second one has a shadow and thick outline.
// These effects make the size of the second shape's silhouette bigger than that of the first.
// Even though the rectangle's size shows up when we click on these shapes in Microsoft Word,
// the visible outer bounds of the second shape are affected by the shadow and outline and thus are bigger.
// We can use the "AdjustWithEffects" method to see the true size of the shape.
Assert.assertEquals(0.0, firstShape.getStrokeWeight());
Assert.assertEquals(20.0, secondShape.getStrokeWeight());
Assert.assertFalse(firstShape.getShadowEnabled());
Assert.assertTrue(secondShape.getShadowEnabled());
Shape shape = firstShape;
// Run this method to get the size of the rectangle adjusted for all our shape effects.
Rectangle2D.Float rectangleFOut = shape.adjustWithEffects(new Rectangle2D.Float(200f, 200f, 1000f, 1000f));
// Since the shape has no border-changing effects, its boundary dimensions are unaffected.
Assert.assertEquals(200.0, rectangleFOut.getX());
Assert.assertEquals(200.0, rectangleFOut.getY());
Assert.assertEquals(1000.0, rectangleFOut.getWidth());
Assert.assertEquals(1000.0, rectangleFOut.getHeight());
// Verify the final extent of the first shape, in points.
Assert.assertEquals(0.0, shape.getBoundsWithEffects().getX());
Assert.assertEquals(0.0, shape.getBoundsWithEffects().getY());
Assert.assertEquals(147.0, shape.getBoundsWithEffects().getWidth());
Assert.assertEquals(147.0, shape.getBoundsWithEffects().getHeight());
shape = secondShape;
rectangleFOut = shape.adjustWithEffects(new Rectangle2D.Float(200f, 200f, 1000f, 1000f));
// The shape effects have moved the apparent top left corner of the shape slightly.
Assert.assertEquals(171.5, rectangleFOut.getX());
Assert.assertEquals(167.0, rectangleFOut.getY());
// The effects have also affected the visible dimensions of the shape.
Assert.assertEquals(1045.0, rectangleFOut.getWidth());
Assert.assertEquals(1133.5, rectangleFOut.getHeight());
// The effects have also affected the visible bounds of the shape.
Assert.assertEquals(-28.5, shape.getBoundsWithEffects().getX());
Assert.assertEquals(-33.0, shape.getBoundsWithEffects().getY());
Assert.assertEquals(192.0, shape.getBoundsWithEffects().getWidth());
Assert.assertEquals(280.5, shape.getBoundsWithEffects().getHeight());
public java.awt.geom.Rectangle2D.Float adjustWithEffects(java.awt.geom.Rectangle2D.Float source)
Examples:
Shows how to check how a shape's bounds are affected by shape effects.
Document doc = new Document(getMyDir() + "Shape shadow effect.docx");
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(shapeList.size(), 2);
Shape firstShape = shapeList.get(0);
Shape secondShape = shapeList.get(1);
// The two shapes are identical in terms of dimensions and shape type.
Assert.assertEquals(firstShape.getWidth(), secondShape.getWidth());
Assert.assertEquals(firstShape.getHeight(), secondShape.getHeight());
Assert.assertEquals(firstShape.getShapeType(), secondShape.getShapeType());
// The first shape has no effects, and the second one has a shadow and thick outline.
// These effects make the size of the second shape's silhouette bigger than that of the first.
// Even though the rectangle's size shows up when we click on these shapes in Microsoft Word,
// the visible outer bounds of the second shape are affected by the shadow and outline and thus are bigger.
// We can use the "AdjustWithEffects" method to see the true size of the shape.
Assert.assertEquals(0.0, firstShape.getStrokeWeight());
Assert.assertEquals(20.0, secondShape.getStrokeWeight());
Assert.assertFalse(firstShape.getShadowEnabled());
Assert.assertTrue(secondShape.getShadowEnabled());
Shape shape = firstShape;
// Run this method to get the size of the rectangle adjusted for all our shape effects.
Rectangle2D.Float rectangleFOut = shape.adjustWithEffects(new Rectangle2D.Float(200f, 200f, 1000f, 1000f));
// Since the shape has no border-changing effects, its boundary dimensions are unaffected.
Assert.assertEquals(200.0, rectangleFOut.getX());
Assert.assertEquals(200.0, rectangleFOut.getY());
Assert.assertEquals(1000.0, rectangleFOut.getWidth());
Assert.assertEquals(1000.0, rectangleFOut.getHeight());
// Verify the final extent of the first shape, in points.
Assert.assertEquals(0.0, shape.getBoundsWithEffects().getX());
Assert.assertEquals(0.0, shape.getBoundsWithEffects().getY());
Assert.assertEquals(147.0, shape.getBoundsWithEffects().getWidth());
Assert.assertEquals(147.0, shape.getBoundsWithEffects().getHeight());
shape = secondShape;
rectangleFOut = shape.adjustWithEffects(new Rectangle2D.Float(200f, 200f, 1000f, 1000f));
// The shape effects have moved the apparent top left corner of the shape slightly.
Assert.assertEquals(171.5, rectangleFOut.getX());
Assert.assertEquals(167.0, rectangleFOut.getY());
// The effects have also affected the visible dimensions of the shape.
Assert.assertEquals(1045.0, rectangleFOut.getWidth());
Assert.assertEquals(1133.5, rectangleFOut.getHeight());
// The effects have also affected the visible bounds of the shape.
Assert.assertEquals(-28.5, shape.getBoundsWithEffects().getX());
Assert.assertEquals(-33.0, shape.getBoundsWithEffects().getY());
Assert.assertEquals(192.0, shape.getBoundsWithEffects().getWidth());
Assert.assertEquals(280.5, shape.getBoundsWithEffects().getHeight());
public int getShapeType()
Examples:
Shows how to create a group of shapes, and print its contents using a document visitor.
public void groupOfShapes() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// If you need to create "NonPrimitive" shapes, such as SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped,
// TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, DiagonalCornersRounded
// please use DocumentBuilder.InsertShape methods.
Shape balloon = new Shape(doc, ShapeType.BALLOON);
balloon.setWidth(200.0);
balloon.setHeight(200.0);
balloon.setStrokeColor(Color.RED);
Shape cube = new Shape(doc, ShapeType.CUBE);
cube.setWidth(100.0);
cube.setHeight(100.0);
cube.setStrokeColor(Color.BLUE);
GroupShape group = new GroupShape(doc);
group.appendChild(balloon);
group.appendChild(cube);
Assert.assertTrue(group.isGroup());
builder.insertNode(group);
ShapeInfoPrinter printer = new ShapeInfoPrinter();
group.accept(printer);
System.out.println(printer.getText());
}
/// <summary>
/// Prints the contents of a visited shape group to the console.
/// </summary>
public static class ShapeInfoPrinter extends DocumentVisitor {
public ShapeInfoPrinter() {
mBuilder = new StringBuilder();
}
public String getText() {
return mBuilder.toString();
}
public int visitGroupShapeStart(final GroupShape groupShape) {
mBuilder.append("Shape group started:\r\n");
return VisitorAction.CONTINUE;
}
public int visitGroupShapeEnd(final GroupShape groupShape) {
mBuilder.append("End of shape group\r\n");
return VisitorAction.CONTINUE;
}
public int visitShapeStart(final Shape shape) {
mBuilder.append("\tShape - " + shape.getShapeType() + ":\r\n");
mBuilder.append("\t\tWidth: " + shape.getWidth() + "\r\n");
mBuilder.append("\t\tHeight: " + shape.getHeight() + "\r\n");
mBuilder.append("\t\tStroke color: " + shape.getStroke().getColor() + "\r\n");
mBuilder.append("\t\tFill color: " + shape.getFill().getForeColor() + "\r\n");
return VisitorAction.CONTINUE;
}
public int visitShapeEnd(final Shape shape) {
mBuilder.append("\tEnd of shape\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
}
ShapeType constants.public byte getMarkupLanguage()
Examples:
Shows how to verify a shape's size and markup language.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Transparent background logo.png");
Assert.assertEquals(ShapeMarkupLanguage.DML, shape.getMarkupLanguage());
Assert.assertEquals(new Point2D.Float(300f, 300f), shape.getSizeInPoints());
ShapeMarkupLanguage constants.public java.awt.geom.Point2D.Float getSizeInPoints()
Remarks:
Point2D.Float is used as return type because we need in float dimension values here. One should to assume that Point2D's x == width and y == height.
Examples:
Shows how to verify a shape's size and markup language.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(getImageDir() + "Transparent background logo.png");
Assert.assertEquals(ShapeMarkupLanguage.DML, shape.getMarkupLanguage());
Assert.assertEquals(new Point2D.Float(300f, 300f), shape.getSizeInPoints());
public int getFlipOrientation()
Remarks:
The default value is FlipOrientation.NONE.
Examples:
Shows how to flip a shape on an axis.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert an image shape and leave its orientation in its default state.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
Assert.assertEquals(FlipOrientation.NONE, shape.getFlipOrientation());
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 250.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the second shape on the y-axis,
// making it into a horizontal mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.HORIZONTAL);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 250.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the third shape on the x-axis,
// making it into a vertical mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.VERTICAL);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 250.0,
RelativeVerticalPosition.TOP_MARGIN, 250.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the fourth shape on both the x and y axes,
// making it into a horizontal and vertical mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.BOTH);
doc.save(getArtifactsDir() + "Shape.FlipShapeOrientation.docx");
int value. The returned value is a bitwise combination of FlipOrientation constants.public void setFlipOrientation(int value)
Remarks:
The default value is FlipOrientation.NONE.
Examples:
Shows how to flip a shape on an axis.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert an image shape and leave its orientation in its default state.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
Assert.assertEquals(FlipOrientation.NONE, shape.getFlipOrientation());
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 250.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the second shape on the y-axis,
// making it into a horizontal mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.HORIZONTAL);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 100.0,
RelativeVerticalPosition.TOP_MARGIN, 250.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the third shape on the x-axis,
// making it into a vertical mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.VERTICAL);
shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 250.0,
RelativeVerticalPosition.TOP_MARGIN, 250.0, 100.0, 100.0, WrapType.NONE);
shape.getImageData().setImage(getImageDir() + "Logo.jpg");
// Set the "FlipOrientation" property to "FlipOrientation.Horizontal" to flip the fourth shape on both the x and y axes,
// making it into a horizontal and vertical mirror image of the first shape.
shape.setFlipOrientation(FlipOrientation.BOTH);
doc.save(getArtifactsDir() + "Shape.FlipShapeOrientation.docx");
value - The corresponding int value. The value must be a bitwise combination of FlipOrientation constants.public int getRelativeHorizontalPosition()
Remarks:
The default value is RelativeHorizontalPosition.COLUMN.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
int value. The returned value is one of RelativeHorizontalPosition constants.public void setRelativeHorizontalPosition(int value)
Remarks:
The default value is RelativeHorizontalPosition.COLUMN.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
value - The corresponding int value. The value must be one of RelativeHorizontalPosition constants.public int getRelativeVerticalPosition()
Remarks:
The default value is RelativeVerticalPosition.PARAGRAPH.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
int value. The returned value is one of RelativeVerticalPosition constants.public void setRelativeVerticalPosition(int value)
Remarks:
The default value is RelativeVerticalPosition.PARAGRAPH.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
value - The corresponding int value. The value must be one of RelativeVerticalPosition constants.public int getRelativeHorizontalSize()
Remarks:
The default value is RelativeHorizontalSize.
Has effect only if getWidthRelative() / setWidthRelative(float) is set.
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
RelativeHorizontalSize constants.public void setRelativeHorizontalSize(int value)
Remarks:
The default value is RelativeHorizontalSize.
Has effect only if getWidthRelative() / setWidthRelative(float) is set.
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value of shape's relative size in horizontal direction. The value must be one of RelativeHorizontalSize constants.public int getRelativeVerticalSize()
Remarks:
The default value is RelativeVerticalSize.MARGIN.
Has effect only if getHeightRelative() / setHeightRelative(float) is set.
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
RelativeVerticalSize constants.public void setRelativeVerticalSize(int value)
Remarks:
The default value is RelativeVerticalSize.MARGIN.
Has effect only if getHeightRelative() / setHeightRelative(float) is set.
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value of shape's relative size in vertical direction. The value must be one of RelativeVerticalSize constants.public float getLeftRelative()
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
public void setLeftRelative(float value)
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value that represents shape's relative left position in percent.public float getTopRelative()
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
public void setTopRelative(float value)
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value that represents shape's relative top position in percent.public float getHeightRelative()
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
public void setHeightRelative(float value)
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value that represents the percentage of shape's relative height.public float getWidthRelative()
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
public void setWidthRelative(float value)
Examples:
Shows how to set relative size and position.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Adding a simple shape with absolute size and position.
Shape shape = builder.insertShape(ShapeType.RECTANGLE, 100.0, 40.0);
// Set WrapType to WrapType.None since Inline shapes are automatically converted to absolute units.
shape.setWrapType(WrapType.NONE);
// Checking and setting the relative horizontal size.
if (shape.getRelativeHorizontalSize() == RelativeHorizontalSize.DEFAULT)
{
// Setting the horizontal size binding to Margin.
shape.setRelativeHorizontalSize(RelativeHorizontalSize.MARGIN);
// Setting the width to 50% of Margin width.
shape.setWidthRelative(50f);
}
// Checking and setting the relative vertical size.
if (shape.getRelativeVerticalSize() == RelativeVerticalSize.DEFAULT)
{
// Setting the vertical size binding to Margin.
shape.setRelativeVerticalSize(RelativeVerticalSize.MARGIN);
// Setting the heigh to 30% of Margin height.
shape.setHeightRelative(30f);
}
// Checking and setting the relative vertical position.
if (shape.getRelativeVerticalPosition() == RelativeVerticalPosition.PARAGRAPH)
{
// etting the position binding to TopMargin.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.TOP_MARGIN);
// Setting relative Top to 30% of TopMargin position.
shape.setTopRelative(30f);
}
// Checking and setting the relative horizontal position.
if (shape.getRelativeHorizontalPosition() == RelativeHorizontalPosition.DEFAULT)
{
// Setting the position binding to RightMargin.
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.RIGHT_MARGIN);
// The position relative value can be negative.
shape.setLeftRelative(-260);
}
doc.save(getArtifactsDir() + "Shape.RelativeSizeAndPosition.docx");
value - The value that represents the percentage of shape's relative width.public int getHorizontalAlignment()
Remarks:
The default value is HorizontalAlignment.NONE.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
int value. The returned value is one of HorizontalAlignment constants.public void setHorizontalAlignment(int value)
Remarks:
The default value is HorizontalAlignment.NONE.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
value - The corresponding int value. The value must be one of HorizontalAlignment constants.public int getVerticalAlignment()
Remarks:
The default value is VerticalAlignment.NONE.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
int value. The returned value is one of VerticalAlignment constants.public void setVerticalAlignment(int value)
Remarks:
The default value is VerticalAlignment.NONE.
Has effect only for top level floating shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
value - The corresponding int value. The value must be one of VerticalAlignment constants.public int getWrapType()
Remarks:
The default value is WrapType.NONE.
Has effect only for top level shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
Shows how to create and format a text box.
Document doc = new Document();
// Create a floating text box.
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setWrapType(WrapType.NONE);
textBox.setHeight(50.0);
textBox.setWidth(200.0);
// Set the horizontal, and vertical alignment of the text inside the shape.
textBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
textBox.setVerticalAlignment(VerticalAlignment.TOP);
// Add a paragraph to the text box and add a run of text that the text box will display.
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Run run = new Run(doc);
run.setText("Hello world!");
para.appendChild(run);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
doc.save(getArtifactsDir() + "Shape.CreateTextBox.docx");
int value. The returned value is one of WrapType constants.public void setWrapType(int value)
Remarks:
The default value is WrapType.NONE.
Has effect only for top level shapes.
Examples:
Shows how to insert a floating image to the center of a page.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.insertImage(getImageDir() + "Logo.jpg");
shape.setWrapType(WrapType.NONE);
shape.setBehindText(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "Image.CreateFloatingPageCenter.docx");
Shows how to create and format a text box.
Document doc = new Document();
// Create a floating text box.
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setWrapType(WrapType.NONE);
textBox.setHeight(50.0);
textBox.setWidth(200.0);
// Set the horizontal, and vertical alignment of the text inside the shape.
textBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
textBox.setVerticalAlignment(VerticalAlignment.TOP);
// Add a paragraph to the text box and add a run of text that the text box will display.
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Run run = new Run(doc);
run.setText("Hello world!");
para.appendChild(run);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
doc.save(getArtifactsDir() + "Shape.CreateTextBox.docx");
value - The corresponding int value. The value must be one of WrapType constants.public int getWrapSide()
Remarks:
The default value is WrapSide.BOTH.
Has effect only for top level shapes.
Examples:
Shows how to replace all textbox shapes with image shapes.
Document doc = new Document(getMyDir() + "Textboxes in drawing canvas.docx");
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(3, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX));
Assert.assertEquals(1, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE));
for (Shape shape : shapeList) {
if (((shape.getShapeType()) == (ShapeType.TEXT_BOX))) {
Shape replacementShape = new Shape(doc, ShapeType.IMAGE);
replacementShape.getImageData().setImage(getImageDir() + "Logo.jpg");
replacementShape.setLeft(shape.getLeft());
replacementShape.setTop(shape.getTop());
replacementShape.setWidth(shape.getWidth());
replacementShape.setHeight(shape.getHeight());
replacementShape.setRelativeHorizontalPosition(shape.getRelativeHorizontalPosition());
replacementShape.setRelativeVerticalPosition(shape.getRelativeVerticalPosition());
replacementShape.setHorizontalAlignment(shape.getHorizontalAlignment());
replacementShape.setVerticalAlignment(shape.getVerticalAlignment());
replacementShape.setWrapType(shape.getWrapType());
replacementShape.setWrapSide(shape.getWrapSide());
shape.getParentNode().insertAfter(replacementShape, shape);
shape.remove();
}
}
shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(0, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX));
Assert.assertEquals(4, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE));
doc.save(getArtifactsDir() + "Shape.ReplaceTextboxesWithImages.docx");
int value. The returned value is one of WrapSide constants.public void setWrapSide(int value)
Remarks:
The default value is WrapSide.BOTH.
Has effect only for top level shapes.
Examples:
Shows how to replace all textbox shapes with image shapes.
Document doc = new Document(getMyDir() + "Textboxes in drawing canvas.docx");
List<Shape> shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(3, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX));
Assert.assertEquals(1, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE));
for (Shape shape : shapeList) {
if (((shape.getShapeType()) == (ShapeType.TEXT_BOX))) {
Shape replacementShape = new Shape(doc, ShapeType.IMAGE);
replacementShape.getImageData().setImage(getImageDir() + "Logo.jpg");
replacementShape.setLeft(shape.getLeft());
replacementShape.setTop(shape.getTop());
replacementShape.setWidth(shape.getWidth());
replacementShape.setHeight(shape.getHeight());
replacementShape.setRelativeHorizontalPosition(shape.getRelativeHorizontalPosition());
replacementShape.setRelativeVerticalPosition(shape.getRelativeVerticalPosition());
replacementShape.setHorizontalAlignment(shape.getHorizontalAlignment());
replacementShape.setVerticalAlignment(shape.getVerticalAlignment());
replacementShape.setWrapType(shape.getWrapType());
replacementShape.setWrapSide(shape.getWrapSide());
shape.getParentNode().insertAfter(replacementShape, shape);
shape.remove();
}
}
shapeList = Arrays.stream(doc.getChildNodes(NodeType.SHAPE, true).toArray())
.filter(Shape.class::isInstance)
.map(Shape.class::cast)
.collect(Collectors.toList());
Assert.assertEquals(0, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.TEXT_BOX));
Assert.assertEquals(4, IterableUtils.countMatches(shapeList, s -> s.getShapeType() == ShapeType.IMAGE));
doc.save(getArtifactsDir() + "Shape.ReplaceTextboxesWithImages.docx");
value - The corresponding int value. The value must be one of WrapSide constants.public java.awt.Point getCoordOrigin()
Remarks:
The default value is (0,0).
Examples:
Shows how to translate the x and y coordinate location on a shape's coordinate plane to a location on the parent shape's coordinate plane.
Document doc = new Document();
// Insert a group shape, and place it 100 points below and to the right of
// the document's x and Y coordinate origin point.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(100f, 100f, 500f, 500f));
// Use the "LocalToParent" method to determine that (0, 0) on the group's internal x and y coordinates
// lies on (100, 100) of its parent shape's coordinate system. The group shape's parent is the document itself.
Assert.assertEquals(new Point2D.Float(100f, 100f), group.localToParent(new Point2D.Float(0f, 0f)));
// By default, a shape's internal coordinate plane has the top left corner at (0, 0),
// and the bottom right corner at (1000, 1000). Due to its size, our group shape covers an area of 500pt x 500pt
// in the document's plane. This means that a movement of 1pt on the document's coordinate plane will translate
// to a movement of 2pts on the group shape's coordinate plane.
Assert.assertEquals(new Point2D.Float(150f, 150f), group.localToParent(new Point2D.Float(100f, 100f)));
Assert.assertEquals(new Point2D.Float(200f, 200f), group.localToParent(new Point2D.Float(200f, 200f)));
Assert.assertEquals(new Point2D.Float(250f, 250f), group.localToParent(new Point2D.Float(300f, 300f)));
// Move the group shape's x and y axis origin from the top left corner to the center.
// This will offset the group's internal coordinates relative to the document's coordinates even further.
group.setCoordOrigin(new Point(-250, -250));
Assert.assertEquals(new Point2D.Float(375f, 375f), group.localToParent(new Point2D.Float(300f, 300f)));
// Changing the scale of the coordinate plane will also affect relative locations.
group.setCoordSize(new Dimension(500, 500));
Assert.assertEquals(new Point2D.Float(650f, 650f), group.localToParent(new Point2D.Float(300f, 300f)));
// If we wish to add a shape to this group while defining its location based on a location in the document,
// we will need to first confirm a location in the group shape that will match the document's location.
Assert.assertEquals(new Point2D.Float(700f, 700f), group.localToParent(new Point2D.Float(350f, 350f)));
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
group.appendChild(shape);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
doc.save(getArtifactsDir() + "Shape.LocalToParent.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
Point value.public void setCoordOrigin(java.awt.Point value)
Remarks:
The default value is (0,0).
Examples:
Shows how to translate the x and y coordinate location on a shape's coordinate plane to a location on the parent shape's coordinate plane.
Document doc = new Document();
// Insert a group shape, and place it 100 points below and to the right of
// the document's x and Y coordinate origin point.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(100f, 100f, 500f, 500f));
// Use the "LocalToParent" method to determine that (0, 0) on the group's internal x and y coordinates
// lies on (100, 100) of its parent shape's coordinate system. The group shape's parent is the document itself.
Assert.assertEquals(new Point2D.Float(100f, 100f), group.localToParent(new Point2D.Float(0f, 0f)));
// By default, a shape's internal coordinate plane has the top left corner at (0, 0),
// and the bottom right corner at (1000, 1000). Due to its size, our group shape covers an area of 500pt x 500pt
// in the document's plane. This means that a movement of 1pt on the document's coordinate plane will translate
// to a movement of 2pts on the group shape's coordinate plane.
Assert.assertEquals(new Point2D.Float(150f, 150f), group.localToParent(new Point2D.Float(100f, 100f)));
Assert.assertEquals(new Point2D.Float(200f, 200f), group.localToParent(new Point2D.Float(200f, 200f)));
Assert.assertEquals(new Point2D.Float(250f, 250f), group.localToParent(new Point2D.Float(300f, 300f)));
// Move the group shape's x and y axis origin from the top left corner to the center.
// This will offset the group's internal coordinates relative to the document's coordinates even further.
group.setCoordOrigin(new Point(-250, -250));
Assert.assertEquals(new Point2D.Float(375f, 375f), group.localToParent(new Point2D.Float(300f, 300f)));
// Changing the scale of the coordinate plane will also affect relative locations.
group.setCoordSize(new Dimension(500, 500));
Assert.assertEquals(new Point2D.Float(650f, 650f), group.localToParent(new Point2D.Float(300f, 300f)));
// If we wish to add a shape to this group while defining its location based on a location in the document,
// we will need to first confirm a location in the group shape that will match the document's location.
Assert.assertEquals(new Point2D.Float(700f, 700f), group.localToParent(new Point2D.Float(350f, 350f)));
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
group.appendChild(shape);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
doc.save(getArtifactsDir() + "Shape.LocalToParent.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
value - The corresponding Point value.public java.awt.Dimension getCoordSize()
Remarks:
The default value is (1000, 1000).
Examples:
Shows how to translate the x and y coordinate location on a shape's coordinate plane to a location on the parent shape's coordinate plane.
Document doc = new Document();
// Insert a group shape, and place it 100 points below and to the right of
// the document's x and Y coordinate origin point.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(100f, 100f, 500f, 500f));
// Use the "LocalToParent" method to determine that (0, 0) on the group's internal x and y coordinates
// lies on (100, 100) of its parent shape's coordinate system. The group shape's parent is the document itself.
Assert.assertEquals(new Point2D.Float(100f, 100f), group.localToParent(new Point2D.Float(0f, 0f)));
// By default, a shape's internal coordinate plane has the top left corner at (0, 0),
// and the bottom right corner at (1000, 1000). Due to its size, our group shape covers an area of 500pt x 500pt
// in the document's plane. This means that a movement of 1pt on the document's coordinate plane will translate
// to a movement of 2pts on the group shape's coordinate plane.
Assert.assertEquals(new Point2D.Float(150f, 150f), group.localToParent(new Point2D.Float(100f, 100f)));
Assert.assertEquals(new Point2D.Float(200f, 200f), group.localToParent(new Point2D.Float(200f, 200f)));
Assert.assertEquals(new Point2D.Float(250f, 250f), group.localToParent(new Point2D.Float(300f, 300f)));
// Move the group shape's x and y axis origin from the top left corner to the center.
// This will offset the group's internal coordinates relative to the document's coordinates even further.
group.setCoordOrigin(new Point(-250, -250));
Assert.assertEquals(new Point2D.Float(375f, 375f), group.localToParent(new Point2D.Float(300f, 300f)));
// Changing the scale of the coordinate plane will also affect relative locations.
group.setCoordSize(new Dimension(500, 500));
Assert.assertEquals(new Point2D.Float(650f, 650f), group.localToParent(new Point2D.Float(300f, 300f)));
// If we wish to add a shape to this group while defining its location based on a location in the document,
// we will need to first confirm a location in the group shape that will match the document's location.
Assert.assertEquals(new Point2D.Float(700f, 700f), group.localToParent(new Point2D.Float(350f, 350f)));
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
group.appendChild(shape);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
doc.save(getArtifactsDir() + "Shape.LocalToParent.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
Dimension value.public void setCoordSize(java.awt.Dimension value)
Remarks:
The default value is (1000, 1000).
Examples:
Shows how to translate the x and y coordinate location on a shape's coordinate plane to a location on the parent shape's coordinate plane.
Document doc = new Document();
// Insert a group shape, and place it 100 points below and to the right of
// the document's x and Y coordinate origin point.
GroupShape group = new GroupShape(doc);
group.setBounds(new Rectangle2D.Float(100f, 100f, 500f, 500f));
// Use the "LocalToParent" method to determine that (0, 0) on the group's internal x and y coordinates
// lies on (100, 100) of its parent shape's coordinate system. The group shape's parent is the document itself.
Assert.assertEquals(new Point2D.Float(100f, 100f), group.localToParent(new Point2D.Float(0f, 0f)));
// By default, a shape's internal coordinate plane has the top left corner at (0, 0),
// and the bottom right corner at (1000, 1000). Due to its size, our group shape covers an area of 500pt x 500pt
// in the document's plane. This means that a movement of 1pt on the document's coordinate plane will translate
// to a movement of 2pts on the group shape's coordinate plane.
Assert.assertEquals(new Point2D.Float(150f, 150f), group.localToParent(new Point2D.Float(100f, 100f)));
Assert.assertEquals(new Point2D.Float(200f, 200f), group.localToParent(new Point2D.Float(200f, 200f)));
Assert.assertEquals(new Point2D.Float(250f, 250f), group.localToParent(new Point2D.Float(300f, 300f)));
// Move the group shape's x and y axis origin from the top left corner to the center.
// This will offset the group's internal coordinates relative to the document's coordinates even further.
group.setCoordOrigin(new Point(-250, -250));
Assert.assertEquals(new Point2D.Float(375f, 375f), group.localToParent(new Point2D.Float(300f, 300f)));
// Changing the scale of the coordinate plane will also affect relative locations.
group.setCoordSize(new Dimension(500, 500));
Assert.assertEquals(new Point2D.Float(650f, 650f), group.localToParent(new Point2D.Float(300f, 300f)));
// If we wish to add a shape to this group while defining its location based on a location in the document,
// we will need to first confirm a location in the group shape that will match the document's location.
Assert.assertEquals(new Point2D.Float(700f, 700f), group.localToParent(new Point2D.Float(350f, 350f)));
Shape shape = new Shape(doc, ShapeType.RECTANGLE);
{
shape.setWidth(100.0);
shape.setHeight(100.0);
shape.setLeft(700.0);
shape.setTop(700.0);
}
group.appendChild(shape);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(group);
doc.save(getArtifactsDir() + "Shape.LocalToParent.docx");
Shows how to create and populate a group shape.
Document doc = new Document();
// Create a group shape. A group shape can display a collection of child shape nodes.
// In Microsoft Word, clicking within the group shape's boundary or on one of the group shape's child shapes will
// select all the other child shapes within this group and allow us to scale and move all the shapes at once.
GroupShape group = new GroupShape(doc);
Assert.assertEquals(WrapType.NONE, group.getWrapType());
// Create a 400pt x 400pt group shape and place it at the document's floating shape coordinate origin.
group.setBounds(new Rectangle2D.Float(0f, 0f, 400f, 400f));
// Set the group's internal coordinate plane size to 500 x 500pt.
// The top left corner of the group will have an x and y coordinate of (0, 0),
// and the bottom right corner will have an x and y coordinate of (500, 500).
group.setCoordSize(new Dimension(500, 500));
// Set the coordinates of the top left corner of the group to (-250, -250).
// The group's center will now have an x and y coordinate value of (0, 0),
// and the bottom right corner will be at (250, 250).
group.setCoordOrigin(new Point(-250, -250));
Shape rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(group.getCoordSize().width);
rectangleShape.setHeight(group.getCoordSize().height);
rectangleShape.setLeft(group.getCoordOrigin().x);
rectangleShape.setTop(group.getCoordOrigin().y);
// Create a rectangle that will display the boundary of this group shape and add it to the group.
group.appendChild(rectangleShape);
// Once a shape is a part of a group shape, we can access it as a child node and then modify it.
((Shape) group.getChild(NodeType.SHAPE, 0, true)).getStroke().setDashStyle(DashStyle.DASH);
Shape starShape = new Shape(doc, ShapeType.STAR);
starShape.setWidth(20.0);
starShape.setHeight(20.0);
starShape.setLeft(-10);
starShape.setTop(-10);
starShape.setFillColor(Color.RED);
// Create a small red star and insert it into the group.
// Line up the shape with the group's coordinate origin, which we have moved to the center.
group.appendChild(starShape);
rectangleShape = new Shape(doc, ShapeType.RECTANGLE);
rectangleShape.setWidth(250.0);
rectangleShape.setHeight(250.0);
rectangleShape.setLeft(-250);
rectangleShape.setTop(-250);
rectangleShape.setFillColor(Color.BLUE);
// Insert a rectangle, and then insert a slightly smaller rectangle in the same place with an image.
// Newer shapes that we add to the group overlap older shapes. The light blue rectangle will partially overlap the red star,
// and then the shape with the image will overlap the light blue rectangle, using it as a frame.
// We cannot use the "ZOrder" properties of shapes to manipulate their arrangement within a group shape.
group.appendChild(rectangleShape);
Shape imageShape = new Shape(doc, ShapeType.IMAGE);
imageShape.setWidth(200.0);
imageShape.setHeight(200.0);
imageShape.setLeft(-225);
imageShape.setTop(-225);
group.appendChild(imageShape);
((Shape) group.getChild(NodeType.SHAPE, 3, true)).getImageData().setImage(getImageDir() + "Logo.jpg");
Shape textboxShape = new Shape(doc, ShapeType.TEXT_BOX);
textboxShape.setWidth(200.0);
textboxShape.setHeight(50.0);
textboxShape.setLeft(group.getCoordSize().width + new Point(group.getCoordOrigin()).x - 200);
textboxShape.setTop(group.getCoordSize().height + new Point(group.getCoordOrigin()).y);
// Insert a text box into the group shape. Set the "Left" property so that the text box's right edge
// touches the right boundary of the group shape. Set the "Top" property so that the text box sits outside
// the boundary of the group shape, with its top size lined up along the group shape's bottom margin.
group.appendChild(textboxShape);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertNode(group);
builder.moveTo(((Shape) group.getChild(NodeType.SHAPE, 4, true)).appendChild(new Paragraph(doc)));
builder.write("Hello world!");
doc.save(getArtifactsDir() + "Shape.GroupShape.docx");
value - The corresponding Dimension value.public Font getFont()
Examples:
Shows how to insert a text box, and set the font of its contents.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world!");
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, 300.0, 50.0);
builder.moveTo(shape.getLastParagraph());
builder.write("This text is inside the text box.");
// Set the "Hidden" property of the shape's "Font" object to "true" to hide the text box from sight
// and collapse the space that it would normally occupy.
// Set the "Hidden" property of the shape's "Font" object to "false" to leave the text box visible.
shape.getFont().setHidden(hideShape);
// If the shape is visible, we will modify its appearance via the font object.
if (!hideShape) {
shape.getFont().setHighlightColor(Color.LIGHT_GRAY);
shape.getFont().setColor(Color.RED);
shape.getFont().setUnderline(Underline.DASH);
}
// Move the builder out of the text box back into the main document.
builder.moveTo(shape.getParentParagraph());
builder.writeln("\nThis text is outside the text box.");
doc.save(getArtifactsDir() + "Shape.Font.docx");
Font value.public boolean isSignatureLine()
SignatureLine.
Examples:
Shows how to create a line for a signature and insert it into a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
SignatureLineOptions options = new SignatureLineOptions();
{
options.setAllowComments(true);
options.setDefaultInstructions(true);
options.setEmail("john.doe@management.com");
options.setInstructions("Please sign here");
options.setShowDate(true);
options.setSigner("John Doe");
options.setSignerTitle("Senior Manager");
}
// Insert a shape that will contain a signature line, whose appearance we will
// customize using the "SignatureLineOptions" object we have created above.
// If we insert a shape whose coordinates originate at the bottom right hand corner of the page,
// we will need to supply negative x and y coordinates to bring the shape into view.
Shape shape = builder.insertSignatureLine(options, RelativeHorizontalPosition.RIGHT_MARGIN, -170.0,
RelativeVerticalPosition.BOTTOM_MARGIN, -60.0, WrapType.NONE);
Assert.assertTrue(shape.isSignatureLine());
// Verify the properties of our signature line via its Shape object.
SignatureLine signatureLine = shape.getSignatureLine();
Assert.assertEquals(signatureLine.getEmail(), "john.doe@management.com");
Assert.assertEquals(signatureLine.getSigner(), "John Doe");
Assert.assertEquals(signatureLine.getSignerTitle(), "Senior Manager");
Assert.assertEquals(signatureLine.getInstructions(), "Please sign here");
Assert.assertTrue(signatureLine.getShowDate());
Assert.assertTrue(signatureLine.getAllowComments());
Assert.assertTrue(signatureLine.getDefaultInstructions());
doc.save(getArtifactsDir() + "Shape.SignatureLine.docx");
boolean value.public boolean isLayoutInCell()
Remarks:
The default value is true.
Has effect only for top level shapes, the property getWrapType() / setWrapType(int) of which is set to value other than Inline.
Examples:
Shows how to determine how to display a shape in a table cell.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.setBottomPadding(20.0);
tableStyle.setLeftPadding(10.0);
tableStyle.setRightPadding(10.0);
tableStyle.setTopPadding(20.0);
tableStyle.getBorders().setColor(Color.BLACK);
tableStyle.getBorders().setLineStyle(LineStyle.SINGLE);
table.setStyle(tableStyle);
builder.moveTo(table.getFirstRow().getFirstCell().getFirstParagraph());
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 50.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
// Set the "IsLayoutInCell" property to "true" to display the shape as an inline element inside the cell's paragraph.
// The coordinate origin that will determine the shape's location will be the top left corner of the shape's cell.
// If we re-size the cell, the shape will move to maintain the same position starting from the cell's top left.
// Set the "IsLayoutInCell" property to "false" to display the shape as an independent floating shape.
// The coordinate origin that will determine the shape's location will be the top left corner of the page,
// and the shape will not respond to any re-sizing of its cell.
shape.isLayoutInCell(isLayoutInCell);
// We can only apply the "IsLayoutInCell" property to floating shapes.
shape.setWrapType(WrapType.NONE);
doc.save(getArtifactsDir() + "Shape.LayoutInTableCell.docx");
public void isLayoutInCell(boolean value)
Remarks:
The default value is true.
Has effect only for top level shapes, the property getWrapType() / setWrapType(int) of which is set to value other than Inline.
Examples:
Shows how to determine how to display a shape in a table cell.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.setBottomPadding(20.0);
tableStyle.setLeftPadding(10.0);
tableStyle.setRightPadding(10.0);
tableStyle.setTopPadding(20.0);
tableStyle.getBorders().setColor(Color.BLACK);
tableStyle.getBorders().setLineStyle(LineStyle.SINGLE);
table.setStyle(tableStyle);
builder.moveTo(table.getFirstRow().getFirstCell().getFirstParagraph());
Shape shape = builder.insertShape(ShapeType.RECTANGLE, RelativeHorizontalPosition.LEFT_MARGIN, 50.0,
RelativeVerticalPosition.TOP_MARGIN, 100.0, 100.0, 100.0, WrapType.NONE);
// Set the "IsLayoutInCell" property to "true" to display the shape as an inline element inside the cell's paragraph.
// The coordinate origin that will determine the shape's location will be the top left corner of the shape's cell.
// If we re-size the cell, the shape will move to maintain the same position starting from the cell's top left.
// Set the "IsLayoutInCell" property to "false" to display the shape as an independent floating shape.
// The coordinate origin that will determine the shape's location will be the top left corner of the page,
// and the shape will not respond to any re-sizing of its cell.
shape.isLayoutInCell(isLayoutInCell);
// We can only apply the "IsLayoutInCell" property to floating shapes.
shape.setWrapType(WrapType.NONE);
doc.save(getArtifactsDir() + "Shape.LayoutInTableCell.docx");
value - A flag indicating whether the shape is displayed inside a table or outside of it.public boolean getHidden()
Remarks:
The default value is false.
Examples:
Shows how to hide the shape.
Document doc = new Document(getMyDir() + "Shadow color.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
if (!shape.getHidden())
shape.setHidden(true);
doc.save(getArtifactsDir() + "Shape.Hidden.docx");
public void setHidden(boolean value)
Remarks:
The default value is false.
Examples:
Shows how to hide the shape.
Document doc = new Document(getMyDir() + "Shadow color.docx");
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
if (!shape.getHidden())
shape.setHidden(true);
doc.save(getArtifactsDir() + "Shape.Hidden.docx");
value - A boolean value indicating whether the shape is visible.