Phillip Pickett <[email protected]> wrote on 24/02/2009 03:28:35 AM:
> I have successfully been able to read and write some simple text strings
> to a doc file but I need to go further with this. My goal is to create a
app
>
> to embed images, text, tables, and headers and footers. Using a graphing
> library I will generate jpgs or gifs and then embed these images into my
doc
>
> file using apache poi. Can anyone point me to an example for inserting
an
> image? Thanks.
>
> -Phil
Example method:
public void addImage( int topLeftX, int topLeftY, int
bottomRightX, int bottomRightY, short startColumn, short endColumn, int
startRow, int endRow, String imageFilePath ) throws MySpreadsheetException
{
int readCount = 0;
byte[] buffer = new byte[20480];
ByteArrayOutputStream fileData = new ByteArrayOutputStream(
20480 );
try {
BufferedInputStream in = new BufferedInputStream( new URL(
imageFilePath ).openConnection().getInputStream() );
while( readCount != -1 ) {
readCount = in.read( buffer );
if ( readCount > -1 )
fileData.write( buffer, 0, readCount );
}
} catch( FileNotFoundException fnfe ) {
logger.warn( imageFilePath + " file not found." );
return;
} catch( IOException ioe ) {
throw new MySpreadsheetException ( Level.FATAL, ioe );
}
String[] expandedPath = imageFilePath.split( "\\." );
String extension = expandedPath[ expandedPath.length - 1 ];
int pictureFormat = extension.equalsIgnoreCase("png") ?
HSSFWorkbook.PICTURE_TYPE_PNG : HSSFWorkbook.PICTURE_TYPE_JPEG;
int imageIndex = workBook.addPicture( fileData.toByteArray(),
pictureFormat );
if ( patriarch == null )
patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor imageAnchor = new HSSFClientAnchor( topLeftX,
topLeftY, bottomRightX, bottomRightY, startColumn, startRow, endColumn,
endRow );
imageAnchor.setAnchorType( 2 ); // Move but don't size with
the underlying cells
patriarch.createPicture( imageAnchor, imageIndex );
}
}
Example Call:
frontSheet.addImage( 550, 0, 1023, 255, (short)2, (short)2,
row.getRowNumber(), row.getRowNumber(), "http://" +
request.getServerName() + "/resources/images/alogo.jpg" );
---------------------------------------------------------------------------------------------------------------
Disclaimer:
The information in this electronic mail message is confidential and may be
legally privileged.
It is intended solely for the addressee. Access to this Internet electronic
mail message by anyone else is
unauthorised. If you are not the intended recipient, any disclosure, copying,
distribution or any action taken
or omitted to be taken in reliance on it is prohibited and may be unlawful. If
you have received this e-mail
by mistake please call the sender immediately on 07 3853 5555 and erase the
original message and any attachments.
The Company accepts no responsibility for any effects this email message or
attachments has on the recipient
network or computer system.
---------------------------------------------------------------------------------------------------------------