I want to add http image address in shape and scale it.
In our case, a shape can contain text and image. So I have used ShapeType.TEXT_BOX
Shape s1 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph = new Paragraph(document);
Run run1 = new Run(document, "");
paragraph.appendChild(run1);
builder.moveTo(run1);
Shape imageShape = builder.insertImage("https://images.pexels.com/photos/18968412/pexels-photo-18968412/free-photo-of-flowerbed-of-daffodils.jpeg");
imageShape.setWidth(400);
imageShape.setHeight(200);
Image gets added but scaling doesn’t work.
@aishwarya12joshi Could you please elaborate your requirements in more details? As I can see the inserted image is properly scaled to the textbox size:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape s1 = new Shape(doc, ShapeType.TEXT_BOX);
s1.setWidth(400);
s1.setHeight(200);
Paragraph paragraph = new Paragraph(doc);
s1.appendChild(paragraph);
builder.moveTo(paragraph);
builder.insertImage("https://images.pexels.com/photos/18968412/pexels-photo-18968412/free-photo-of-flowerbed-of-daffodils.jpeg");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(s1);
doc.save("C:\\Temp\\out.docx");
out.docx (2.1 MB)
As you can see in your output file, image didn’t stretch to take full shape width even though image itself has larger dimensions than that of shape.
I want that image should also take width as 400.
Also we can use http image address in word right?
@aishwarya12joshi The inserted image preserve the original image aspect ratio. If it is required to fill the created shape with image, you can use code like this:
Document doc = new Document();
Shape s1 = new Shape(doc, ShapeType.TEXT_BOX);
s1.setWidth(400);
s1.setHeight(200);
s1.getImageData().setImage("https://images.pexels.com/photos/18968412/pexels-photo-18968412/free-photo-of-flowerbed-of-daffodils.jpeg");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(s1);
doc.save("C:\\Temp\\out.docx");
out.docx (2.1 MB)
Sure, Aspose.Words downloads the image from the specified address and inserts it into the document.