Accessing Microsoft PowerPoint from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Microsoft PowerPoint from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Microsoft PowerPoint. It provides bi-directional access of Java objects and COM components.

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about Microsoft PowerPoint programming, please refer to the Microsoft PowerPoint VBA Language Reference. The quickest way to start programming Java-to-PowerPoint is to find a VB example first, and then map the VB example to Java.

Refer to the Excel example for the details of steps.

Example 1

This example shows how to create a new PowerPoint file.

/* This example is for PowerPoint XP.
 *
 * It assumes that the 'com2java' tool has been run on the Powerpoint type library
 * C:\Program Files\Microsoft Office\Office10\MSPPT.OLB
 * and that the pure Java files that were generated are in a subdirectory called 'powerpoint'
 */

import com.linar.jintegra.AuthInfo;
import powerpoint.*;

import java.util.Date;

public class PowerpointExample {

  public static void main(java.lang.String[] args) {

    try {
      // If running this Java client under MS windows then make sure the J-Integra® 'bin' directory
      // is in your PATH, otherwise run DCOMCNFG on the machine running Powerpoint (Start|Run|DCOMCNFG),
      // grant a specific user default launch and access permissions, and uncomment the following
      // line, specifying the appropriate domain/user/password
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");

      // Specify host name as parameter if you wish to access Powerpoint remotely.
      Application app = new Application();
      app.setVisible(-1);   // Nice to see what is happening
      Presentations presentations = app.getPresentations();

      // Create, save, and close presentation 1
      Presentation presentation = presentations.add(-1); // -1 = visible
      Slide slide = presentation.getSlides().add(1, PpSlideLayout.ppLayoutTitle);
      slide.getShapes().getTitle().getTextFrame().getTextRange().setText("Presentation1");
      presentation.saveAs("c:\\temp\\presentation1.ppt", PpSaveAsFileType.ppSaveAsDefault, -1);
      presentation.close();

      // Create presentation 2
      presentation = presentations.add(-1); // -1 = visible
      Slides slides = presentation.getSlides();
      slide = slides.add(1, PpSlideLayout.ppLayoutTitle);
      slide.getShapes().getTitle().getTextFrame().getTextRange().setText("Presentation2");

      // Merge in presentation 1
      slides.insertFromFile("c:\\temp\\presentation1.ppt", 1, 1, 1);

      // Save and close presentation 2
      presentation.saveAs("c:\\temp\\presentation2.ppt", PpSaveAsFileType.ppSaveAsDefault, -1);
      presentation.close();

      app.quit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // Release all remote objects that haven't already been garbage collected.
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}

Example 2

This example shows how to open an existing PowerPoint file and then run slide show.

import com.linar.jintegra.AuthInfo;
import powerpoint.*;

public class SlideShow {

  public static void main(java.lang.String[] args) {
    try {
      Application app = new Application();
      app.setVisible(-1); // Nice to see what is happening
      Presentations presentations = app.getPresentations();
      presentations.open("c:\\Temp\\test.ppt", 0, 0, 0);
      Presentation presentation = presentations.item(new Integer(1));
      SlideShowSettings slideShowSettings = presentation.getSlideShowSettings();
      Slide slide = presentation.getSlides().add(1, PpSlideLayout.ppLayoutTitle);
      slideShowSettings.run();

      // Slide show closes when the PowerPoint Window loses focus,
      // so wait to show the slides
      Thread.sleep(10000);

      app.quit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // Release all remote objects that haven't already been garbage collected.
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}