package org.apache.poi.xssf.usermodel.examples;\r
\r
import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.util.IOUtils;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
\r
import java.io.IOException;\r
import java.io.InputStream;\r
public static void main(String[] args) throws IOException {\r
\r
//create a new workbook\r
- XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
+ CreationHelper helper = wb.getCreationHelper();\r
\r
//add a picture in this workbook.\r
- InputStream is = new FileInputStream("lilies.jpg");\r
- int pictureIdx = wb.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG);\r
+ InputStream is = new FileInputStream(args[0]);\r
+ byte[] bytes = IOUtils.toByteArray(is);\r
is.close();\r
+ int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);\r
\r
//create sheet\r
- XSSFSheet sheet = wb.createSheet();\r
+ Sheet sheet = wb.createSheet();\r
\r
//create drawing\r
- XSSFDrawing drawing = sheet.createDrawingPatriarch();\r
+ Drawing drawing = sheet.createDrawingPatriarch();\r
\r
//add a picture shape\r
- XSSFPicture pict = drawing.createPicture(new XSSFClientAnchor(), pictureIdx);\r
+ ClientAnchor anchor = helper.createClientAnchor();\r
+ anchor.setCol1(1);\r
+ anchor.setRow1(1);\r
+ Picture pict = drawing.createPicture(anchor, pictureIdx);\r
\r
//auto-size picture\r
- pict.resize();\r
+ pict.resize(2);\r
\r
//save workbook\r
- FileOutputStream fileOut = new FileOutputStream("xssf-picture.xlsx");\r
+ String file = "picture.xls";\r
+ if(wb instanceof XSSFWorkbook) file += "x";\r
+ FileOutputStream fileOut = new FileOutputStream(file);\r
wb.write(fileOut);\r
fileOut.close();\r
\r
}
/**
- * Reset the image to the original size.
+ * Resize the image
*
- * @since POI 3.0.2
+ * @param scale the amount by which image dimensions are multiplied relative to the original size.
+ * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
+ * <code>resize(2.0)</code> resizes to 200% of the original.
*/
- public void resize(){
+ public void resize(double scale){
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
anchor.setAnchorType(2);
- HSSFClientAnchor pref = getPreferredSize();
+ HSSFClientAnchor pref = getPreferredSize(scale);
int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
anchor.setDy2(pref.getDy2());
}
+ /**
+ * Reset the image to the original size.
+ */
+ public void resize(){
+ resize(1.0);
+ }
+
/**
* Calculate the preferred size for this picture.
*
* @since POI 3.0.2
*/
public HSSFClientAnchor getPreferredSize(){
+ return getPreferredSize(1.0);
+ }
+
+ /**
+ * Calculate the preferred size for this picture.
+ *
+ * @param scale the amount by which image dimensions are multiplied relative to the original size.
+ * @return HSSFClientAnchor with the preferred size for this image
+ * @since POI 3.0.2
+ */
+ public HSSFClientAnchor getPreferredSize(double scale){
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
Dimension size = getImageDimension();
+ double scaledWidth = size.getWidth() * scale;
+ double scaledHeight = size.getHeight() * scale;
float w = 0;
//space in the leftmost cell
- w += getColumnWidthInPixels(anchor.col1)*(1 - anchor.dx1/1024);
+ w += getColumnWidthInPixels(anchor.col1)*(1 - (float)anchor.dx1/1024);
short col2 = (short)(anchor.col1 + 1);
int dx2 = 0;
- while(w < size.width){
+ while(w < scaledWidth){
w += getColumnWidthInPixels(col2++);
}
- if(w > size.width) {
+ if(w > scaledWidth) {
//calculate dx2, offset in the rightmost cell
col2--;
- float cw = getColumnWidthInPixels(col2);
- float delta = w - size.width;
+ double cw = getColumnWidthInPixels(col2);
+ double delta = w - scaledWidth;
dx2 = (int)((cw-delta)/cw*1024);
}
anchor.col2 = col2;
anchor.dx2 = dx2;
float h = 0;
- h += (1 - anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
+ h += (1 - (float)anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
int row2 = anchor.row1 + 1;
int dy2 = 0;
- while(h < size.height){
+ while(h < scaledHeight){
h += getRowHeightInPixels(row2++);
}
- if(h > size.height) {
+ if(h > scaledHeight) {
row2--;
- float ch = getRowHeightInPixels(row2);
- float delta = h - size.height;
+ double ch = getRowHeightInPixels(row2);
+ double delta = h - scaledHeight;
dy2 = (int)((ch-delta)/ch*256);
}
anchor.row2 = row2;
* Reset the image to the original size.\r
*/\r
void resize();\r
+\r
+ /**\r
+ * Reset the image to the original size.\r
+ *\r
+ * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+ * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,\r
+ * <code>resize(2.0)</code> resizes to 200% of the original.\r
+ */\r
+ void resize(double scale);\r
}\r
* Reset the image to the original size.\r
*/\r
public void resize(){\r
+ resize(1.0);\r
+ }\r
+\r
+ /**\r
+ * Reset the image to the original size.\r
+ *\r
+ * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+ * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,\r
+ * <code>resize(2.0)</code> resizes to 200% of the original.\r
+ */\r
+ public void resize(double scale){\r
XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor();\r
\r
- XSSFClientAnchor pref = getPreferredSize();\r
+ XSSFClientAnchor pref = getPreferredSize(scale);\r
\r
int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());\r
int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());\r
* @return XSSFClientAnchor with the preferred size for this image\r
*/\r
public XSSFClientAnchor getPreferredSize(){\r
+ return getPreferredSize(1);\r
+ }\r
+\r
+ /**\r
+ * Calculate the preferred size for this picture.\r
+ *\r
+ * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+ * @return XSSFClientAnchor with the preferred size for this image\r
+ */\r
+ public XSSFClientAnchor getPreferredSize(double scale){\r
XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor();\r
\r
XSSFPictureData data = getPictureData();\r
Dimension size = getImageDimension(data.getPackagePart(), data.getPictureType());\r
+ double scaledWidth = size.getWidth() * scale;\r
+ double scaledHeight = size.getHeight() * scale;\r
\r
float w = 0;\r
int col2 = anchor.getCol1();\r
\r
for (;;) {\r
w += getColumnWidthInPixels(col2);\r
- if(w > size.width) break;\r
+ if(w > scaledWidth) break;\r
col2++;\r
}\r
\r
- if(w > size.width) {\r
- float cw = getColumnWidthInPixels(col2 + 1);\r
- float delta = w - size.width;\r
+ if(w > scaledWidth) {\r
+ double cw = getColumnWidthInPixels(col2 + 1);\r
+ double delta = w - scaledWidth;\r
dx2 = (int)(EMU_PER_PIXEL*(cw-delta));\r
}\r
anchor.setCol2(col2);\r
anchor.setDx2(dx2);\r
\r
- float h = 0;\r
+ double h = 0;\r
int row2 = anchor.getRow1();\r
int dy2 = 0;\r
\r
\r
for (;;) {\r
h += getRowHeightInPixels(row2);\r
- if(h > size.height) break;\r
+ if(h > scaledHeight) break;\r
row2++;\r
}\r
\r
- if(h > size.height) {\r
- float ch = getRowHeightInPixels(row2 + 1);\r
- float delta = h - size.height;\r
+ if(h > scaledHeight) {\r
+ double ch = getRowHeightInPixels(row2 + 1);\r
+ double delta = h - scaledHeight;\r
dy2 = (int)(EMU_PER_PIXEL*(ch-delta));\r
}\r
anchor.setRow2(row2);\r
anchor.setDy2(dy2);\r
\r
CTPositiveSize2D size2d = ctPicture.getSpPr().getXfrm().getExt();\r
- size2d.setCx(size.width*EMU_PER_PIXEL);\r
- size2d.setCy(size.height*EMU_PER_PIXEL);\r
+ size2d.setCx((long)(scaledWidth*EMU_PER_PIXEL));\r
+ size2d.setCy((long)(scaledHeight*EMU_PER_PIXEL));\r
\r
return anchor;\r
}\r