Hi.
I want to insert chart into PowerPoint presentation. First of all I have created a chart in Excel and that invoked a toImage() method to convert it to a picture. Then I added this picture to PP slide.
So, the toImage() are not working well. The appearance of an Excel chart and resulting picture is defferent. The category data text has a wrong alignment.
private static String PATH = System.getProperty( “user.home” );
private static String categoryDataText = “Figur 1.2: Det samlede resultat for hver dimension sammenstillet med det valgte benchmark”;
public static void main( String[] args ) throws IOException
{
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().getSheet( 0 );
sheet.setName( “Data” );
Cells cells = workbook.getWorksheets().getSheet( 0 ).getCells();
cells.getCell( “A2” ).setValue( “France” );
cells.getCell( “A3” ).setValue( “Germany” );
cells.getCell( “A4” ).setValue( “England” );
cells.getCell( “B2” ).setValue( 55000 );
cells.getCell( “B3” ).setValue( 70000 );
cells.getCell( “B4” ).setValue( 30000 );
cells.getCell( “A1” ).setValue( categoryDataText );
Chart aChart = sheet.getCharts().addChart( ChartType.BAR_CLUSTERED, 2, 2, 26, 11 );
NSeries nSeries = aChart.getNSeries();
createSeries( nSeries );
aChart.getNSeries().setCategoryData( “A1” );
aChart.getCategoryAxis().setRotation( 90 );
aChart.getCategoryAxis().getDisplayUnitLabel().setTextVerticalAlignment( TextAlignmentType.CENTER );
exportToPP( aChart );
}
private static void exportToPP( Chart aChart )
throws IOException
{
Presentation presentation = new Presentation( );
Slide slide = presentation.addEmptySlide();
ByteArrayOutputStream pictureBytes = new ByteArrayOutputStream();
ImageOptions imageOptions = new ImageOptions();
imageOptions.setImageFormat( ImageFormat.PNG );
imageOptions.setFashion( FileFormatType.EXCEL97TO2003 );
aChart.toImage( pictureBytes, imageOptions );
InputStream inputStream = new ByteArrayInputStream( pictureBytes.toByteArray() );
com.aspose.slides.Picture picture = new com.aspose.slides.Picture( presentation, inputStream );
int pictureId = presentation.getPictures().add( picture );
int pictureWidth = (int) (picture.getWidth() * 5.26);
int pictureHeight = (int) (picture.getHeight() * 5.26);
int slideWidth = slide.getBackground().getWidth();
int slideHeight = slide.getBackground().getHeight();
int pictureX = (slideWidth / 2) - (pictureWidth / 2);
int pictureY = (slideHeight / 2) - (pictureHeight / 2);
slide.getShapes().addPictureFrame( pictureId, pictureX, pictureY, pictureWidth, pictureHeight );
presentation.save( PATH + “sample.ppt”, SaveFormat.PPT );
}
private static void createSeries( NSeries nSeries )
{
ASeries as1 = nSeries.get( nSeries.add() );
as1.setName(“A2”);
as1.setValues( “B2” );
ASeries as2 = nSeries.get( nSeries.add() );
as2.setName(“A3”);
as2.setValues( “B3” );
ASeries as3 = nSeries.get( nSeries.add() );
as3.setName(“A4”);
as3.setValues( “B4” );
}