Menu

pptsample

Hervé Girod

The example below presents a simple use of the jfxConverter class to convert as a PowerPoint file. Most of the code is there to settup the JavaFX framework, create the Node tree, and ask the user for a PPT File. The only "interesting" part is part of the createSlides() method with the following code:

Bounds dim = node.getBoundsInLocal();
PPTGraphics2D g2d = new PPTGraphics2D(slide, (float) dim.getWidth(), (float) dim.getHeight(), Color.WHITE, Color.BLACK);
JFXConverter converter = new JFXConverter();
 converter.convert(g2d, node);

Complete source code below:

package org.tools.jfxconverter.samples.ppt;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javax.swing.JFileChooser;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.jfxconverter.JFXConverter;
import org.mdiutil.io.FileUtilities;
import org.mdiutil.swing.ExtensionFileFilter;
import org.tools.jfxconverter.tools.ppt.PPTGraphics2D;

/**
 * An example class for PPT conversion.
 */
public class FirstPPTSample extends Application {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      launch(args);
   }

   @Override
   public void start(Stage primaryStage) {
      StackPane root = new StackPane();
      Node node = getNode();
      root.getChildren().add(node);

      Scene scene = new Scene(root, 300, 250);

      primaryStage.setTitle("FirstPPTSample");
      primaryStage.setScene(scene);
      primaryStage.show();

      createSlides(node);
   }

   /**
    * Creates the Node hierarchy to convert.
    *
    * @return the root of the Node hierarchy
    */
   protected Node getNode() {
      Pane pane = new Pane();
      pane.setPrefWidth(400);
      pane.setPrefHeight(400);
      Button button = new Button("PUSH");
      button.setPrefWidth(100);
      button.setPrefHeight(150);
      button.setLayoutX(50);
      button.setLayoutY(50);
      Line line = new Line(0, 0, 200, 200);
      line.setStroke(javafx.scene.paint.Color.RED);
      pane.getChildren().add(line);
      pane.getChildren().add(button);
      return pane;
   }

   /**
    * Creates the Slide corresponding to the Node.
    *
    * @param node the Node
    */
   private void createSlides(Node node) {
      try {
         File file = getFile();
         if (file != null) {
            Bounds dim = node.getBoundsInLocal();
            HSLFSlideShow pptSlides = new HSLFSlideShow();
            HSLFSlide slide = pptSlides.createSlide();

            PPTGraphics2D g2d = new PPTGraphics2D(slide, (float) dim.getWidth(), (float) dim.getHeight(), Color.WHITE, Color.BLACK);
            JFXConverter converter = new JFXConverter();
            converter.convert(g2d, node);

            FileOutputStream stream = new FileOutputStream(file);
            pptSlides.write(stream);
            stream.flush();
            stream.close();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   /**
    * Return a user-selected File.
    *
    * @return the File
    */
   protected File getFile() {
      File dir = new File(System.getProperty("user.dir"));
      JFileChooser chooser = new JFileChooser();
      chooser.setCurrentDirectory(dir);
      chooser.setDialogTitle("Select PPT File to create");
      String[] ext = { "ppt" };
      ExtensionFileFilter pptfilter = new ExtensionFileFilter(ext, "PPT Files");
      chooser.addChoosableFileFilter(pptfilter);
      chooser.setDialogType(JFileChooser.SAVE_DIALOG);
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
      File file = null;
      if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
         file = chooser.getSelectedFile();
         file = FileUtilities.getCompatibleFile(file, "ppt");
      }
      return file;
   }
}

Related

Wiki: Home