Example usage for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_EMF

List of usage examples for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_EMF

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_EMF.

Prototype

int PICTURE_TYPE_EMF

To view the source code for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_EMF.

Click Source Link

Document

Extended windows meta file

Usage

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetAddImage.java

License:Open Source License

public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
    cfSpreadSheetData spreadsheet = (cfSpreadSheetData) parameters.get(2);
    String imgPath = parameters.get(1).getString();
    String[] anchor = parameters.get(0).getString().split(",");

    // Check the anchor
    if (anchor.length != 4 && anchor.length != 8)
        throwException(_session, "Invalid Anchor parameter. Must be 4 or 8 comma separated numbers");

    // Determine the file type
    String fileExt = imgPath.substring(imgPath.lastIndexOf(".") + 1).toLowerCase();
    int imgTypeIndex = 0;
    if (fileExt.equals("dib")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_DIB;
    } else if (fileExt.equals("jpg")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_JPEG;
    } else if (fileExt.equals("emf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_EMF;
    } else if (fileExt.equals("pict")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PICT;
    } else if (fileExt.equals("png")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PNG;
    } else if (fileExt.equals("wmf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_WMF;
    } else/*from   w  ww  .  ja va  2s. c om*/
        throwException(_session, "Unknown file type: " + imgPath);

    // Read the file
    FileInputStream fs = null;
    byte[] fileBuffer = null;
    try {
        fs = new FileInputStream(imgPath);
        fileBuffer = org.apache.poi.util.IOUtils.toByteArray(fs);
    } catch (Exception fe) {
        throwException(_session, "Unable to read file: " + imgPath);
    } finally {
        try {
            fs.close();
        } catch (IOException e) {
        }
    }

    // Add the picture
    int imageIndex = spreadsheet.getWorkBook().addPicture(fileBuffer, imgTypeIndex);

    HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
    if (anchor.length == 4) {
        clientAnchor.setRow1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[3]) - 1);
    } else {
        clientAnchor.setDx1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setDy1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setDx2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setDy2(Integer.valueOf(anchor[3]) - 1);

        clientAnchor.setRow1(Integer.valueOf(anchor[4]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[5]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[6]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[7]) - 1);
    }

    // finalise the image to the sheet
    spreadsheet.getActiveSheet().createDrawingPatriarch().createPicture(clientAnchor, imageIndex);

    return cfBooleanData.TRUE;
}