public class TextPathAlignment
Example:
public void insertTextPaths() throws Exception {
Document doc = new Document();
// Insert a WordArt object and capture the shape that contains it in a variable
Shape shape = appendWordArt(doc, "Bold & Italic", "Arial", 240.0, 24.0, Color.WHITE, Color.BLACK, ShapeType.TEXT_PLAIN_TEXT);
// View and verify various text formatting settings
shape.getTextPath().setBold(true);
shape.getTextPath().setItalic(true);
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(shape.getTextPath().getSize(), 36.0);
Assert.assertEquals(shape.getTextPath().getText(), "Bold & Italic");
Assert.assertEquals(shape.getShapeType(), ShapeType.TEXT_PLAIN_TEXT);
// Toggle whether or not to display 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.RED, ShapeType.TEXT_PLAIN_TEXT);
shape.getTextPath().setOn(false);
// Apply kerning
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);
// Apply custom spacing, 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);
// Rotate letters 90 degrees to the left, text is still laid out horizontally
shape = appendWordArt(doc, "RotateLetters", "Calibri", 200.0, 36.0, Color.YELLOW, Color.GREEN, ShapeType.TEXT_WAVE);
shape.getTextPath().setRotateLetters(true);
// Set the x-height 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 size of the text will scale to always fit the size of the containing shape, 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 FitShape to false, the size of the text will defy the shape bounds and always keep the size value we set below
// We can also set TextPathAlignment to align the text
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 Shape appendWordArt(Document doc, String text, String textFontFamily, double shapeWidth, double shapeHeight, Color wordArtFill, Color line, int wordArtShapeType) throws Exception {
// Insert a new paragraph
Paragraph para = (Paragraph) doc.getFirstSection().getBody().appendChild(new Paragraph(doc));
// Create an inline Shape, which will serve as a container for our WordArt, and append it to the paragraph
// The shape can only be a valid WordArt shape if the ShapeType assigned here is a WordArt-designated ShapeType
// These types will have "WordArt object" in the description and their enumerator names will start with "Text..."
Shape shape = new Shape(doc, wordArtShapeType);
shape.setWrapType(WrapType.INLINE);
para.appendChild(shape);
// Set the shape's width and height
shape.setWidth(shapeWidth);
shape.setHeight(shapeHeight);
// These color settings will apply to the letters of the displayed WordArt text
shape.setFillColor(wordArtFill);
shape.setStrokeColor(line);
// The WordArt object is accessed here, and we will set the text and font like this
shape.getTextPath().setText(text);
shape.getTextPath().setFontFamily(textFontFamily);
return shape;
}
Field Summary | ||
---|---|---|
static final int | STRETCH | |
Stretch each line of text to fit width.
|
||
static final int | CENTER | |
Center text on width.
|
||
static final int | LEFT | |
Left justify.
|
||
static final int | RIGHT | |
Right justify.
|
||
static final int | LETTER_JUSTIFY | |
Spread letters out to fit width.
|
||
static final int | WORD_JUSTIFY | |
Spread words out to fit width.
|
public static final int STRETCH
public static final int CENTER
public static final int LEFT
public static final int RIGHT
public static final int LETTER_JUSTIFY
public static final int WORD_JUSTIFY