Not able to add borders to image

Document testingDocument = new Document();
DocumentBuilder testingDocumentBuilder = new DocumentBuilder(testingDocument);

Shape shape = testingDocumentBuilder.insertImage("sampleImg.jpg");

shape.getImageData().getBorders().setLineWidth(Double.parseDouble("5.0"));
shape.getImageData().getBorders().setColor(Color.red);
shape.getImageData().getBorders().setLineStyle(LineStyle.THICK);

testingDocument.save("testing.docx");

This is my code and im not finding any border in my image. If this is not the correct way . Then tell me correct way to add borders to image

@VishalPro

To add borders to an image in Aspose.Words for Java, you should set the border properties directly on the Shape object itself, rather than on the ImageData of the shape. The Borders property is not applicable to the ImageData but rather to the Shape itself.

Here’s how you can modify your code to add borders to the image:

Document testingDocument = new Document();
DocumentBuilder testingDocumentBuilder = new DocumentBuilder(testingDocument);

// Insert the image into the document
Shape shape = testingDocumentBuilder.insertImage("sampleImg.jpg");

// Set the border properties directly on the shape
shape.getStroke().setColor(Color.RED);
shape.getStroke().setWeight(5.0);
shape.getStroke().setDashStyle(DashStyle.SOLID);

// Save the document
testingDocument.save("testing.docx");

In this code:

  • The getStroke() method is used to access the stroke properties of the shape, which allows you to set the border color, weight, and style.
  • The setColor() method sets the border color to red.
  • The setWeight() method sets the border thickness.
  • The setDashStyle() method can be used to define the style of the border (e.g., solid, dashed).

Make sure to adjust the properties according to your requirements. This should effectively add a visible border around your image in the generated document.