# Set Cell Background Image for Cell Range

DsExcel Java is a MESCIUS Documents product that offers a comprehensive library to create, manipulate, convert, and share Microsoft Excel-compatible spreadsheets.

## Content





DsExcel enables you to set the cell background image and its layout for the cell range using [setBackgroundImage](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html#setBackgroundImage) and [setBackgroundImageLayout](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html#setBackgroundImageLayout) methods in [IRange](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/IRange.html) interface. You can only export the cell background image to PDF, HTML, and IMG and can only view it in SpreadJS when saved in .sjs format. [BackgroundImageLayout](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/BackgroundImageLayout.html) enumeration allows you to set the background image layout to Stretch (default), Center, Zoom, or None.

DsExcel only supports and exports the following image formats as the cell background image:

*   PNG
*   JPEG/JPG
*   ICO
*   SVG
*   GIF

Refer to the following example code to add a cell background image in different layouts and export the workbook to a PDF file:

```Java
// Create a new workbook.
Workbook workbook = new Workbook();

IWorksheet worksheet = workbook.getWorksheets().get(0);

// Load the image.
InputStream stream = new FileInputStream("Chrome_icon.png");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while (true) {
    try {
        if (!((bytesRead = stream.read(buffer)) != -1)) break;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    outputStream.write(buffer, 0, bytesRead);
}
byte[] imagebyte = outputStream.toByteArray();

worksheet.getRange("A2:E2").setValue(new String[] { "Stretch", "Center", "Zoom", "None", "Default(Stretch)" });
worksheet.getRange("A3:E3").setValue("Chrome");
worksheet.getRange("A3:E3").setRowHeightInPixel(80);
worksheet.getRange("A3:E3").setColumnWidthInPixel(100);
        
// Add cell background image.
worksheet.getRange("A3:E3").setBackgroundImage(imagebyte);

// Set image layout.
worksheet.getRange("A3").setBackgroundImageLayout(BackgroundImageLayout.Stretch);
worksheet.getRange("B3").setBackgroundImageLayout(BackgroundImageLayout.Center);
worksheet.getRange("C3").setBackgroundImageLayout(BackgroundImageLayout.Zoom);
worksheet.getRange("D3").setBackgroundImageLayout(BackgroundImageLayout.None);

// Set PDF export options.
workbook.getActiveSheet().getPageSetup().setPrintGridlines(true);
workbook.getActiveSheet().getPageSetup().setPrintHeadings(true);
    
// Save to a PDF file.
workbook.save("CellBackgroundImage.pdf");
```

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/set-background-image.png)

**Limitations**

DsExcel does not support saving the background image to Excel; hence, you cannot view it in Excel.