When inserting a picture and nearby cell's font size is set, picture scale height increases

HI,
An image is being inserted. When lines 31-33 are commented out the scale height of the picture is around 100% which is expected.

However when these lines are uncommented, the text in cell 1, 1 is set to 24pts. The picture scale height is > 130%.

When performing this action in Excel outside of Aspose this behaviour is not observed. The example code is show below.

import com.aspose.cells.*;

import java.io.InputStream;

public class ExcelImageInserter {

public static void main(String[] args) {
try {
// Create a new workbook
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

  // Load image from resources
  InputStream imageStream = ExcelImageInserter.class
      .getClassLoader()
      .getResourceAsStream("audi_logo.png");

  if (imageStream == null) {
    throw new RuntimeException("Image not found in resources.");
  }

  // Add image to worksheet
  int pictureIndex = sheet.getPictures().add(1, 6, imageStream, 100, 100);
  Picture picture = sheet.getPictures().get(pictureIndex);
  picture.setPlacement(PlacementType.FREE_FLOATING);

  Cell titleCell = sheet.getCells().get(1, 1);
  titleCell.putValue("Audi Lead Track");

  // IMAGE OK WHEN CODE BELOW IS COMMENTED OUT BUT FONT SIZE NEEDS TO BE 24

// Style style = titleCell.getStyle();
// style.getFont().setSize(24);
// titleCell.setStyle(style);

  // Save the workbook
  workbook.save("workbook_with_image.xlsx");

  System.out.println("Workbook created successfully.");
} catch (Exception err) {
  System.out.println(err.getMessage());
}

}

}

@trackback

It sounds like you’re experiencing an issue where the height of the inserted image increases unexpectedly when you set the font size of a nearby cell. This behavior can occur due to the way Aspose.Cells handles the placement and scaling of images in relation to cell formatting.

Cause: The unexpected increase in image height is likely due to the PlacementType.FREE_FLOATING setting, which allows the image to float independently of cell formatting.

Solution: To address this issue, you can try the following steps:

  1. Check Placement Type: Change the placement type of the image from PlacementType.FREE_FLOATING to PlacementType.MOVE_AND_SIZE. This may help maintain the image size relative to cell formatting.

  2. Adjust Image Size Manually: After setting the font size, manually adjust the height scale of the image to maintain the desired size by setting the HeightScale property of the Picture object.

  3. Set Cell Style Before Image Insertion: If possible, set the cell style (font size) before inserting the image. This may help in preserving the expected image size.

Here’s a modified version of your code that includes setting the height scale after adjusting the font size:

import com.aspose.cells.*;

import java.io.InputStream;

public class ExcelImageInserter {

  public static void main(String[] args) {
    try {
      // Create a new workbook
      Workbook workbook = new Workbook();
      Worksheet sheet = workbook.getWorksheets().get(0);

      // Load image from resources
      InputStream imageStream = ExcelImageInserter.class
          .getClassLoader()
          .getResourceAsStream("audi_logo.png");

      if (imageStream == null) {
        throw new RuntimeException("Image not found in resources.");
      }

      // Add image to worksheet
      int pictureIndex = sheet.getPictures().add(1, 6, imageStream, 100, 100);
      Picture picture = sheet.getPictures().get(pictureIndex);
      picture.setPlacement(PlacementType.MOVE_AND_SIZE); // Change placement type

      Cell titleCell = sheet.getCells().get(1, 1);
      titleCell.putValue("Audi Lead Track");

      // Set font size
      Style style = titleCell.getStyle();
      style.getFont().setSize(24);
      titleCell.setStyle(style);

      // Adjust image height scale if necessary
      picture.setHeightScale(100); // Set to desired scale

      // Save the workbook
      workbook.save("workbook_with_image.xlsx");

      System.out.println("Workbook created successfully.");
    } catch (Exception err) {
      System.out.println(err.getMessage());
    }
  }
}

Next Steps: Implement the changes in your code and test to see if the image size remains consistent when adjusting the font size of nearby cells.

If you encounter any further issues or have additional questions, please feel free to reach out!

I have performed the changes suggested above however the issue is not fixed.

This is the modified code.

import com.aspose.cells.*;

import java.io.InputStream;

