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

Free Support Forum - aspose.com

Replace image placeholder with actual image in docx using java

My requirement is like , i want to insert an image in place of a text box containing <<image [image_expression] >> (placeholder) using java.
Could you please provide the sample code (with explanation) for getting placeholder and replacing it with image.
Please guide what kind of image expression should be used.

@Rit365 The expression declared within an image tag must return a value of one of the following types:

  • A byte array containing an image data
  • An InputStream instance able to read an image data
  • A BufferedImage object
  • A string containing an image URI, path, or Base64-encoded image data

Please see our documentation for more information:
https://docs.aspose.com/words/java/inserting-images-dynamically/

For example see the following simple code:

Document doc = new Document("C:\\Temp\\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, Files.readAllBytes(Paths.get("C:\\Temp\\myimage.png")), "myimage");
doc.save("C:\\Temp\\out.docx");

Here is template: in.docx (15.0 KB)

In the code byte array is used and an image data.

@alexey.noskov Thanks a lot for the solution .

1 Like