Search in sources :

Example 1 with Picture

use of org.apache.poi.ss.usermodel.Picture in project poi by apache.

the class WorkingWithPictures method main.

public static void main(String[] args) throws IOException {
    //create a new workbook
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    try {
        CreationHelper helper = wb.getCreationHelper();
        //add a picture in this workbook.
        InputStream is = new FileInputStream(args[0]);
        byte[] bytes = IOUtils.toByteArray(is);
        is.close();
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        //create sheet
        Sheet sheet = wb.createSheet();
        //create drawing
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        //add a picture shape
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(1);
        anchor.setRow1(1);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        //auto-size picture
        pict.resize(2);
        //save workbook
        String file = "picture.xls";
        if (wb instanceof XSSFWorkbook) {
            // NOSONAR
            file += "x";
        }
        OutputStream fileOut = new FileOutputStream(file);
        try {
            wb.write(fileOut);
        } finally {
            fileOut.close();
        }
    } finally {
        wb.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 2 with Picture

use of org.apache.poi.ss.usermodel.Picture in project poi by apache.

the class EmbeddedExtractor method extractAll.

protected void extractAll(ShapeContainer<?> parent, List<EmbeddedData> embeddings) throws IOException {
    for (Shape shape : parent) {
        EmbeddedData data = null;
        if (shape instanceof ObjectData) {
            ObjectData od = (ObjectData) shape;
            try {
                if (od.hasDirectoryEntry()) {
                    data = extractOne((DirectoryNode) od.getDirectory());
                } else {
                    String contentType = CONTENT_TYPE_BYTES;
                    if (od instanceof XSSFObjectData) {
                        contentType = ((XSSFObjectData) od).getObjectPart().getContentType();
                    }
                    data = new EmbeddedData(od.getFileName(), od.getObjectData(), contentType);
                }
            } catch (Exception e) {
                LOG.log(POILogger.WARN, "Entry not found / readable - ignoring OLE embedding", e);
            }
        } else if (shape instanceof Picture) {
            data = extractOne((Picture) shape);
        } else if (shape instanceof ShapeContainer) {
            extractAll((ShapeContainer<?>) shape, embeddings);
        }
        if (data == null) {
            continue;
        }
        data.setShape(shape);
        String filename = data.getFilename();
        String extension = (filename == null || filename.lastIndexOf('.') == -1) ? ".bin" : filename.substring(filename.lastIndexOf('.'));
        // try to find an alternative name
        if (filename == null || "".equals(filename) || filename.startsWith("MBD") || filename.startsWith("Root Entry")) {
            filename = shape.getShapeName();
            if (filename != null) {
                filename += extension;
            }
        }
        // default to dummy name
        if (filename == null || "".equals(filename)) {
            filename = "picture_" + embeddings.size() + extension;
        }
        filename = filename.trim();
        data.setFilename(filename);
        embeddings.add(data);
    }
}
Also used : Shape(org.apache.poi.ss.usermodel.Shape) Picture(org.apache.poi.ss.usermodel.Picture) ObjectData(org.apache.poi.ss.usermodel.ObjectData) XSSFObjectData(org.apache.poi.xssf.usermodel.XSSFObjectData) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) XSSFObjectData(org.apache.poi.xssf.usermodel.XSSFObjectData) ShapeContainer(org.apache.poi.ss.usermodel.ShapeContainer) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) IOException(java.io.IOException)

Example 3 with Picture

use of org.apache.poi.ss.usermodel.Picture in project Aspose.Cells-for-Java by aspose-cells.

the class ApacheAddImage method main.

public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(ApacheAddImage.class);
    // create a new workbook
    // or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    // add picture data to this workbook.
    InputStream is = new FileInputStream(dataDir + "aspose.jpg");
    byte[] bytes = IOUtils.toByteArray(is);
    int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
    is.close();
    CreationHelper helper = wb.getCreationHelper();
    // create sheet
    Sheet sheet = wb.createSheet();
    // Create the drawing patriarch.  This is the top level container for all shapes.
    Drawing drawing = sheet.createDrawingPatriarch();
    // add a picture shape
    ClientAnchor anchor = helper.createClientAnchor();
    // set top-left corner of the picture,
    // subsequent call of Picture#resize() will operate relative to it
    anchor.setCol1(3);
    anchor.setRow1(2);
    Picture pict = drawing.createPicture(anchor, pictureIdx);
    // auto-size picture relative to its top-left corner
    pict.resize();
    // save workbook
    String file = dataDir + "ApacheImage.xls";
    if (wb instanceof XSSFWorkbook)
        file += "x";
    FileOutputStream fileOut = new FileOutputStream(file);
    wb.write(fileOut);
    fileOut.close();
    System.out.println("Done...");
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 4 with Picture

use of org.apache.poi.ss.usermodel.Picture in project nebula.widgets.nattable by eclipse.

the class PoiExcelExporter method exportImage.

/**
 * Adds a picture to the workbook at the given cell position.
 *
 * @param is
 *            The {@link InputStream} to access the picture. This will be
 *            automatically closed after reading.
 * @param xlCell
 *            The {@link Cell} to position the image to.
 * @since 1.5
 */
protected void exportImage(InputStream is, Cell xlCell) {
    try {
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = this.xlWorkbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        if (this.helper == null) {
            this.helper = this.xlWorkbook.getCreationHelper();
        }
        // This is the top level container for all shapes.
        if (this.drawing == null) {
            this.drawing = this.xlSheet.createDrawingPatriarch();
        }
        // add a picture shape
        ClientAnchor anchor = this.helper.createClientAnchor();
        // set top-left corner of the picture,
        // subsequent call of Picture#resize() will operate relative to it
        anchor.setCol1(xlCell.getColumnIndex());
        anchor.setRow1(xlCell.getRowIndex());
        Picture pict = this.drawing.createPicture(anchor, pictureIdx);
        // auto-size picture relative to its top-left corner
        pict.resize();
    } catch (IOException e) {
        // $NON-NLS-1$
        LOG.error("Error on transforming the image input stream to byte array", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // $NON-NLS-1$
            LOG.error("Error on closing the image input stream", e);
        }
    }
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) IOException(java.io.IOException)

Aggregations

Picture (org.apache.poi.ss.usermodel.Picture)4 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 OutputStream (java.io.OutputStream)1 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)1 Ole10NativeException (org.apache.poi.poifs.filesystem.Ole10NativeException)1 Drawing (org.apache.poi.ss.usermodel.Drawing)1 ObjectData (org.apache.poi.ss.usermodel.ObjectData)1 Shape (org.apache.poi.ss.usermodel.Shape)1 ShapeContainer (org.apache.poi.ss.usermodel.ShapeContainer)1 XSSFObjectData (org.apache.poi.xssf.usermodel.XSSFObjectData)1