Comments modified re re-sizing behaviour

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@921819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Beardsley 2010-03-11 12:12:54 +00:00
parent 918f1a496d
commit fd003e28c5

View File

@ -18,6 +18,8 @@
package org.apache.poi.hssf.usermodel.examples; package org.apache.poi.hssf.usermodel.examples;
package bookfromtemplate;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -122,6 +124,19 @@ import org.apache.poi.hssf.util.CellReference;
* rounding the values at the correct point - it is likely that these errors * rounding the values at the correct point - it is likely that these errors
* could be reduced or removed. * could be reduced or removed.
* *
* A note concerning Excels' image resizing behaviour. The HSSFClientAnchor
* class contains a method called setAnchorType(int) which can be used to
* determine how Excel will resize an image in reponse to the user increasing
* or decreasing the dimensions of the cell containing the image. There are
* three values that can be passed to this method; 0 = To move and size the
* image with the cell, 2 = To move but don't size the image with the cell,
* 3 = To prevent the image from moving or being resized along with the cell. If
* an image is inserted using this class and placed into a single cell then if
* the setAnchorType(int) method is called and a value of either 0 or 2 passed
* to it, the resultant resizing behaviour may be a surprise. The image will not
* grow in size of the column is made wider or the row higher but it will shrink
* if the columns width or rows height are reduced.
*
* @author Mark Beardsley [msb at apache.org] * @author Mark Beardsley [msb at apache.org]
* @version 1.00 5th August 2009. * @version 1.00 5th August 2009.
*/ */
@ -238,6 +253,7 @@ public class AddDimensionedImage {
String imageFile, double reqImageWidthMM, double reqImageHeightMM, String imageFile, double reqImageWidthMM, double reqImageHeightMM,
int resizeBehaviour) throws FileNotFoundException, IOException, int resizeBehaviour) throws FileNotFoundException, IOException,
IllegalArgumentException { IllegalArgumentException {
HSSFRow row = null;
HSSFClientAnchor anchor = null; HSSFClientAnchor anchor = null;
HSSFPatriarch patriarch = null; HSSFPatriarch patriarch = null;
ClientAnchorDetail rowClientAnchorDetail = null; ClientAnchorDetail rowClientAnchorDetail = null;
@ -278,13 +294,15 @@ public class AddDimensionedImage {
// For now, set the anchor type to do not move or resize the // For now, set the anchor type to do not move or resize the
// image as the size of the row/column is adjusted. This could easilly // image as the size of the row/column is adjusted. This could easilly
// become another parameter passed to the method. // become another parameter passed to the method.
anchor.setAnchorType(HSSFClientAnchor.DONT_MOVE_AND_RESIZE); //anchor.setAnchorType(HSSFClientAnchor.DONT_MOVE_AND_RESIZE);
anchor.setAnchorType(HSSFClientAnchor.MOVE_AND_RESIZE);
// Now, add the picture to the workbook. Note that the type is assumed // Now, add the picture to the workbook. Note that the type is assumed
// to be a JPEG/JPG, this could easily (and should) be parameterised // to be a JPEG/JPG, this could easily (and should) be parameterised
// however. // however.
int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile), //int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile),
HSSFWorkbook.PICTURE_TYPE_JPEG); // HSSFWorkbook.PICTURE_TYPE_JPEG);
int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile), HSSFWorkbook.PICTURE_TYPE_PNG);
// Get the drawing patriarch and create the picture. // Get the drawing patriarch and create the picture.
patriarch = sheet.createDrawingPatriarch(); patriarch = sheet.createDrawingPatriarch();
@ -484,6 +502,7 @@ public class AddDimensionedImage {
double colWidthMM = 0.0D; double colWidthMM = 0.0D;
double overlapMM = 0.0D; double overlapMM = 0.0D;
double coordinatePositionsPerMM = 0.0D; double coordinatePositionsPerMM = 0.0D;
int fromNumber = startingColumn;
int toColumn = startingColumn; int toColumn = startingColumn;
int inset = 0; int inset = 0;
@ -541,7 +560,7 @@ public class AddDimensionedImage {
// Next, from the columns width, calculate how many co-ordinate // Next, from the columns width, calculate how many co-ordinate
// positons there are per millimetre // positons there are per millimetre
coordinatePositionsPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / coordinatePositionsPerMM = ExcelUtil.TOTAL_COLUMN_COORDINATE_POSITIONS /
colWidthMM; colWidthMM;
// From this figure, determine how many co-ordinat positions to // From this figure, determine how many co-ordinat positions to
// inset the left hand or bottom edge of the image. // inset the left hand or bottom edge of the image.
@ -704,16 +723,18 @@ public class AddDimensionedImage {
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
File file = null;
FileInputStream fis = null;
FileOutputStream fos = null; FileOutputStream fos = null;
HSSFWorkbook workbook = null; HSSFWorkbook workbook = null;
HSSFSheet sheet = null; HSSFSheet sheet = null;
try { try {
workbook = new HSSFWorkbook(); workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Picture Test"); sheet = workbook.createSheet("Picture Test");
new AddDimensionedImage().addImageToSheet("B5", sheet, new AddDimensionedImage().addImageToSheet("A1", sheet,
"image.jpg", 25, 25, "C:/temp/1.png", 25, 25,
AddDimensionedImage.OVERLAY_ROW_AND_COLUMN); AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
fos = new FileOutputStream("Workbook.xls"); fos = new FileOutputStream("C:/temp/Newly Auto Adjusted.xls");
workbook.write(fos); workbook.write(fos);
} }
catch(FileNotFoundException fnfEx) { catch(FileNotFoundException fnfEx) {
@ -764,7 +785,7 @@ public class AddDimensionedImage {
* either how far the image should be inset from the top or the left hand * either how far the image should be inset from the top or the left hand
* edge of the cell. * edge of the cell.
* *
* @author Mark Beardsley [msb at apache.org] * @author Mark Beardsley [mas at apache.org]
* @version 1.00 5th August 2009. * @version 1.00 5th August 2009.
*/ */
public class ClientAnchorDetail { public class ClientAnchorDetail {
@ -888,7 +909,7 @@ public class AddDimensionedImage {
int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR)
* UNIT_OFFSET_LENGTH; * UNIT_OFFSET_LENGTH;
int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR; int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
pixels += Math.round(offsetWidthUnits / pixels += Math.round((float) offsetWidthUnits /
((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH)); ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
return pixels; return pixels;
} }