Aspose Cells gets images and converts them into Base64 string

Good morning,
we need to get images from Excel using Aspose Cells and convert them into base64 string because the images are going inserted into MS Word documents using MS Word.js API.
Using our code, the MS Word.js API returns error because the MIME Type of the image is “application/octet-stream” instead of “image/octet-stream”.
Our base64 string is not compatible with the API and with your conversion tool

We use Aspose Cells version 23.8.

This is our code. We have two methods:

  1. Get image from chart
public void getImageBase64FromChart() {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
      ImageOrPrintOptions opts = new ImageOrPrintOptions();
      opts.setImageType(ImageType.EMF);
      opts.setOnePagePerSheet(true);
      opts.setSVGFitToViewPort(true);

      Worksheet worksheet = currentWorkbook.getWorksheets().get("Charts");
      Chart chart = worksheet.getCharts().get("Chart 1");
      chart.toImage(outputStream, opts);

      String result = Base64.getEncoder().encodeToString(outputStream.toByteArray());
      System.out.println("Result: " + result);
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
  1. Get image from range
public void getImageBase64FromRange() {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
      Range range = currentWorkbook.getWorksheets().getRangeByName("Fin_Highlights");

      ImageOrPrintOptions opts = new ImageOrPrintOptions();
      opts.setImageType(ImageType.EMF);
      opts.setOnePagePerSheet(true);
      opts.setSVGFitToViewPort(true);

      Worksheet ws = range.getWorksheet();
      PageSetup pageSetup = ws.getPageSetup();
      pageSetup.setPrintArea(range.getAddress());
      pageSetup.setPrintTitleRows("");
      pageSetup.setPrintTitleColumns("");
      pageSetup.setLeftMargin(0.0);
      pageSetup.setRightMargin(0.0);
      pageSetup.setTopMargin(0.0);
      pageSetup.setBottomMargin(0.0);
      List<Integer> section = IntStream.rangeClosed(0, 2).boxed().collect(Collectors.toList());
      section.forEach(s -> {
        pageSetup.setFirstPageHeader(s, "");
        pageSetup.setHeader(s, "");
        pageSetup.setFirstPageFooter(s, "");
        pageSetup.setFooter(s, "");
      });
      SheetRender renderer = new SheetRender(ws, opts);
      renderer.toImage(0, outputStream);

      String result = Base64.getEncoder().encodeToString(outputStream.toByteArray());
      System.out.println("Result: " + result);
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
    }
  }

We have used “Base64.getEncoder().encodeToString(…)” because we do not found, in Aspose cells, methods or commands that help us to save images in base64 string.
Could you explain us if there is anything that solve our problem?
Thanks
Giulio

@giulio.andolfi,

I simply tested your scenario/case a bit. I extract/render an image from Excel worksheet using SheetRender API. When I copied the base64 string into the app (Convert BASE64 to image Online), it gives invalid based64 string error. But when I used some other online converters (e.g., converter1), it works fine and I get the correct image.

Here is my simplest code (I used a simple Excel file having some data in the first worksheet):
e.g.
Sample code:

        
        // Load the workbook
        Workbook workbook = new Workbook("f:\\files\\xlsx-file.xlsx");

        // Get the worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        ImageOrPrintOptions opts = new ImageOrPrintOptions();
        opts.setImageType(ImageType.JPEG);
        opts.setOnePagePerSheet(true);

        // Render the worksheet to an image
        SheetRender render = new SheetRender(worksheet, opts);
        render.toImage(0, "f:\\files\\out1.jpg");

        BufferedImage originalImage = ImageIO.read(new File("f:\\files\\out1.jpg"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( originalImage, "jpg", baos );
        baos.flush();
        byte[] imageBytes = baos.toByteArray();
        baos.close();
        String base64Image = java.util.Base64.getEncoder().encodeToString(imageBytes);

        java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter("f:\\files\\image1.txt"));
        writer.write(base64Image);
        writer.close();

@amjad.sahi

I tested your code and it works fine in both cases:

But our code works fine and returns right base 64 string if we change ImageType from EMF to JPEG or PNG.
Do you have any idea what we can doing to use EMF as ImageType?

Thanks,
Giulio

@giulio.andolfi,

Yes, I find the issue when using EMF image type:
"MIME detected as “application/octet-stream”, but the decoder displays it as “image/octet-stream”.

May be it is something to do with Base64 encoding to string when handling EMF type. There seems nothing to do on Aspose.Cells part. To confirm this is not an issue with Aspose.Cells APIs, you may use any valid EMF image and encode to string via Base64 API (without involving Aspose.Cells APIs) to check if it works or not.

String base64Image = java.util.Base64.getEncoder().encodeToString(outputStream.toByteArray());