Search code examples
javaasposeaspose-cells

Aspose: Generate Thumbnail for first page


I wanted to generate a thumbnail for first page. Tried to generate with the following page, cannot print image with good resolution, that should show the image with good resolution in when zoom. Even if we have option to print thumb nail for first few columns would be fine. Please suggest.

Workbook book = new Workbook(new ByteArrayInputStream(documentData));
    // Define ImageOrPrintOptions
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
    // Set the vertical and horizontal resolution
    imgOptions.setVerticalResolution(200);
    imgOptions.setHorizontalResolution(200);
    // Set the image's format
    imgOptions.setImageFormat(ImageFormat.getJpeg());
    // One page per sheet is enabled
    imgOptions.setOnePagePerSheet(true);
    // Get the first worksheet
    Worksheet sheet = book.getWorksheets().get(0);
    // Render the sheet with respect to specified image/print options
    SheetRender sr = new SheetRender(sheet, imgOptions);
    // Render the image for the sheet
    sr.toImage(0, "mythumb.jpg");
    // Creating Thumbnail   
    java.awt.Image img = ImageIO.read(new File("mythumb.jpg")).getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
    BufferedImage img1 = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    img1.createGraphics().drawImage(
    ImageIO.read(new File("mythumb.jpg")).getScaledInstance(100, 100, img.SCALE_SMOOTH), 0, 0, null);
    return img1;

Solution

  • The quality should be fine if you use some vector image file format. For example, you may try to use Emf as output image format type for the worksheet. If you want to render specific range (in the worksheet) to be rendered in the image, you should try to set your desired printable area before rendering the worksheet, see the sample lines of code which you may add at the start of your code segment: e.g Sample code:

    ........
    // Access the first worksheet
    Worksheet worksheet = book.getWorksheets().get(0);
    
    // Set the print area with your desired range
    worksheet.getPageSetup().setPrintArea("E8:H15");
    
    // Set all margins as 0 to get remove unnecessary white space
    worksheet.getPageSetup().setLeftMargin(0);
    worksheet.getPageSetup().setRightMargin(0);
    worksheet.getPageSetup().setTopMargin(0);
    worksheet.getPageSetup().setBottomMargin(0);
    
    ..........
    

    I am working as Support developer/ Evangelist at Aspose.