diff options
-rw-r--r-- | poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java | 663 |
1 files changed, 326 insertions, 337 deletions
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java index 2611b5a6e4..6a764d6e32 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/AddDimensionedImage.java @@ -18,12 +18,6 @@ package org.apache.poi.examples.ss; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.URL; -import java.util.Locale; - import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.ClientAnchor; @@ -35,6 +29,12 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.IOUtils; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Locale; + /** * Demonstrates how to add an image to a worksheet and set that images size @@ -44,50 +44,50 @@ import org.apache.poi.util.IOUtils; * left hand corners of the image - can be identified either in the familiar * Excel manner - A1 for instance - or using POI's methodology of a column and * row index where 0, 0 would indicate cell A1. - * + * <p> * The best way to make use of these techniques is to delay adding the image to * the sheet until all other work has been completed. That way, the sizes of * all rows and columns will have been adjusted - assuming that step was * necessary. Even though the anchors type is set to prevent the image moving * or re-sizing, this setting does not have any effect until the sheet is being * viewed using the Excel application. - * + * <p> * The key to the process is the ClientAnchor class. It defines methods that allow * us to define the location of an image by specifying the following; - * - * * How far - in terms of coordinate positions - the image should be inset - * from the left hand border of a cell. - * * How far - in terms of coordinate positions - the image should be inset - * from the from the top of the cell. - * * How far - in terms of coordinate positions - the right hand edge of - * the image should protrude into a cell (measured from the cells left hand - * edge to the images right hand edge). - * * How far - in terms of coordinate positions - the bottom edge of the - * image should protrude into a row (measured from the cells top edge to - * the images bottom edge). - * * The index of the column that contains the cell whose top left hand - * corner should be aligned with the top left hand corner of the image. - * * The index of the row that contains the cell whose top left hand corner - * should be aligned with the images top left hand corner. - * * The index of the column that contains the cell whose top left hand - * corner should be aligned with the images bottom right hand corner - * * The index number of the row that contains the cell whose top left - * hand corner should be aligned with the images bottom right hand corner. - * + * <p> + * * How far - in terms of coordinate positions - the image should be inset + * from the left hand border of a cell. + * * How far - in terms of coordinate positions - the image should be inset + * from the from the top of the cell. + * * How far - in terms of coordinate positions - the right hand edge of + * the image should protrude into a cell (measured from the cells left hand + * edge to the images right hand edge). + * * How far - in terms of coordinate positions - the bottom edge of the + * image should protrude into a row (measured from the cells top edge to + * the images bottom edge). + * * The index of the column that contains the cell whose top left hand + * corner should be aligned with the top left hand corner of the image. + * * The index of the row that contains the cell whose top left hand corner + * should be aligned with the images top left hand corner. + * * The index of the column that contains the cell whose top left hand + * corner should be aligned with the images bottom right hand corner + * * The index number of the row that contains the cell whose top left + * hand corner should be aligned with the images bottom right hand corner. + * <p> * It can be used to add an image into cell A1, for example, in the following * manner; - * - * ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor(); - * - * anchor.setDx1(0); - * anchor.setDy1(0); - * anchor.setDx2(0); - * anchor.setDy2(0); - * anchor.setCol1(0); - * anchor.setRow1(0); - * anchor.setCol2(1); - * anchor.setRow2(1); - * + * <p> + * ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor(); + * <p> + * anchor.setDx1(0); + * anchor.setDy1(0); + * anchor.setDx2(0); + * anchor.setDy2(0); + * anchor.setCol1(0); + * anchor.setRow1(0); + * anchor.setCol2(1); + * anchor.setRow2(1); + * <p> * Taken together, the first four methods define the locations of the top left * and bottom right hand corners of the image if you imagine that the image is * represented by a simple rectangle. The setDx1() and setDy1() methods locate @@ -101,7 +101,7 @@ import org.apache.poi.util.IOUtils; * together identify the cell that should contain the top left hand corner of * the image while setCol2() and setRow2() do the same for the images bottom * right hand corner. - * + * <p> * Knowing that, it is possible to look again at the example above and to see * that the top left hand corner of the image will be located in cell A1 (0, 0) * and it will be aligned with the very top left hand corner of the cell. Likewise, @@ -113,7 +113,7 @@ import org.apache.poi.util.IOUtils; * 'attached' for want of a better word, to a neighbouring cell, then that image * will not increase in size in response to the user dragging the column wider * or the cell higher. - * + * <p> * The following example demonstrates a slightly different way to insert an * image into cell A1 and to ensure that it occupies the whole of the cell. This * is accomplished by specifying the the images bottom right hand corner should be @@ -121,18 +121,18 @@ import org.apache.poi.util.IOUtils; * where the image will not increase in size if the user increases the size of * the enclosing cell - irrespective of the anchors type - but it will reduce in * size if the cell is made smaller. - * - * ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor(); - * - * anchor.setDx1(0); - * anchor.setDy1(0); - * anchor.setDx2(1023); - * anchor.setDy2(255); - * anchor.setCol1(0); - * anchor.setRow1(0); - * anchor.setCol2(0); - * anchor.setRow2(0); - * + * <p> + * ClientAnchor anchor = sheet.getWorkbook().getCreationHelper().createClientAnchor(); + * <p> + * anchor.setDx1(0); + * anchor.setDy1(0); + * anchor.setDx2(1023); + * anchor.setDy2(255); + * anchor.setCol1(0); + * anchor.setRow1(0); + * anchor.setCol2(0); + * anchor.setRow2(0); + * <p> * Note that the final four method calls all pass the same value and seem to * indicate that the images top left hand corner is aligned with the top left * hand corner of cell A1 and that it's bottom right hand corner is also @@ -142,7 +142,7 @@ import org.apache.poi.util.IOUtils; * determining the images coordinates within the cell. They indicate that the * image should occupy - in order - the full width of the column and the full * height of the row. - * + * <p> * The co-ordinate values shown are the maxima; and they are independent of * row height/column width and of the font used. Passing 255 will always result * in the image occupying the full height of the row and passing 1023 will @@ -152,21 +152,21 @@ import org.apache.poi.util.IOUtils; * necessary to perform conversions between Excels characters units, points, * pixels and millimetres in order to establish how many rows/columns an image * should occupy and just what the various insets ought to be. - * + * <p> * Note that the setDx1(int) and setDy1(int) methods of the ClientAchor class * are not made use of in the code that follows. It would be fairly trivial * however to extend this example further and provide methods that would centre * an image within a cell or allow the user to specify that a plain border a * fixed number of millimetres wide should wrap around the image. Those first * two parameters would make this sort of functionality perfectly possible. - * + * <p> * Owing to the various conversions used, the actual size of the image may vary * from that required; testing has so far found this to be in the region of * plus or minus two millimetres. Most likely by modifying the way the * calculations are performed - possibly using double(s) throughout and * rounding the values at the correct point - it is likely that these errors * could be reduced or removed. - * + * <p> * A note concerning Excels image resizing behaviour. The ClientAnchor * class contains a method called setAnchorType(int) which can be used to * determine how Excel will resize an image in response to the user increasing @@ -181,24 +181,24 @@ import org.apache.poi.util.IOUtils; * if the columns width or rows height are reduced. * * @version 1.00 5th August 2009. - * 2.00 26th February 2010. - * Ported to make use of the the SS usermodel classes. - * Ability to reuse the Drawing Patriarch so that multiple images - * can be inserted without unintentionally erasing earlier images. - * Check on image type added; i.e. jpg, jpeg or png. - * The String used to contain the files name is now converted - * into a URL. - * 2.10 17th May 2012 - * Corrected gross error that occurred when using the code with - * XSSF or SXSSF workbooks. In short, the code did not correctly - * calculate the size of the image(s) owing to the use of EMUs - * within the OOXML file format. That problem has largely been - * corrected although it should be mentioned that images are not - * sized with the same level of accuracy. Discrepancies of up to - * 2mm have been noted in testing. Further investigation will - * continue to rectify this issue. + * 2.00 26th February 2010. + * Ported to make use of the the SS usermodel classes. + * Ability to reuse the Drawing Patriarch so that multiple images + * can be inserted without unintentionally erasing earlier images. + * Check on image type added; i.e. jpg, jpeg or png. + * The String used to contain the files name is now converted + * into a URL. + * 2.10 17th May 2012 + * Corrected gross error that occurred when using the code with + * XSSF or SXSSF workbooks. In short, the code did not correctly + * calculate the size of the image(s) owing to the use of EMUs + * within the OOXML file format. That problem has largely been + * corrected although it should be mentioned that images are not + * sized with the same level of accuracy. Discrepancies of up to + * 2mm have been noted in testing. Further investigation will + * continue to rectify this issue. */ -@SuppressWarnings({"java:S106","java:S4823"}) +@SuppressWarnings({"java:S106", "java:S4823"}) public class AddDimensionedImage { // Four constants that determine how - and indeed whether - the rows @@ -230,98 +230,98 @@ public class AddDimensionedImage { /** * Add an image to a worksheet. * - * @param cellNumber A String that contains the location of the cell whose - * top left hand corner should be aligned with the top - * left hand corner of the image; for example "A1", "A2" - * etc. This is to support the familiar Excel syntax. - * Whilst images are not actually inserted into cells - * this provides a convenient method of indicating where - * the image should be positioned on the sheet. - * @param sheet A reference to the sheet that contains the cell referenced - * above. - * @param drawing An instance of the DrawingPatriarch class. This is now - * passed into the method where it was, previously, recovered - * from the sheet in order to allow multiple pictures be - * inserted. If the patriarch was not 'cached in this manner - * each time it was created any previously positioned images - * would be simply over-written. - * @param imageFile An instance of the URL class that encapsulates the name - * of and path to the image that is to be 'inserted into' - * the sheet. - * @param reqImageWidthMM A primitive double that contains the required - * width of the image in millimetres. + * @param cellNumber A String that contains the location of the cell whose + * top left hand corner should be aligned with the top + * left hand corner of the image; for example "A1", "A2" + * etc. This is to support the familiar Excel syntax. + * Whilst images are not actually inserted into cells + * this provides a convenient method of indicating where + * the image should be positioned on the sheet. + * @param sheet A reference to the sheet that contains the cell referenced + * above. + * @param drawing An instance of the DrawingPatriarch class. This is now + * passed into the method where it was, previously, recovered + * from the sheet in order to allow multiple pictures be + * inserted. If the patriarch was not 'cached in this manner + * each time it was created any previously positioned images + * would be simply over-written. + * @param imageFile An instance of the URL class that encapsulates the name + * of and path to the image that is to be 'inserted into' + * the sheet. + * @param reqImageWidthMM A primitive double that contains the required + * width of the image in millimetres. * @param reqImageHeightMM A primitive double that contains the required * height of the image in millimetres. - * @param resizeBehaviour A primitive int whose value will determine how - * the code should react if the image is larger than - * the cell referenced by the cellNumber parameter. - * Four constants are provided to determine what - * should happen; - * AddDimensionedImage.EXPAND_ROW - * AddDimensionedImage.EXPAND_COLUMN - * AddDimensionedImage.EXPAND_ROW_AND_COLUMN - * AddDimensionedImage.OVERLAY_ROW_AND_COLUMN - * @throws java.io.FileNotFoundException If the file containing the image - * cannot be located. - * @throws java.io.IOException If a problem occurs whilst reading the file - * of image data. + * @param resizeBehaviour A primitive int whose value will determine how + * the code should react if the image is larger than + * the cell referenced by the cellNumber parameter. + * Four constants are provided to determine what + * should happen; + * AddDimensionedImage.EXPAND_ROW + * AddDimensionedImage.EXPAND_COLUMN + * AddDimensionedImage.EXPAND_ROW_AND_COLUMN + * AddDimensionedImage.OVERLAY_ROW_AND_COLUMN + * @throws java.io.FileNotFoundException If the file containing the image + * cannot be located. + * @throws java.io.IOException If a problem occurs whilst reading the file + * of image data. * @throws java.lang.IllegalArgumentException If an invalid value is passed * to the resizeBehaviour * parameter. */ public void addImageToSheet(String cellNumber, Sheet sheet, Drawing<?> drawing, - URL imageFile, double reqImageWidthMM, double reqImageHeightMM, - int resizeBehaviour) throws IOException, IllegalArgumentException { + URL imageFile, double reqImageWidthMM, double reqImageHeightMM, + int resizeBehaviour) throws IOException, IllegalArgumentException { // Convert the String into column and row indices then chain the // call to the overridden addImageToSheet() method. CellReference cellRef = new CellReference(cellNumber); this.addImageToSheet(cellRef.getCol(), cellRef.getRow(), sheet, drawing, - imageFile, reqImageWidthMM, reqImageHeightMM,resizeBehaviour); + imageFile, reqImageWidthMM, reqImageHeightMM, resizeBehaviour); } /** * Add an image to a worksheet. * - * @param colNumber A primitive int that contains the index number of a - * column on the worksheet; POI column indices are zero - * based. Together with the rowNumber parameter's value, - * this parameter identifies a cell on the worksheet. The - * images top left hand corner will be aligned with the - * top left hand corner of this cell. - * @param rowNumber A primitive int that contains the index number of a row - * on the worksheet; POI row indices are zero based. - * Together with the rowNumber parameter's value, this - * parameter identifies a cell on the worksheet. The - * images top left hand corner will be aligned with the - * top left hand corner of this cell. - * @param sheet A reference to the sheet that contains the cell identified - * by the two parameters above. - * @param drawing An instance of the DrawingPatriarch class. This is now - * passed into the method where it was, previously, recovered - * from the sheet in order to allow multiple pictures be - * inserted. If the patriarch was not 'cached in this manner - * each time it was created any previously positioned images - * would be simply over-written. - * @param imageFile An instance of the URL class that encapsulates the name - * of and path to the image that is to be 'inserted into' - * the sheet. - * @param reqImageWidthMM A primitive double that contains the required - * width of the image in millimetres. + * @param colNumber A primitive int that contains the index number of a + * column on the worksheet; POI column indices are zero + * based. Together with the rowNumber parameter's value, + * this parameter identifies a cell on the worksheet. The + * images top left hand corner will be aligned with the + * top left hand corner of this cell. + * @param rowNumber A primitive int that contains the index number of a row + * on the worksheet; POI row indices are zero based. + * Together with the rowNumber parameter's value, this + * parameter identifies a cell on the worksheet. The + * images top left hand corner will be aligned with the + * top left hand corner of this cell. + * @param sheet A reference to the sheet that contains the cell identified + * by the two parameters above. + * @param drawing An instance of the DrawingPatriarch class. This is now + * passed into the method where it was, previously, recovered + * from the sheet in order to allow multiple pictures be + * inserted. If the patriarch was not 'cached in this manner + * each time it was created any previously positioned images + * would be simply over-written. + * @param imageFile An instance of the URL class that encapsulates the name + * of and path to the image that is to be 'inserted into' + * the sheet. + * @param reqImageWidthMM A primitive double that contains the required + * width of the image in millimetres. * @param reqImageHeightMM A primitive double that contains the required * height of the image in millimetres. - * @param resizeBehaviour A primitive int whose value will determine how - * the code should react if the image is larger than - * the cell referenced by the colNumber and - * rowNumber parameters. Four constants are provided - * to determine what should happen; - * AddDimensionedImage.EXPAND_ROW - * AddDimensionedImage.EXPAND_COLUMN - * AddDimensionedImage.EXPAND_ROW_AND_COLUMN - * AddDimensionedImage.OVERLAY_ROW_AND_COLUMN - * @throws java.io.FileNotFoundException If the file containing the image - * cannot be located. - * @throws java.io.IOException If a problem occurs whilst reading the file - * of image data. + * @param resizeBehaviour A primitive int whose value will determine how + * the code should react if the image is larger than + * the cell referenced by the colNumber and + * rowNumber parameters. Four constants are provided + * to determine what should happen; + * AddDimensionedImage.EXPAND_ROW + * AddDimensionedImage.EXPAND_COLUMN + * AddDimensionedImage.EXPAND_ROW_AND_COLUMN + * AddDimensionedImage.OVERLAY_ROW_AND_COLUMN + * @throws java.io.FileNotFoundException If the file containing the image + * cannot be located. + * @throws java.io.IOException If a problem occurs whilst reading the file + * of image data. * @throws java.lang.IllegalArgumentException If an invalid value is passed * to the resizeBehaviour * parameter or if the extension @@ -330,19 +330,19 @@ public class AddDimensionedImage { * currently be added to the worksheet. */ public void addImageToSheet(int colNumber, int rowNumber, Sheet sheet, Drawing<?> drawing, - URL imageFile, double reqImageWidthMM, double reqImageHeightMM, - int resizeBehaviour) throws IOException, - IllegalArgumentException { + URL imageFile, double reqImageWidthMM, double reqImageHeightMM, + int resizeBehaviour) throws IOException, + IllegalArgumentException { ClientAnchor anchor; ClientAnchorDetail rowClientAnchorDetail; ClientAnchorDetail colClientAnchorDetail; int imageType; // Validate the resizeBehaviour parameter. - if((resizeBehaviour != AddDimensionedImage.EXPAND_COLUMN) && - (resizeBehaviour != AddDimensionedImage.EXPAND_ROW) && - (resizeBehaviour != AddDimensionedImage.EXPAND_ROW_AND_COLUMN) && - (resizeBehaviour != AddDimensionedImage.OVERLAY_ROW_AND_COLUMN)) { + if ((resizeBehaviour != AddDimensionedImage.EXPAND_COLUMN) && + (resizeBehaviour != AddDimensionedImage.EXPAND_ROW) && + (resizeBehaviour != AddDimensionedImage.EXPAND_ROW_AND_COLUMN) && + (resizeBehaviour != AddDimensionedImage.OVERLAY_ROW_AND_COLUMN)) { throw new IllegalArgumentException("Invalid value passed to the " + "resizeBehaviour parameter of AddDimensionedImage.addImageToSheet()"); } @@ -388,18 +388,16 @@ public class AddDimensionedImage { // to the method, the images type is identified before it is added to the // sheet. String sURL = imageFile.toString().toLowerCase(Locale.ROOT); - if( sURL.endsWith(".png") ) { + if (sURL.endsWith(".png")) { imageType = Workbook.PICTURE_TYPE_PNG; - } - else if( sURL.endsWith(".jpg") || sURL.endsWith(".jpeg") ) { + } else if (sURL.endsWith(".jpg") || sURL.endsWith(".jpeg")) { imageType = Workbook.PICTURE_TYPE_JPEG; - } - else { + } else { throw new IllegalArgumentException("Invalid Image file : " + - sURL); - } + sURL); + } int index = sheet.getWorkbook().addPicture( - IOUtils.toByteArray(imageFile.openStream()), imageType); + IOUtils.toByteArray(imageFile.openStream()), imageType); drawing.createPicture(anchor, index); } @@ -410,9 +408,9 @@ public class AddDimensionedImage { * an ClientAnchor that will fix the image on the sheet and establish * it's size. * - * @param sheet A reference to the sheet that will 'contain' the image. - * @param colNumber A primitive int that contains the index number of a - * column on the sheet. + * @param sheet A reference to the sheet that will 'contain' the image. + * @param colNumber A primitive int that contains the index number of a + * column on the sheet. * @param reqImageWidthMM A primitive double that contains the required * width of the image in millimetres * @param resizeBehaviour A primitive int whose value will indicate how the @@ -420,16 +418,16 @@ public class AddDimensionedImage { * required width of the image is greater than the * width of the column. * @return An instance of the ClientAnchorDetail class that will contain - * the index number of the column containing the cell whose top - * left hand corner also defines the top left hand corner of the - * image, the index number column containing the cell whose top - * left hand corner also defines the bottom right hand corner of - * the image and an inset that determines how far the right hand - * edge of the image can protrude into the next column - expressed - * as a specific number of coordinate positions. + * the index number of the column containing the cell whose top + * left hand corner also defines the top left hand corner of the + * image, the index number column containing the cell whose top + * left hand corner also defines the bottom right hand corner of + * the image and an inset that determines how far the right hand + * edge of the image can protrude into the next column - expressed + * as a specific number of coordinate positions. */ private ClientAnchorDetail fitImageToColumns(Sheet sheet, int colNumber, - double reqImageWidthMM, int resizeBehaviour) { + double reqImageWidthMM, int resizeBehaviour) { double colWidthMM; double colCoordinatesPerMM; @@ -438,18 +436,18 @@ public class AddDimensionedImage { // Get the colum's width in millimetres colWidthMM = ConvertImageUnits.widthUnits2Millimetres( - (short)sheet.getColumnWidth(colNumber)); + (short) sheet.getColumnWidth(colNumber)); // Check that the column's width will accommodate the image at the // required dimension. If the width of the column is LESS than the // required width of the image, decide how the application should // respond - resize the column or overlay the image across one or more // columns. - if(colWidthMM < reqImageWidthMM) { + if (colWidthMM < reqImageWidthMM) { // Should the column's width simply be expanded? - if((resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN) || - (resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) { + if ((resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN) || + (resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) { // Set the width of the column by converting the required image // width from millimetres into Excel's column width units. sheet.setColumnWidth(colNumber, @@ -458,15 +456,14 @@ public class AddDimensionedImage { // the required width of the image into co-ordinates. This value // will become the inset for the ClientAnchorDetail class that // is then instantiated. - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { colWidthMM = reqImageWidthMM; colCoordinatesPerMM = colWidthMM == 0 ? 0 - : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; - pictureWidthCoordinates = (int)(reqImageWidthMM * colCoordinatesPerMM); + : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; + pictureWidthCoordinates = (int) (reqImageWidthMM * colCoordinatesPerMM); - } - else { - pictureWidthCoordinates = (int)reqImageWidthMM * AddDimensionedImage.EMU_PER_MM; + } else { + pictureWidthCoordinates = (int) reqImageWidthMM * AddDimensionedImage.EMU_PER_MM; } colClientAnchorDetail = new ClientAnchorDetail(colNumber, colNumber, pictureWidthCoordinates); @@ -475,28 +472,27 @@ public class AddDimensionedImage { // to expand ONLY the size of the rows, then calculate how to lay // the image out across one or more columns. else if ((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN) || - (resizeBehaviour == AddDimensionedImage.EXPAND_ROW)) { + (resizeBehaviour == AddDimensionedImage.EXPAND_ROW)) { colClientAnchorDetail = this.calculateColumnLocation(sheet, colNumber, reqImageWidthMM); } } // If the column is wider than the image. else { - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { // Mow many co-ordinate positions are there per millimetre? colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / - colWidthMM; + colWidthMM; // Given the width of the image, what should be it's co-ordinate? - pictureWidthCoordinates = (int)(reqImageWidthMM * colCoordinatesPerMM); - } - else { - pictureWidthCoordinates = (int)reqImageWidthMM * + pictureWidthCoordinates = (int) (reqImageWidthMM * colCoordinatesPerMM); + } else { + pictureWidthCoordinates = (int) reqImageWidthMM * AddDimensionedImage.EMU_PER_MM; } colClientAnchorDetail = new ClientAnchorDetail(colNumber, colNumber, pictureWidthCoordinates); } - return(colClientAnchorDetail); + return (colClientAnchorDetail); } /** @@ -506,26 +502,26 @@ public class AddDimensionedImage { * a ClientAnchor that will fix the image on the sheet and establish * it's size. * - * @param sheet A reference to the sheet that will 'contain' the image. - * @param rowNumber A primitive int that contains the index number of a - * row on the sheet. + * @param sheet A reference to the sheet that will 'contain' the image. + * @param rowNumber A primitive int that contains the index number of a + * row on the sheet. * @param reqImageHeightMM A primitive double that contains the required * height of the image in millimetres - * @param resizeBehaviour A primitive int whose value will indicate how the - * height of the row should be adjusted if the - * required height of the image is greater than the - * height of the row. + * @param resizeBehaviour A primitive int whose value will indicate how the + * height of the row should be adjusted if the + * required height of the image is greater than the + * height of the row. * @return An instance of the ClientAnchorDetail class that will contain - * the index number of the row containing the cell whose top - * left hand corner also defines the top left hand corner of the - * image, the index number of the row containing the cell whose - * top left hand corner also defines the bottom right hand - * corner of the image and an inset that determines how far the - * bottom edge of the image can protrude into the next (lower) - * row - expressed as a specific number of coordinate positions. + * the index number of the row containing the cell whose top + * left hand corner also defines the top left hand corner of the + * image, the index number of the row containing the cell whose + * top left hand corner also defines the bottom right hand + * corner of the image and an inset that determines how far the + * bottom edge of the image can protrude into the next (lower) + * row - expressed as a specific number of coordinate positions. */ private ClientAnchorDetail fitImageToRows(Sheet sheet, int rowNumber, - double reqImageHeightMM, int resizeBehaviour) { + double reqImageHeightMM, int resizeBehaviour) { Row row; double rowHeightMM; double rowCoordinatesPerMM; @@ -534,7 +530,7 @@ public class AddDimensionedImage { // Get the row and it's height row = sheet.getRow(rowNumber); - if(row == null) { + if (row == null) { // Create row if it does not exist. row = sheet.createRow(rowNumber); } @@ -546,20 +542,19 @@ public class AddDimensionedImage { // dimensions. If the height of the row is LESS than the required height // of the image, decide how the application should respond - resize the // row or overlay the image across a series of rows. - if(rowHeightMM < reqImageHeightMM) { - if((resizeBehaviour == AddDimensionedImage.EXPAND_ROW) || - (resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) { - row.setHeightInPoints((float)(reqImageHeightMM * + if (rowHeightMM < reqImageHeightMM) { + if ((resizeBehaviour == AddDimensionedImage.EXPAND_ROW) || + (resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) { + row.setHeightInPoints((float) (reqImageHeightMM * ConvertImageUnits.POINTS_PER_MILLIMETRE)); - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { rowHeightMM = reqImageHeightMM; rowCoordinatesPerMM = rowHeightMM == 0 ? 0 - : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM; - pictureHeightCoordinates = (int)(reqImageHeightMM * + : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM; + pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM); - } - else { - pictureHeightCoordinates = (int)(reqImageHeightMM * + } else { + pictureHeightCoordinates = (int) (reqImageHeightMM * AddDimensionedImage.EMU_PER_MM); } rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, @@ -568,7 +563,7 @@ public class AddDimensionedImage { // If the user has chosen to overlay both rows and columns or just // to expand ONLY the size of the columns, then calculate how to lay // the image out ver one or more rows. - else if((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN) || + else if ((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN) || (resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN)) { rowClientAnchorDetail = this.calculateRowLocation(sheet, rowNumber, reqImageHeightMM); @@ -576,19 +571,18 @@ public class AddDimensionedImage { } // Else, if the image is smaller than the space available else { - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / - rowHeightMM; - pictureHeightCoordinates = (int)(reqImageHeightMM * rowCoordinatesPerMM); - } - else { - pictureHeightCoordinates = (int)(reqImageHeightMM * + rowHeightMM; + pictureHeightCoordinates = (int) (reqImageHeightMM * rowCoordinatesPerMM); + } else { + pictureHeightCoordinates = (int) (reqImageHeightMM * AddDimensionedImage.EMU_PER_MM); } rowClientAnchorDetail = new ClientAnchorDetail(rowNumber, - rowNumber, pictureHeightCoordinates); + rowNumber, pictureHeightCoordinates); } - return(rowClientAnchorDetail); + return (rowClientAnchorDetail); } /** @@ -597,21 +591,21 @@ public class AddDimensionedImage { * overlie just a part of one column in order to be presented at the * required size. * - * @param sheet The sheet that will 'contain' the image. - * @param startingColumn A primitive int whose value is the index of the - * column that contains the cell whose top left hand - * corner should be aligned with the top left hand - * corner of the image. + * @param sheet The sheet that will 'contain' the image. + * @param startingColumn A primitive int whose value is the index of the + * column that contains the cell whose top left hand + * corner should be aligned with the top left hand + * corner of the image. * @param reqImageWidthMM A primitive double whose value will indicate the * required width of the image in millimetres. * @return An instance of the ClientAnchorDetail class that will contain - * the index number of the column containing the cell whose top - * left hand corner also defines the top left hand corner of the - * image, the index number column containing the cell whose top - * left hand corner also defines the bottom right hand corner of - * the image and an inset that determines how far the right hand - * edge of the image can protrude into the next column - expressed - * as a specific number of coordinate positions. + * the index number of the column containing the cell whose top + * left hand corner also defines the top left hand corner of the + * image, the index number column containing the cell whose top + * left hand corner also defines the bottom right hand corner of + * the image and an inset that determines how far the right hand + * edge of the image can protrude into the next column - expressed + * as a specific number of coordinate positions. */ private ClientAnchorDetail calculateColumnLocation(Sheet sheet, int startingColumn, @@ -626,9 +620,9 @@ public class AddDimensionedImage { // Calculate how many columns the image will have to // span in order to be presented at the required size. - while(totalWidthMM < reqImageWidthMM) { + while (totalWidthMM < reqImageWidthMM) { colWidthMM = ConvertImageUnits.widthUnits2Millimetres( - (short)(sheet.getColumnWidth(toColumn))); + (short) (sheet.getColumnWidth(toColumn))); // Note use of the cell border width constant. Testing with an image // declared to fit exactly into one column demonstrated that it's // width was greater than the width of the column the POI returned. @@ -649,7 +643,7 @@ public class AddDimensionedImage { // to the toColumn variable. // // Convert both values to ints to perform the test. - if((int)totalWidthMM == (int)reqImageWidthMM) { + if ((int) totalWidthMM == (int) reqImageWidthMM) { // A problem could occur if the image is sized to fit into one or // more columns. If that occurs, the value in the toColumn variable // will be in error. To overcome this, there are two options, to @@ -657,13 +651,12 @@ public class AddDimensionedImage { // total number of co-ordinate positions to the third paramater // of the ClientAnchorDetail constructor. For no sepcific reason, // the latter option is used below. - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { anchorDetail = new ClientAnchorDetail(startingColumn, - toColumn, ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS); - } - else { + toColumn, ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS); + } else { anchorDetail = new ClientAnchorDetail(startingColumn, - toColumn, (int)reqImageWidthMM * AddDimensionedImage.EMU_PER_MM); + toColumn, (int) reqImageWidthMM * AddDimensionedImage.EMU_PER_MM); } } // In this case, the image will overlap part of another column and it is @@ -678,28 +671,27 @@ public class AddDimensionedImage { // the calcaulation above can produce a negative value. To prevent // problems occuring in later caculations, this is simply removed // be setting the overlapMM value to zero. - if(overlapMM < 0) { + if (overlapMM < 0) { overlapMM = 0.0D; } - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { // Next, from the columns width, calculate how many co-ordinate // positons there are per millimetre coordinatePositionsPerMM = (colWidthMM == 0) ? 0 - : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; + : ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; // From this figure, determine how many co-ordinat positions to // inset the left hand or bottom edge of the image. - inset = (int)(coordinatePositionsPerMM * overlapMM); - } - else { - inset = (int)overlapMM * AddDimensionedImage.EMU_PER_MM; + inset = (int) (coordinatePositionsPerMM * overlapMM); + } else { + inset = (int) overlapMM * AddDimensionedImage.EMU_PER_MM; } // Now create the ClientAnchorDetail object, setting the from and to // columns and the inset. anchorDetail = new ClientAnchorDetail(startingColumn, toColumn, inset); } - return(anchorDetail); + return (anchorDetail); } /** @@ -708,24 +700,24 @@ public class AddDimensionedImage { * overlie just a part of one row in order to be presented at the * required size. * - * @param sheet The sheet that will 'contain' the image. - * @param startingRow A primitive int whose value is the index of the row - * that contains the cell whose top left hand corner - * should be aligned with the top left hand corner of - * the image. + * @param sheet The sheet that will 'contain' the image. + * @param startingRow A primitive int whose value is the index of the row + * that contains the cell whose top left hand corner + * should be aligned with the top left hand corner of + * the image. * @param reqImageHeightMM A primitive double whose value will indicate the * required height of the image in millimetres. * @return An instance of the ClientAnchorDetail class that will contain - * the index number of the row containing the cell whose top - * left hand corner also defines the top left hand corner of the - * image, the index number of the row containing the cell whose top - * left hand corner also defines the bottom right hand corner of - * the image and an inset that determines how far the bottom edge - * can protrude into the next (lower) row - expressed as a specific - * number of co-ordinate positions. + * the index number of the row containing the cell whose top + * left hand corner also defines the top left hand corner of the + * image, the index number of the row containing the cell whose top + * left hand corner also defines the bottom right hand corner of + * the image and an inset that determines how far the bottom edge + * can protrude into the next (lower) row - expressed as a specific + * number of co-ordinate positions. */ private ClientAnchorDetail calculateRowLocation(Sheet sheet, - int startingRow, double reqImageHeightMM) { + int startingRow, double reqImageHeightMM) { ClientAnchorDetail clientAnchorDetail; Row row; double rowHeightMM = 0.0D; @@ -737,11 +729,11 @@ public class AddDimensionedImage { // Step through the rows in the sheet and accumulate a total of their // heights. - while(totalRowHeightMM < reqImageHeightMM) { + while (totalRowHeightMM < reqImageHeightMM) { row = sheet.getRow(toRow); // Note, if the row does not already exist on the sheet then create // it here. - if(row == null) { + if (row == null) { row = sheet.createRow(toRow); } // Get the row's height in millimetres and add to the running total. @@ -761,17 +753,15 @@ public class AddDimensionedImage { // To overcome problems that can occur with comparing double values for // equality, cast both to int(s) to truncate the value; VERY crude and // I do not really like it!! - if((int)totalRowHeightMM == (int)reqImageHeightMM) { - if(sheet instanceof HSSFSheet) { + if ((int) totalRowHeightMM == (int) reqImageHeightMM) { + if (sheet instanceof HSSFSheet) { clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, - ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS); - } - else { + ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS); + } else { clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, - (int)reqImageHeightMM * AddDimensionedImage.EMU_PER_MM); + (int) reqImageHeightMM * AddDimensionedImage.EMU_PER_MM); } - } - else { + } else { // Calculate how far the image will project into the next row. Note // that the height of the last row assessed is subtracted from the // total height of all rows assessed so far. @@ -779,45 +769,44 @@ public class AddDimensionedImage { // To prevent an exception being thrown when the required width of // the image is very close indeed to the column size. - if(overlapMM < 0) { + if (overlapMM < 0) { overlapMM = 0.0D; } - if(sheet instanceof HSSFSheet) { + if (sheet instanceof HSSFSheet) { rowCoordinatesPerMM = (rowHeightMM == 0) ? 0 - : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM; - inset = (int)(overlapMM * rowCoordinatesPerMM); - } - else { - inset = (int)overlapMM * AddDimensionedImage.EMU_PER_MM; + : ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM; + inset = (int) (overlapMM * rowCoordinatesPerMM); + } else { + inset = (int) overlapMM * AddDimensionedImage.EMU_PER_MM; } clientAnchorDetail = new ClientAnchorDetail(startingRow, - toRow, inset); + toRow, inset); } - return(clientAnchorDetail); + return (clientAnchorDetail); } /** * The main entry point to the program. It contains code that demonstrates * one way to use the program. - * + * <p> * Note, the code is not restricted to use on new workbooks only. If an * image is to be inserted into an existing workbook. just open that * workbook, gat a reference to a sheet and pass that; - * - * AddDimensionedImage addImage = new AddDimensionedImage(); - * - * File file = new File("....... Existing Workbook ......."); - * FileInputStream fis = new FileInputStream(file); - * Workbook workbook = new HSSFWorkbook(fis); - * HSSFSheet sheet = workbook.getSheetAt(0); - * addImage.addImageToSheet("C3", sheet, "image.jpg", 30, 20, - * AddDimensionedImage.EXPAND.ROW); + * <p> + * AddDimensionedImage addImage = new AddDimensionedImage(); + * <p> + * File file = new File("....... Existing Workbook ......."); + * FileInputStream fis = new FileInputStream(file); + * Workbook workbook = new HSSFWorkbook(fis); + * HSSFSheet sheet = workbook.getSheetAt(0); + * addImage.addImageToSheet("C3", sheet, "image.jpg", 30, 20, + * AddDimensionedImage.EXPAND.ROW); * * @param args the command line arguments */ public static void main(String[] args) throws IOException { - if(args.length < 2){ + if (args.length < 2) { System.err.println("Usage: AddDimensionedImage imageFile outputFile"); return; } @@ -837,21 +826,21 @@ public class AddDimensionedImage { /** * The HSSFClientAnchor class accepts eight arguments. In order, these are; - * - * * How far the left hand edge of the image is inset from the left hand - * edge of the cell - * * How far the top edge of the image is inset from the top of the cell - * * How far the right hand edge of the image is inset from the left - * hand edge of the cell - * * How far the bottom edge of the image is inset from the top of the - * cell. - * * Together, arguments five and six determine the column and row - * coordinates of the cell whose top left hand corner will be aligned - * with the images top left hand corner. - * * Together, arguments seven and eight determine the column and row - * coordinates of the cell whose top left hand corner will be aligned - * with the images bottom right hand corner. - * + * <p> + * * How far the left hand edge of the image is inset from the left hand + * edge of the cell + * * How far the top edge of the image is inset from the top of the cell + * * How far the right hand edge of the image is inset from the left + * hand edge of the cell + * * How far the bottom edge of the image is inset from the top of the + * cell. + * * Together, arguments five and six determine the column and row + * coordinates of the cell whose top left hand corner will be aligned + * with the images top left hand corner. + * * Together, arguments seven and eight determine the column and row + * coordinates of the cell whose top left hand corner will be aligned + * with the images bottom right hand corner. + * <p> * An instance of the ClientAnchorDetail class provides three of the eight * parameters, one of the coordinates for the images top left hand corner, * one of the coordinates for the images bottom right hand corner and @@ -873,12 +862,12 @@ public class AddDimensionedImage { * @param fromIndex A primitive int that contains one of the * coordinates (row or column index) for the top left * hand corner of the image. - * @param toIndex A primitive int that contains one of the - * coordinates (row or column index) for the bottom - * right hand corner of the image. - * @param inset A primitive int that contains a value which indicates - * how far the image should be inset from the top or the - * left hand edge of a cell. + * @param toIndex A primitive int that contains one of the + * coordinates (row or column index) for the bottom + * right hand corner of the image. + * @param inset A primitive int that contains a value which indicates + * how far the image should be inset from the top or the + * left hand edge of a cell. */ public ClientAnchorDetail(int fromIndex, int toIndex, int inset) { this.fromIndex = fromIndex; @@ -892,10 +881,10 @@ public class AddDimensionedImage { * corner of the image. * * @return The value - row or column index - for one of the coordinates - * of the top left hand corner of the image. + * of the top left hand corner of the image. */ public int getFromIndex() { - return(this.fromIndex); + return (this.fromIndex); } /** @@ -904,20 +893,20 @@ public class AddDimensionedImage { * corner of the image. * * @return The value - row or column index - for one of the coordinates - * of the bottom right hand corner of the image. + * of the bottom right hand corner of the image. */ public int getToIndex() { - return(this.toIndex); + return (this.toIndex); } /** * Get the images offset from the edge of a cell. * * @return How far either the right hand or bottom edge of the image is - * inset from the left hand or top edge of a cell. + * inset from the left hand or top edge of a cell. */ public int getInset() { - return(this.inset); + return (this.inset); } } @@ -927,9 +916,9 @@ public class AddDimensionedImage { * various constants that are required in other calculations. * * @version 1.01 30th July 2009. - * Added by Mark Beardsley [msb at apache.org]. - * Additional constants. - * widthUnits2Millimetres() and millimetres2Units() methods. + * Added by Mark Beardsley [msb at apache.org]. + * Additional constants. + * widthUnits2Millimetres() and millimetres2Units() methods. */ public static class ConvertImageUnits { @@ -954,11 +943,11 @@ public class AddDimensionedImage { public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D; public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256; public static final int UNIT_OFFSET_LENGTH = 7; - private static final int[] UNIT_OFFSET_MAP = { 0, 36, 73, 109, 146, 182, 219 }; + private static final int[] UNIT_OFFSET_MAP = {0, 36, 73, 109, 146, 182, 219}; /** - * pixel units to excel width units(units of 1/256th of a character width) - */ + * pixel units to excel width units(units of 1/256th of a character width) + */ public static short pixel2WidthUnits(int pxs) { short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH)); @@ -985,11 +974,11 @@ public class AddDimensionedImage { * @param widthUnits The width of the column or the height of the * row in Excels units. * @return A primitive double that contains the columns width or rows - * height in millimetres. + * height in millimetres. */ public static double widthUnits2Millimetres(short widthUnits) { - return(ConvertImageUnits.widthUnits2Pixel(widthUnits) / - ConvertImageUnits.PIXELS_PER_MILLIMETRES); + return (ConvertImageUnits.widthUnits2Pixel(widthUnits) / + ConvertImageUnits.PIXELS_PER_MILLIMETRES); } /** @@ -998,10 +987,10 @@ public class AddDimensionedImage { * @param millimetres A primitive double that contains the columns * width or rows height in millimetres. * @return A primitive int that contains the columns width or rows - * height in Excels units. + * height in Excels units. */ public static int millimetres2WidthUnits(double millimetres) { - return(ConvertImageUnits.pixel2WidthUnits((int)(millimetres * + return (ConvertImageUnits.pixel2WidthUnits((int) (millimetres * ConvertImageUnits.PIXELS_PER_MILLIMETRES))); } } |