Lavorare con le forme

In questo argomento viene illustrato come lavorare a livello di codice con le forme utilizzando Aspose.Words.

Le forme in Aspose.Words rappresentano un oggetto nel livello di disegno, ad esempio un oggetto AutoShape, una casella di testo, una forma libera, un oggetto OLE, un controllo ActiveX o un’immagine. Un documento Word può contenere una o più forme diverse. Le forme del documento sono rappresentate dalla classe Shape.

Inserisci forma usando Document Builder

È possibile inserire una forma in linea con tipo e dimensione specificati e una forma fluttuante con la posizione, la dimensione e il tipo di testo a capo specificati in un documento utilizzando il metodo InsertShape. Il metodo InsertShape consente di inserire la forma DML nel modello del documento. Il documento deve essere salvato nel formato, che supporta DML forme, altrimenti, tali nodi verranno convertiti in VML forma, durante il salvataggio del documento.

L’esempio di codice seguente mostra come inserire questi tipi di forme nel documento:

// 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);

Imposta Proporzioni bloccate

Utilizzando Aspose.Words, è possibile specificare se le proporzioni della forma sono bloccate tramite la proprietà AspectRatioLocked.

Il seguente esempio di codice mostra come lavorare con la proprietà 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);

Imposta il layout della forma nella cella

È inoltre possibile specificare se la forma viene visualizzata all’interno di una tabella o all’esterno di essa utilizzando la proprietà IsLayoutInCell.

Il seguente esempio di codice mostra come lavorare con la proprietà 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);

Aggiungi angoli ritagliati

È possibile creare un rettangolo angolo snip utilizzando Aspose.Words. I tipi di forma sono *SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,*e DiagonalCornersRounded.

La forma DML viene creata usando il metodo InsertShape con questi tipi di forma. Questi tipi non possono essere utilizzati per creare VML forme. Tentare di creare una forma utilizzando il costruttore pubblico della classe " Shape “genera l’eccezione” NotSupportedException".

L’esempio di codice seguente mostra come inserire questo tipo di forme nel documento:

// 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);

Ottieni Limiti di forma effettivi Punti

Usando Aspose.Words API, è possibile ottenere la posizione e la dimensione della forma contenente il blocco in punti, rispetto all’ancoraggio della forma più in alto. A tale scopo, utilizzare la proprietà BoundsInPoints.

Il seguente esempio di codice mostra come lavorare con la proprietà 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());

Specificare Ancoraggio verticale

È possibile specificare l’allineamento verticale del testo all’interno di una forma utilizzando la proprietà VerticalAnchor.

Il seguente esempio di codice mostra come lavorare con la proprietà 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");

Rileva SmartArt Forma

Aspose.Words permette anche di rilevare se la Forma ha un oggetto SmartArt. A tale scopo, utilizzare la proprietà HasSmartArt.

Il seguente esempio di codice mostra come lavorare con la proprietà 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.");

Formato regola orizzontale

È possibile inserire una forma di regola orizzontale in un documento utilizzando il metodo InsertHorizontalRule.

Aspose.Words API fornisce la proprietà HorizontalRuleFormat per accedere alle proprietà della forma regola orizzontale. La classe HorizontalRuleFormat espone proprietà di base come Altezza, Colore, NoShade ecc. per la formattazione di una regola orizzontale.

Il seguente esempio di codice mostra come impostare 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");