Unable to Set Image opacity

Hi , im not able to set image Opacity. Heres my code

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

Shape imageShape = documentBuilder.insertImage("sampleImg.jpg");

imageShape.setFillColor(Color.BLACK);
imageShape.getFill().setOpacity(0.5f);

doc.save("test.docx");

Kindly Help

@VishalPro

Unfortunately, the Aspose.Words API does not currently support setting the opacity or transparency of image shapes directly. This limitation has been noted, and there is an issue logged for this feature (WORDSNET-19658).

As a workaround, you can use the Aspose.Imaging for Java API to adjust the transparency of your image before inserting it into the Word document. Here’s a brief guide on how to do this:

  1. Use Aspose.Imaging to load your image.
  2. Set the desired transparency.
  3. Save the modified image.
  4. Insert the modified image into your Word document using Aspose.Words.

Here’s a sample code snippet to illustrate this approach:

import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptions;
import com.aspose.imaging.fileformats.png.PngOptions;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Shape;

public class ImageOpacityExample {
    public static void main(String[] args) {
        // Load the image using Aspose.Imaging
        Image image = Image.load("sampleImg.jpg");
        
        // Set the transparency (0.0f is fully transparent, 1.0f is fully opaque)
        image.getPalette().setTransparency(0.5f);
        
        // Save the modified image
        image.save("modifiedImg.png", new PngOptions());

        // Now, use Aspose.Words to insert the modified image
        Document doc = new Document();
        DocumentBuilder documentBuilder = new DocumentBuilder(doc);
        
        Shape imageShape = documentBuilder.insertImage("modifiedImg.png");
        
        doc.save("test.docx");
    }
}

Make sure to include the necessary Aspose.Imaging and Aspose.Words libraries in your project to use the above code.

For more details on manipulating image transparency, you can refer to the documentation on Specifying Transparency for PNG Images.

If you have any further questions or need additional assistance, feel free to ask!

@VishalPro Please use the following code:

Document doc = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
    
Shape imageShape = documentBuilder.insertShape(ShapeType.RECTANGLE, 100, 100);

imageShape.getFill().setImage("C:\\Temp\\test.png");
imageShape.getFill().setOpacity(0.5f);
    
doc.save("C:\\temp\\out.docx");

@alexey.noskov Im not able to find setImage method in my aspose library . Im currently using v_20_12_0 Version.

@VishalPro Please try using the latest 24.8 version of Aspose.Words.

@alexey.noskov is there any alternative way ? moving to 24.8 is not possible

@VishalPro I am afraid, I do not see any other way to achieve this.