How to add multiple images on a worksheet at a given X,Y and image width and image height

12 views
Skip to first unread message

ajitk...@gmail.com

unread,
Dec 21, 2017, 10:33:31 PM12/21/17
to JExcel Forum
Have an excel worksheet with data. Adding an image following the example : http://jexcel-support.teamdev.com/samples/merge-and-insert
adds it on top-left corner overlapping the data.

Referring the example in  http://jexcel-support.teamdev.com/samples/merge-and-insert
      File file = new File("logo.jpg");
      BStr bStr = new BStr(file.getAbsolutePath());
      iPictures.insert(bStr,unspecifiedParameter);

 When adding an image to a worksheet, is there a way to specify X,Y coordinates and image width and image height?

ajitk...@gmail.com

unread,
Dec 22, 2017, 12:48:54 AM12/22/17
to JExcel Forum, ajitk...@gmail.com
I was able to resolve this as follows:


Need to assign the result of insert operation and then set position parameters on Picture object.
e.g.

Picture p = iPictures.insert(bStr,unspecifiedParameter);
p.setLeft(new DoubleFloat(xPositionOffset));
p.setTop(new DoubleFloat(yPositionOffset));


One may set width and height also, if required.

Anna Dolbina

unread,
Dec 22, 2017, 2:49:34 AM12/22/17
to ajitk...@gmail.com, JExcel Forum

Hi,

It is possible to configure X and Y coordinates for the inserted picture.

The following sample code demonstrates how to perform this:

Picture picture = iPictures.insert(new BStr("C:\\picture.png"), Variant.createUnspecifiedParameter());
picture. setTop(new DoubleFloat(100));
picture. setLeft(new DoubleFloat(50));

Please let us know if you have any further questions.

Sincerely,
Anna

Friday, 22 December 2017, 05:33AM +02:00 from ajitk...@gmail.com:

--
You received this message because you are subscribed to the Google Groups "JExcel Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jexcel-forum...@teamdev.com.
To view this discussion on the web visit https://groups.google.com/a/teamdev.com/d/msgid/jexcel-forum/6381baf6-a24c-4f73-849b-2e711818a723%40teamdev.com.

Anna Dolbina

unread,
Dec 22, 2017, 3:03:46 AM12/22/17
to ajitk...@gmail.com, JExcel Forum
Hi,

The picture width and height can be configured in a similar way:

Picture picture = iPictures.insert(new BStr("C:\\picture.png"), Variant.createUnspecifiedParameter());
picture.setTop(new DoubleFloat(100));
picture.setLeft(new DoubleFloat(50));
picture.setWidth(new DoubleFloat(500));
picture.setHeight(new DoubleFloat(500));

Please let us know if you have any further questions.

Sincerely,
Anna


On Fri, Dec 22, 2017 at 9:49 AM, Anna Dolbina <anna.d...@teamdev.com> wrote:

Hi,

It is possible to configure X and Y coordinates for the inserted picture.

The following sample code demonstrates how to perform this:

Picture picture = iPictures.insert(new BStr("C:\\picture.png"), Variant.createUnspecifiedParameter());
picture. setTop(new DoubleFloat(100));
picture. setLeft(new DoubleFloat(50));

Please let us know if you have any further questions.

Sincerely,
Anna

Friday, 22 December 2017, 05:33AM +02:00 from ajitk...@gmail.com:
Have an excel worksheet with data. Adding an image following the example : http://jexcel-support.teamdev.com/samples/merge-and-insert
adds it on top-left corner overlapping the data.

Referring the example in  http://jexcel-support.teamdev.com/samples/merge-and-insert
      File file = new File("logo.jpg");
      BStr bStr = new BStr(file.getAbsolutePath());
      iPictures.insert(bStr,unspecifiedParameter);

 When adding an image to a worksheet, is there a way to specify X,Y coordinates and image width and image height?

--
You received this message because you are subscribed to the Google Groups "JExcel Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jexcel-forum+unsubscribe@teamdev.com.

Ajit Kumar

unread,
Dec 26, 2017, 12:11:05 PM12/26/17
to Anna Dolbina, JExcel Forum

I was able to add the images at desired location. Thanks.

When adding the picture with absolute path of image, the excel opens fine with image but when the excel is copied to another location, the image is no longer displayed.

Excel shows a box at the image location with message "The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location."

Is there a way to insert picture with the image embedded in Excel so that the excel file generated after adding images can be moved around without moving the images?

Anna Dolbina

unread,
Dec 27, 2017, 2:02:10 AM12/27/17
to Ajit Kumar, JExcel Forum
Hi,

To save the pictures with the workbook and avoid creating links, you need to use a different approach.
The approach is based on adding the picture to Shapes instead of Pictures.

Here is the sample code demonstrating the approach:
application.getOleMessageLoop().doInvokeAndWait(new Runnable()
{
public void run()
{
_Worksheet worksheet = activeWorksheet.getPeer();
Shapes shapes = worksheet.getShapes();

shapes.addPicture(new BStr("c:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"),
new MsoTriState(MsoTriState.msoFalse),//link to file
new MsoTriState(MsoTriState.msoCTrue), //save with document
new SingleFloat(50), //left
new SingleFloat(50), //top
new SingleFloat(100), //width
new SingleFloat(100) //height
);
}
});
The documentation for the Shapes.addPicture() method can be found by the following link:

Please let us know if you have any further questions.

Sincerely,
Anna

Reply all
Reply to author
Forward
0 new messages