Question

I want to convert my excel file as well as its entites(charts, tables, images) to jpeg/png images. Currently using aspose for that. Here is my code

public static int excelToImages(final String sourceFilePath, final String outFilePrefix) throws Exception {
    int noOfImages = 0;

    Workbook workbook = getWorkbook(sourceFilePath);
    List<Worksheet> worksheets = getAllWorksheets(workbook);

    if (worksheets != null) {
        for (Worksheet worksheet : worksheets) {
            if (worksheet.getCells().getCount() > 0) {
                String outFilePath = FileUtils.getAbsoluteFilePath(outFilePrefix + (noOfImages++));

                SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions());
                sr.toImage(0, outFilePath);
            }
        }
    }
    return noOfImages;
}

private static ImageOrPrintOptions getImageOrPrintOptions() {
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
    imgOptions.setImageFormat(ImageFormat.getJpeg());
    imgOptions.setOnePagePerSheet(true);
    return imgOptions;
}

private static List<Worksheet> getAllWorksheets(final Workbook workbook) {
    List<Worksheet> worksheets = new ArrayList<Worksheet>();

    WorksheetCollection worksheetCollection = workbook.getWorksheets();
    for (int i = 0; i < worksheetCollection.getCount(); i++) {
        worksheets.add(worksheetCollection.get(i));
    }
    return worksheets;
}

My problem is that size of output image is either split into multiple A4 size or single 1 sheet depends upon the value of

imgOptions.setOnePagePerSheet(true);

Can anybody tell me how I can customize the size of output image file?

Was it helpful?

Solution

You can try it with imgOptions.setOnlyArea(true);. That will set the size of the image to the minimal that's needed to put everything to the image. But I'm not sure if the generated image is split into A4 parts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top