Bug 51455 - It would be really nice to be able to set the background picture of a comment
Summary: It would be really nice to be able to set the background picture of a comment
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks: 53010
  Show dependency tree
 
Reported: 2011-06-30 16:02 UTC by Tucker
Modified: 2012-08-12 11:30 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tucker 2011-06-30 16:02:28 UTC
What I would like to have is the ability from the HSSFComment class to set a background picture.  I know that your code preserves pictures if they are already there but I could not find a straight forward, or a not so straight forward way to set the background picture.

If there exists a convoluted way to pull-off setting the background picture of a comment, please let me know.

To add a background picture to a comment in Excel it's not straight forward, to the point where if you weren't walked through it, you probably wouldn't think it was possible. To add a background picture to a comment to a cell, right click on the boarder of the comment(not in the comment itself), select "format comment...", select the "color and lines" tab, click on the color pull-down, select "fill effects", select the picture tab, then you're finally at the dialog that will let you add a picture to the background of a comment.

Thanks for reading my bug and hopefully you'll get around to implemented it in the not too distant future.  This would make my product owners day, if I could problematically add a background picture to a comment
Comment 1 Evgeniy Berlog 2012-08-12 11:30:01 UTC
This problem should be fixed in trunk.

Please try with a nightly build - see download links on http://poi.apache.org/
or build yourself from SVN trunk, see http://poi.apache.org/subversion.html

Here is an example of code you can use to set backgroung image of comment:

public class Test {

    public static void main(String[] args) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheetA = workbook.createSheet();

        int idx = loadPicture("test.png", (HSSFWorkbook) workbook);
        HSSFPatriarch patriarch = (HSSFPatriarch) sheetA.createDrawingPatriarch();
        HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor(0,0,0,0,(short)0,0,(short)10,10));
        comment.setString(new HSSFRichTextString("HSSF POI comment background image"));
        comment.setBackgroundImage(idx);
        FileOutputStream fos = new FileOutputStream("comment_backgroung_image.xls");
        workbook.write(fos);
        fos.close();
    }

    private static int loadPicture( String path, HSSFWorkbook wb ) throws IOException
    {
        int pictureIndex;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try
        {
            fis = new FileInputStream( path);
            bos = new ByteArrayOutputStream( );
            int c;
            while ( (c = fis.read()) != -1)
                bos.write( c );
            pictureIndex = wb.addPicture( bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG );
        }
        finally
        {
            if (fis != null)
                fis.close();
            if (bos != null)
                bos.close();
        }
        return pictureIndex;
    }
}