We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Linking Shapes

Hello,
I would like to create a link between two shapes. In ms Word I just

  1. create eg: rectangle and circle
  2. right mouse button on shape --> Add text
  3. I enter some text and I wrap it as a bookmark.

Finally, I can connect shapes based on their text using bookmark linking. How to achieve the same result using aspose?

I tried

final Shape rectangleShape = new Shape(builder.getDocument(), ShapeType.RECTANGLE);
final Paragraph  firstParagraph = new Paragraph(builder.getDocument());
shape.appendChild(firstParagraph);
 builder.moveTo(paragraph); // I cant move cusror there and call builder.insertHyperlink(...)

Expected behaviour:
Expected_Shape_Linking.zip (16.0 KB)

@aolo23,

Please see these Sample DOCX files.zip (28.4 KB) and try running the following Java code:

Document doc = new Document("C:\\Temp\\Expected_Shape_Linking\\sample.docx");
Shape shape = (Shape) ((Paragraph) doc.getFirstSection().getBody().getParagraphs().get(2)).getFirstChild();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(shape.getFirstParagraph());

builder.getFont().setColor(Color.BLUE);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

builder.startBookmark("x");
FieldHyperlink link = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, false);
link.setSubAddress("y");
link.setResult("XXXXX");
builder.endBookmark("x");

doc.updateFields();

doc.save("C:\\temp\\Expected_Shape_Linking\\awjava-21.9.docx");

Upon clicking, the bookmarked hyperlink will point to bookmarked content in another Shape.

Can it be done without any previously created document?
That mean instead doing this : Document doc = new Document("C:\\Temp\\Expected_Shape_Linking\\sample.docx");
I want to create shapes programmatically.

Sth like it does not work:

    public static void test() throws Exception {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        final Shape shape = new Shape(builder.getDocument(), ShapeType.RECTANGLE);
        builder.insertNode(shape);
        shape.setTop(50);
        shape.setLeft(50);
        shape.setHeight(100);
        shape.setWidth(100);

        final Paragraph firstParagraph = new Paragraph(builder.getDocument());
        shape.appendChild(firstParagraph);
        builder.moveTo(firstParagraph);

        builder.getFont().setColor(Color.BLUE);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

        builder.startBookmark("x");
        FieldHyperlink link = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, false);
        link.setSubAddress("y");
        link.setResult("XXXXX");
        builder.endBookmark("x");



        final Shape shape2 = new Shape(builder.getDocument(), ShapeType.RECTANGLE);
        builder.insertNode(shape2);
        shape2.setTop(200);
        shape2.setLeft(200);
        shape2.setHeight(100);
        shape2.setWidth(100);

        final Paragraph firstParagraph2 = new Paragraph(builder.getDocument());
        shape2.appendChild(firstParagraph2);

        builder.moveTo(firstParagraph2);

        builder.getFont().setColor(Color.BLUE);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

        builder.startBookmark("y");
        FieldHyperlink link2 = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, false);
        link2.setSubAddress("x");
        link2.setResult("YYYY");
        builder.endBookmark("y");

        doc.updateFields();
        saveDocument(builder, "awjava_");

    }

@aolo23,

Please see sample documents (Sample DOCX Files 2.zip (27.1 KB)) and you can build logic on the following code to get the desired output:

Document doc = new Document("C:\\Temp\\Expected_Shape_Linking\\sample - Copy.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shape = builder.insertShape(ShapeType.RECTANGLE, 2 * 72, 1.31 * 72);
shape.setFillColor(Color.WHITE);
Paragraph para = new Paragraph(doc);
shape.appendChild(para);

builder.moveTo(shape.getFirstParagraph());

builder.getFont().setColor(Color.BLUE);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

builder.startBookmark("x");
FieldHyperlink link = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, false);
link.setSubAddress("y");
link.setResult("XXXXX");
builder.endBookmark("x");

doc.updateFields();

doc.save("C:\\temp\\Expected_Shape_Linking\\awjava-21.9.docx");

I can’t use any input docx file as a start point. It my case it create to many possible configurations which should I take into account

@aolo23,

Please check the following code will draw Ellipse the and Rectangle type shapes and then link their content:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shapeELLIPSE = builder.insertShape(ShapeType.ELLIPSE, 2.01 * 72, 1.66 * 72);
Paragraph para1 = new Paragraph(doc);
shapeELLIPSE.appendChild(para1);

builder.moveTo(shapeELLIPSE.getFirstParagraph());

builder.getFont().setColor(Color.WHITE);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

builder.startBookmark("y");
builder.write("YYYYYY");
builder.endBookmark("y");

shapeELLIPSE.setTop(2 * 72);
shapeELLIPSE.setLeft(5 * 72);
shapeELLIPSE.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shapeELLIPSE.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shapeELLIPSE.setWrapType(WrapType.NONE);


builder.moveToDocumentStart();
Shape shapeRECTANGLE = builder.insertShape(ShapeType.RECTANGLE, 2 * 72, 1.31 * 72);
shapeRECTANGLE.setFillColor(Color.WHITE);
Paragraph para = new Paragraph(doc);
shapeRECTANGLE.appendChild(para);

builder.moveTo(shapeRECTANGLE.getFirstParagraph());

builder.getFont().setColor(Color.BLUE);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

builder.startBookmark("x");
FieldHyperlink link = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, false);
link.setSubAddress("y");
link.setResult("XXXXX");
builder.endBookmark("x");

doc.updateFields();

doc.save("C:\\temp\\awjava-21.9.docx");
1 Like

That was a working example and thank you. But I suggest correct this behaviour when someone can’t move the cursor directly from one paragraph placed into some shape into another without going back to the paragraph placed on the page. (level 0? )

@aolo23,

Unfortunately, your question isn’t clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your problem along with sample Word documents and screenshots. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.