Worksheet.setBackgroundImage() not working

Hi,
I’ve download examples from GitHub - aspose-cells/Aspose.Cells-for-Java: Aspose.Cells for Java examples, plugins and showcases
and trying to run AsposeCellsExamples.TechnicalArticles.SetBackgroundPictureforWorksheet.java,
but the result is a blank excel without the background image.
Need your help, thanks!

@forward6,

It looks like image file “school.jpg” is missing in " ./resources/TechnicalArticles" folder, that’s why the background image was not set. The image file is here in this folder (instead):

we will check the Github sample project and fix it soon.

In the mean time, you may manually change the code segment in the .java file accordingly or simply try the following sample code, it works fine as I tested:
e.g
Sample code:

// Instantiate a new Workbook.
				Workbook workbook = new Workbook();
				// Get the first worksheet.
				Worksheet sheet = workbook.getWorksheets().get(0);

				// Get the image file.
				File file = new File("e:\\test\\school.jpg");//please set image file's path in your env.
				// Get the picture into the streams.
				byte[] imageData = new byte[(int) file.length()];
				FileInputStream fis = new FileInputStream(file);
				fis.read(imageData);

				// Set the background image for the sheet.
				sheet.setBackgroundImage(imageData);

				fis.close();
				// Save the excel file
				workbook.save("f:\\files\\SBPforWorksheet.xls"); //update the path accordingly.

Let us know if you still find any issue.

@forward6,

We have fixed the issue now. Could you try re-downloading the examples project and try the sample, it should work fine now.

Let us know if you still find any issue.

After new “02_OutputDirectory” folder, the test can be done but the result excel still empty >>
SBPforWorksheet.xlsx.zip (17.0 KB)

@forward6,

Thanks for the sample file.

Please note, the file will surely be empty with only sheet background is set. This is due to the sample code of the .java file, i.e.,

if you notice the code does not add any value to any cell in the sheet and only sets sheet background (image) which is applied ok. See the screenshot for your reference.

Let us know if you still have any confusion.

Oh, I use mac Numbers and google online Sheets to open SBPforWorksheet.xlsx, both see none background image, so it’s the tool problem?

Another: I change the file name to SBPforWorksheet.pdf >>
workbook.save(outDir + “SBPforWorksheet.pdf”);
but the result got none bg image also, setBackgroundImage() not support pdf?

@forward6,

Yes, only MS Excel will display the background image of the sheet.

Yes, PDF file won’t display the background sheet image. The reason is the sheet’s background image is only visible/available in MS Excel normal display or page layout view. But when you take the print preview of the sheet (in MS Excel), the sheet background image won’t be visible. Aspose.Cells (or even MS Excel’s built-in pdf renderer) renders PDF file (from Excel spreadsheet) with contents based on what is shown in print preview of MS Excel.

Hope, this helps a bit.

Okay, I’ll try Aspose.PDF.
Many thanks for your help!
:slight_smile:

@forward6,
You are welcome.