public class ExcelImageInserter {

public static void main(String[] args) {
try {
// Create a new workbook
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

  // Load image from resources
  InputStream imageStream = ExcelImageInserter.class
      .getClassLoader()
      .getResourceAsStream("audi_logo.png");

  if (imageStream == null) {
    throw new RuntimeException("Image not found in resources.");
  }

  Cell titleCell = sheet.getCells().get(1, 1);
  titleCell.putValue("Audi Lead Track");
  // IMAGE OK WHEN CODE BELOW IS COMMENTED OUT BUT FONT SIZE NEEDS TO BE 24
  Style style =  titleCell.getStyle();
  style.getFont().setSize(24);
  titleCell.setStyle(style);

  // Add image to worksheet
  int pictureIndex = sheet.getPictures().add(1, 6, imageStream, 100, 100);
  Picture picture = sheet.getPictures().get(pictureIndex);
  picture.setPlacement(PlacementType.MOVE_AND_SIZE);

  picture.setHeightScale(100);

  // Save the workbook
  workbook.save("workbook_with_image.xlsx");

  System.out.println("Workbook created successfully.");
} catch (Exception err) {
  System.out.println(err.getMessage());
}

}

}

Screenshot 2025-07-25 at 17.30.10.jpg (215.2 KB)

The screenshot shows the scale height which is 133%

@trackback,

I tested your scenario/case with a simple image (PNG) and I am able to reproduce the issue you mentioned by using your sample code snippet. I confirmed when inserting a picture and nearby cell’s font size is set, picture’s scale height increases.

We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSJAVA-46443

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@trackback

Please autofit rows before adding images as the following:

AutoFitterOptions options = new AutoFitterOptions();
options.setOnlyAuto(true);
sheet.autoFitRows(options);
// add image

Hi,
I have implemented the autoFitRows(true) setting but the image remains the same. Below is the modified code.

import com.aspose.cells.*;

import java.io.InputStream;

public class ExcelImageInserter {

public static void main(String[] args) {
try {
// Create a new workbook
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

  AutoFitterOptions options = new AutoFitterOptions();
  options.setOnlyAuto(true);
  sheet.autoFitRows(options);
  // add image

  // Load image from resources
  InputStream imageStream = ExcelImageInserter.class
      .getClassLoader()
      .getResourceAsStream("audi_logo.png");

  if (imageStream == null) {
    throw new RuntimeException("Image not found in resources.");
  }

  // Add image to worksheet
  int pictureIndex = sheet.getPictures().add(1, 6, imageStream, 100, 100);
  Picture picture = sheet.getPictures().get(pictureIndex);
  picture.setPlacement(PlacementType.FREE_FLOATING);

  Cell titleCell = sheet.getCells().get(1, 1);
  titleCell.putValue("Audi Lead Track");
  // IMAGE OK WHEN CODE BELOW IS COMMENTED OUT BUT FONT SIZE NEEDS TO BE 24
  Style style =  titleCell.getStyle();
  style.getFont().setSize(24);
  titleCell.setStyle(style);

  picture.setHeightScale(100);

  // Save the workbook
  workbook.save("workbook_with_image.xlsx");

  System.out.println("Workbook created successfully.");
} catch (Exception err) {
  System.out.println(err.getMessage());
}

}

}

@trackback,

Since you are creating a new workbook from the scratch, so kindly do move the lines of code regarding auto-fit rows just before rendering/saving to Excel file. Please refer to the following sample code, I tested similar scenario/case and it works fine with my own sample code.

import com.aspose.cells.*;
import java.io.InputStream;

public class ExcelImageInserter {

public static void main(String[] args) {
try {
// Create a new workbook
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

  // add image
  // Load image from resources
  InputStream imageStream = ExcelImageInserter.class
      .getClassLoader()
      .getResourceAsStream("audi_logo.png");

  if (imageStream == null) {
    throw new RuntimeException("Image not found in resources.");
  }

  // Add image to worksheet
  int pictureIndex = sheet.getPictures().add(1, 6, imageStream, 100, 100);
  Picture picture = sheet.getPictures().get(pictureIndex);
  picture.setPlacement(PlacementType.FREE_FLOATING);

  Cell titleCell = sheet.getCells().get(1, 1);
  titleCell.putValue("Audi Lead Track");
  // IMAGE OK WHEN CODE BELOW IS COMMENTED OUT BUT FONT SIZE NEEDS TO BE 24
  Style style =  titleCell.getStyle();
  style.getFont().setSize(24);
  titleCell.setStyle(style);

  picture.setHeightScale(100);

  AutoFitterOptions options = new AutoFitterOptions();
  options.setOnlyAuto(true);
  sheet.autoFitRows(options);

  // Save the workbook
  workbook.save("workbook_with_image.xlsx");

  System.out.println("Workbook created successfully.");
} catch (Exception err) {
  System.out.println(err.getMessage());
}
}

}

Let us know if you still find the issue.

Thank you, I can confirm this fixes the issues. Moving the auto file before save made the difference.

@trackback,

We are glad to hear that your issue has been resolved by implementing the suggested changes. Should you have any additional questions or comments, please don’t hesitate to reach out to us again.