Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Convert to Image
In This Topic
    Convert to Image
    In This Topic

    DsExcel allows you to convert a worksheet, any specified range and various shape types to images. Hence, making it convenient to use the converted images directly in other documents, like Word, PDF or a PPT document. The supported image formats for conversion are PNG, JPG/JPEG, SVG, and GIF.

    DsExcel also provides various methods in ImageSaveOptions class that can be used to modify and adjust the image when exporting a worksheet, a range, or a shape to an image file through their respective interfaces using toImage method.

    Following are the methods of ImageSaveOptions class, along with the scope in which they can be used:

    Methods Worksheet Range Shape Description
    ScaleX and ScaleY Yes Yes Yes Gets or sets the scale of exported image file.
    Resolution Yes Yes Yes Gets or sets the jpeg file's DPI in exported image file.
    BackgroundColor Yes Yes Yes Gets or sets the background color of the exported image file.
    ShowRowHeadings Yes Yes No Gets or sets whether to display row headings in exported image file.
    ShowColumnHeadings Yes Yes No Gets or sets whether to display column headings in exported image file.
    ShowGridlines Yes Yes No Gets or sets whether to display gridlines in exported image file.
    GridlineColor Yes Yes No Gets or sets the gridlines color in exported image file.
    ShowDrawingObjects Yes Yes No Gets or sets whether to display drawing objects (charts, shapes, or pictures) in exported image file.
    BlackAndWhite Yes Yes Yes Gets or sets whether to export black and white image.
    Note: To convert images with transparency, PNG like image format should be used as JPG/JPEG format doesn't support image transparency.

    Convert Worksheet to Image

    A worksheet can be converted to image using the toImage method of IWorksheet interface. The converted image displays the rectangular area of the worksheet enclosed under cell A1 and the last cell where any data or shape is present. For eg, if a worksheet contains a shape or data in the range D5:F9, the converted image will display the area under the range A1:F9.  

    A blank worksheet cannot be converted to image.

    Refer to the following example code to convert a worksheet to an image with or without ImageSaveOptions. In the following example code, the ImageSaveOptions modify the scale, display property of row and column headings, drawing objects, and gridlines, and change the background and gridline color.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Add data
    worksheet.getRange("A1").setValue("Sales Report");
    worksheet.getRange("A1").getFont().setColor(Color.FromArgb(56, 93, 171));
    worksheet.getRange("A1").getFont().setSize(24);
    worksheet.getRange("A1").getFont().setBold(true);
    worksheet.getRange("A3:E7")
            .setValue(new Object[][] { { "Date", "Product", "Customer", "Amount", "Show" },
                    { "1/1/2021", "Bose 785593-0050", "Fabrikam, Inc.", "$1,886.00", "1" },
                    { "1/3/2021", "Canon EOS 1500D", "Alpine Ski House", "$4,022.00", "" },
                    { "1/4/2021", "Haier 394L 4Star", "Coho Winery", "$8,144.00", "" },
                    { "1/7/2021", "IFB 6.5 Kg FullyAuto", "Southridge Video", "$8,002.00", "1" } });
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setShowRowHeadings(true);
    options.setShowColumnHeadings(false);
    options.setShowDrawingObjects(true);
    options.setBackgroundColor(Color.FromArgb(226, 231, 243));
    options.setShowGridlines(true);
    options.setGridlineColor(Color.FromArgb(145, 167, 214));
    
    // Save worksheet to image without ImageSaveOptions
    worksheet.toImage("WorksheetToImage.png");
    
    // Save worksheet to image using ImageSaveOptions
    worksheet.toImage("WorksheetToImage_UsingImageSaveOptions.png", options);

    Refer to the following example code to convert a worksheet to an image with or without ImageSaveOptions from an existing file.

    Java
    Copy Code
    // Create a new workbook
    var workbook = new Workbook();
    // Open a xlsx file
    workbook.open("Workbook.xlsx");
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setShowRowHeadings(true);
    options.setShowColumnHeadings(false);
    options.setShowDrawingObjects(true);
    options.setBackgroundColor(Color.FromArgb(226, 231, 243));
    options.setShowGridlines(true);
    options.setGridlineColor(Color.FromArgb(145, 167, 214));
    
    // Create a png file stream
    OutputStream out;
    try {
        // Create a png file stream
        out = new FileOutputStream("ConvertWorksheetToImage.png");
        // Export the worksheet to image without ImageSaveOptions
        worksheet.toImage(out, ImageType.PNG);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    OutputStream outputStreamoptions;
    try {
        // Create another png file stream
        outputStreamoptions = new FileOutputStream("ConvertWorksheetToImage_UsingImageSaveOptions.png");
        // Export the worksheet to image using ImageSaveOptions
        worksheet.toImage(outputStreamoptions, ImageType.PNG, options);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Convert Range to Image

    A specific range in a worksheet can be converted to image using the toImage method of the IRange interface. The resulting image displays the rectangular area of the worksheet enclosed under the specified range.

    Refer to the following example code to convert a specified range to an image with or without ImageSaveOptions. In the following example code, the ImageSaveOptions modify the scale, display property of row and column headings, drawing objects, gridlines, and change the background and gridline color.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Add data
    worksheet.getRange("D10:F10").setValue(new String[] { "Device", "Quantity", "Unit Price" });
    worksheet.getRange("D11:F14").setValue(new Object[][] { { "T540p", 12, 9850 }, { "T570", 5, 7460 },
            { "Y460", 6, 5400 }, { "Y460F", 8, 6240 } });
    
    IRange range = worksheet.getRange("D10:F14");
    
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setShowRowHeadings(true);
    options.setShowColumnHeadings(false);
    options.setShowDrawingObjects(true);
    options.setBackgroundColor(Color.FromArgb(226, 231, 243));
    options.setShowGridlines(true);
    options.setGridlineColor(Color.FromArgb(145, 167, 214));
    
    // Save range to image without ImageSaveOptions
    range.toImage("RangeToImage.png");
    
    // Save range to image using ImageSaveOptions
    range.toImage("RangeToImage_UsingImageSaveOptions.png", options);

    Refer to the following example code to convert a specified range to an image with or without ImageSaveOptions from an existing file.

    Java
    Copy Code
    // Create a new workbook
    var workbook = new Workbook();
    
    InputStream openFile;
    try {
        openFile = new FileInputStream("RangeWorkbook.xlsx");
        // Open a xlsx file contains data in a range
        workbook.open(openFile, OpenFileFormat.Xlsx);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setShowRowHeadings(true);
    options.setShowColumnHeadings(false);
    options.setShowDrawingObjects(true);
    options.setBackgroundColor(Color.FromArgb(226, 231, 243));
    options.setShowGridlines(true);
    options.setGridlineColor(Color.FromArgb(145, 167, 214));
    
    OutputStream out;
    try {
        // Create a png file stream
        out = new FileOutputStream("ConvertRangeToImage.png");
        // Export the range to image without ImageSaveOptions
        worksheet.getRange("A1:C5").toImage(out, ImageType.PNG);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    OutputStream outputStreamoptions;
    try {
        // Create another png file stream
        outputStreamoptions = new FileOutputStream("ConvertRangeToImage_UsingImageSaveOptions.png");
        // Export the range to image using ImageSaveOptions
        worksheet.getRange("A1:C5").toImage(outputStreamoptions, ImageType.PNG, options);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Convert Shape to Image

    DsExcel allows you to convert various shape types to image using the toImage method of the IShape interface. The shape types include shapes like picture, slicer, chart and autoshape. The resulting image displays the rectangular area of the worksheet enclosed under the shape.

    Refer to the following example code to convert an autoshape to an image with or without ImageSaveOptions. In the following example code, the ImageSaveOptions modify the scale and change the background color.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Add an oval
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 20, 20, 200, 100);
    
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setBackgroundColor(Color.GetLimeGreen());
    
    // Save shape to image without ImageSaveOptions
    shape.toImage("ShapeToImage.png");
    
    // Save shape to image using ImageSaveOptions
    shape.toImage("ShapeToImage_UsingImageSaveOptions.png", options);

    Refer to the following example code to convert an autoshape to an image with or without ImageSaveOptions from an existing file.

    Java
    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    
    // Open a xlsx file contains a group shape
    workbook.open("ShapeWorkbook.xlsx");
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Instantiate ImageSaveOptions and configure the properties
    ImageSaveOptions options = new ImageSaveOptions();
    options.setScaleX(3.0);
    options.setScaleY(2.0);
    options.setBackgroundColor(Color.GetLime());
    
    OutputStream out;
    try {
        // Create a png file stream
        out = new FileOutputStream("ConvertShapeToImage.png");
        // Export the shape to image without ImageSaveOptions
        worksheet.getShapes().get(0).toImage(out, ImageType.PNG);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    OutputStream outputStreamoptions;
    try {
        // Create another png file stream
        outputStreamoptions = new FileOutputStream("ConvertShapeToImage.png");
        // Export the shape to image using ImageSaveOptions
        worksheet.getShapes().get(0).toImage(outputStreamoptions, ImageType.PNG, options);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Refer to the following example code to convert a chart to image.

    C#
    Copy Code
    // create a new workbook
     Workbook workbook = new Workbook();
     IWorksheet worksheet = workbook.getWorksheets().get(0);
    
     // Prepare data for chart
     worksheet.getRange("A1:D4")
             .setValue(new Object[][] { { null, "Q1", "Q2", "Q3" }, { "Mobile Phones", 1330, 2345, 3493 },
                     { "Laptops", 2032, 3632, 2197 }, { "Tablets", 6233, 3270, 2030 } });
    
     worksheet.getRange("A:D").getColumns().autoFit();
    
     // Add Area Chart
     IShape shape = worksheet.getShapes().addChart(ChartType.Area, 250, 20, 360, 230);
    
     // Add series to SeriesCollection
     shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D4"), RowCol.Columns, true, true);
    
     // Configure Chart Title
     shape.getChart().getChartTitle().getTextFrame().getTextRange().getParagraphs().add("Annual Sales Record");
    
     // Save chart to image
     shape.toImage("ConvertChartToImage.png");

    Refer to the following example code to convert a chart to image from existing file.

    C#
    Copy Code
    // create a png file stream
      FileOutputStream outputStream = new
      FileOutputStream("ConvertChartToImage.png");
      
      // create a new workbook 
      Workbook workbook = new Workbook();
      
      FileInputStream fileStream = new FileInputStream("scatterchart.xlsx");
      
      // Open a xlsx file contains a chart 
      workbook.open(fileStream); 
      IWorksheet worksheet = workbook.getWorksheets().get(0);
      
      // Export the chart to image
      worksheet.getShapes().get(0).toImage(outputStream, ImageType.PNG);
    Note: ToImage method does not support exporting images in EMF or WMF image formats.