使用形状
本主题讨论如何使用Aspose.Words以编程方式处理形状。
Aspose.Words中的形状表示绘图层中的对象,例如AutoShape、textbox、freeform、OLE对象、ActiveX控件或图片。 Word文档可以包含一个或多个不同的形状。 文档的形状由Shape类表示。
使用文档生成器插入形状
您可以使用InsertShape方法将具有指定类型和大小的内联形状以及具有指定位置,大小和文本换行类型的自由浮动形状插入文档。 InsertShape方法允许在文档模型中插入DML形状。 文档必须以支持DML形状的格式保存,否则,这样的节点将被转换为VML形状,同时保存文档。
下面的代码示例演示如何将这些类型的形状插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Free-floating shape insertion. | |
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, | |
RelativeHorizontalPosition.PAGE, 100, | |
RelativeVerticalPosition.PAGE, 100, | |
50, 50, | |
WrapType.NONE); | |
shape.setRotation(30.0); | |
builder.writeln(); | |
//Inline shape insertion. | |
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50); | |
shape.setRotation(30.0); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir, so); |
设置长宽比锁定
使用Aspose.Words,您可以指定是否通过AspectRatioLocked属性锁定形状的宽高比。
下面的代码示例演示如何使用AspectRatioLocked属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(true); | |
// Save the document to disk. | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
doc.save(dataDir); |
在单元格中设置形状布局
您还可以使用IsLayoutInCell属性指定形状是显示在表内部还是表外部。
下面的代码示例演示如何使用IsLayoutInCell属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); | |
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); | |
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); | |
watermark.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.setWidth(300); | |
watermark.setHeight(70); | |
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
watermark.setVerticalAlignment(VerticalAlignment.CENTER); | |
watermark.setRotation(-40); | |
watermark.getFill().setColor(Color.GRAY); | |
watermark.setStrokeColor(Color.GRAY); | |
watermark.getTextPath().setText("watermarkText"); | |
watermark.getTextPath().setFontFamily("Arial"); | |
watermark.setName("WaterMark_0"); | |
watermark.setWrapType(WrapType.NONE); | |
Run run = (Run) doc.getChildNodes(NodeType.RUN, true).get(doc.getChildNodes(NodeType.RUN, true).getCount() - 1); | |
builder.moveTo(run); | |
builder.insertNode(watermark); | |
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010); | |
// Save the document to disk. | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
doc.save(dataDir); |
添加剪下的角
您可以使用Aspose.Words创建剪断角矩形。 形状类型为SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,和DiagonalCornersRounded.
使用这些形状类型的InsertShape方法创建DML形状。 这些类型不能用于创建VML形状。 尝试使用"Shape"类的公共构造函数创建shape会引发"NotSupportedException"异常。
下面的代码示例演示如何将这些类型的形状插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.save(dataDir, so); |
获取实际形状边界点
使用Aspose.WordsAPI,您可以获得包含块的形状的位置和大小(以点为单位),相对于最顶层形状的锚点。 为此,请使用BoundsInPoints属性。
下面的代码示例演示如何使用BoundsInPoints属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(false); | |
System.out.print("\nGets the actual bounds of the shape in points. "); | |
System.out.println(shape.getShapeRenderer().getBoundsInPoints()); |
指定垂直锚点
您可以使用VerticalAnchor属性指定形状内的文本垂直对齐。
下面的代码示例演示如何使用VerticalAnchor属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "VerticalAnchor.docx"); | |
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); | |
int imageIndex = 0; | |
for (Shape textBoxShape : (Iterable<Shape>) shapes) { | |
if (textBoxShape != null) { | |
textBoxShape.getTextBox().setVerticalAnchor(TextBoxAnchor.BOTTOM); | |
} | |
} | |
doc.save(dataDir + "VerticalAnchor_out.docx"); |
检测SmartArt形状
Aspose.Words还允许检测形状是否有SmartArt
对象。 为此,请使用HasSmartArt属性。
下面的代码示例演示如何使用HasSmartArt属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "input.docx"); | |
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); | |
int count = 0; | |
for (Shape textBoxShape : (Iterable<Shape>) shapes) { | |
if (textBoxShape.hasSmartArt()) { | |
count++; | |
} | |
} | |
System.out.println("The document has " + count + " shapes with SmartArt."); |
水平规则格式
您可以使用InsertHorizontalRule方法将水平规则形状插入到文档中。
Aspose.WordsAPI提供HorizontalRuleFormat属性来访问水平规则形状的属性。 HorizontalRuleFormat类公开了基本属性,如高度,颜色,NoShade等。 用于水平规则的格式设置。
下面的代码示例演示如何设置HorizontalRuleFormat:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
Shape shape = builder.insertHorizontalRule(); | |
HorizontalRuleFormat horizontalRuleFormat = shape.getHorizontalRuleFormat(); | |
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER); | |
horizontalRuleFormat.setWidthPercent(70); | |
horizontalRuleFormat.setHeight(3); | |
horizontalRuleFormat.setColor(Color.BLUE); | |
horizontalRuleFormat.setNoShade(true); | |
builder.getDocument().save("HorizontalRuleFormat.docx"); |