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();
}
}
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);
}
}
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...");
}
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);
}
}
}
Aggregations