Document Solutions for Excel, .NET Edition | Document Solutions
Features / Worksheet / Range Operations / Set Cell Background Image for Cell Range
In This Topic
    Set Cell Background Image for Cell Range
    In This Topic

    DsExcel enables you to set the cell background image and its layout for the cell range using BackgroundImage and BackgroundImageLayout properties in IRange 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 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:

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

    C#
    Copy Code
    // Create a new workbook.
    var workbook = new Workbook();
    
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Load the image.
    byte[] imageBytes = File.ReadAllBytes("Chrome_icon.png");
    
    worksheet.Range["A2:E2"].Value = new string[] { "Stretch", "Center", "Zoom", "None", "Default(Stretch)" };
    worksheet.Range["A3:E3"].Value = "Chrome";
    worksheet.Range["A3:E3"].RowHeightInPixel = 80;
    worksheet.Range["A3:E3"].ColumnWidthInPixel = 100;
    
    // Add cell background image.
    worksheet.Range["A3:E3"].BackgroundImage = imageBytes;
    
    // Set image layout.
    worksheet.Range["A3"].BackgroundImageLayout = BackgroundImageLayout.Stretch;
    worksheet.Range["B3"].BackgroundImageLayout = BackgroundImageLayout.Center;
    worksheet.Range["C3"].BackgroundImageLayout = BackgroundImageLayout.Zoom;
    worksheet.Range["D3"].BackgroundImageLayout = BackgroundImageLayout.None;
    
    // Set PDF export options.
    workbook.ActiveSheet.PageSetup.PrintGridlines = true;
    workbook.ActiveSheet.PageSetup.PrintHeadings = true;
    
    // Save to a PDF file.
    workbook.Save("CellBackgroundImage.pdf");

    Limitations

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