Práce s tvary
Toto téma pojednává o tom, jak pracovat programově s tvary pomocí Aspose.Words.
Tvary v Aspose.Words představuje objekt v kresbové vrstvě, jako je AutoShape, textbox, freeform, OLE objekt, ActiveX ovládání, nebo obraz. Dokument Word může obsahovat jeden nebo více různých tvarů. Tvary dokumentu jsou zastoupeny Shape třída.
Vložit tvar pomocí tvůrce dokumentů
Můžete vložit inline tvar se zadaným typem a velikostí a volně plovoucím tvarem s zadanou polohou, velikostí a textovým zábalem do dokumentu pomocí InsertShape metoda. The InsertShape metoda umožňuje vložení tvaru DML do modelu dokumentu. Dokument musí být uložen ve formátu, který podporuje tvary DML, jinak tyto uzly budou převedeny do tvaru VML, zatímco ukládání dokumentů.
Následující příklad kódu ukazuje, jak vložit tyto typy tvarů do dokumentu:
// 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); |
Nastavit poměr Aspect Locked
Použití Aspose.Words, můžete určit, zda je poměr stran tvaru a stran uzamčen přes AspectRatioLocked majetek.
Následující příklad kódu ukazuje, jak pracovat s AspectRatioLocked vlastnost:
// 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); |
Nastavit tvar uspořádání v buňce
Můžete také určit, zda je tvar zobrazen uvnitř tabulky nebo mimo ní pomocí IsLayoutInCell majetek.
Následující příklad kódu ukazuje, jak pracovat s IsLayoutInCell vlastnost:
// 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); |
Přidat rohy odstřižené
Můžete vytvořit střih rohový obdélník pomocí Aspose.Words. Typy tvarů jsou SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, and DiagonalCornersRounded.
Tvar DML je vytvořen pomocí InsertShape metoda s těmito typy tvarů. Tyto typy nelze použít k vytvoření tvarů VML. Pokus o vytvoření tvaru pomocí veřejného konstruktéra třídy “Tvar” zvyšuje výjimku “NotSupportedException.”
Následující příklad kódu ukazuje, jak vložit tento typ tvarů do dokumentu:
// 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); |
Získejte aktuální body tvarů
Použití Aspose.Words API, můžete získat umístění a velikost tvaru obsahující blok v bodech, vzhledem k kotvě nejvyššího tvaru. K tomu použijte BoundsInPoints majetek.
Následující příklad kódu ukazuje, jak pracovat s BoundsInPoints vlastnost:
// 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()); |
Zadejte vertikální kotvu
Můžete zadat text vertikální zarovnání ve tvaru pomocí VerticalAnchor majetek.
Následující příklad kódu ukazuje, jak pracovat s VerticalAnchor vlastnost:
// 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"); |
Detekovat chytré Tvar umění
Aspose.Words také umožňuje zjistit, zda má tvar SmartArt
objekt. K tomu použijte HasSmartArt majetek.
Následující příklad kódu ukazuje, jak pracovat s HasSmartArt vlastnost:
// 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."); |
Formát horizontálního pravidla
Můžete vložit horizontální tvar pravidla do dokumentu pomocí InsertHorizontalRule metoda.
Aspose.Words API poskytuje HorizontalRuleFormat vlastnost pro přístup k vlastnostem vodorovného tvaru pravidla. The HorizontalRuleFormat třída odhaluje základní vlastnosti jako Height, Color, NoShade atd. pro formátování horizontálního pravidla.
Následující příklad kódu ukazuje, jak nastavit 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"); |