aspose file tools*
The moose likes Other Open Source Projects and the fly likes How to display powerpoint slide in java applet using org.apache.poi? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to display powerpoint slide in java applet using org.apache.poi?" Watch "How to display powerpoint slide in java applet using org.apache.poi?" New topic
Author

How to display powerpoint slide in java applet using org.apache.poi?

veron tan
Greenhorn

Joined: Jun 29, 2009
Posts: 2
Hi All,

my java application need to display powerpoint slide in java applet using org.apache.poi . i am new to org.apache.poi, i am not sure how to display ppt in java applet, currently i can manage to create new powerpoint and convert ppt slide into image.

example: the powerpoint must upload into server after uploading into server, the user can see the powerpoint slide in the java applet itself, user also can choose which powerpoint they want to view and user can download the powerpoint from the server into their computer.

below is the code for creating ppt and convert slide into image:

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.image.BufferedImage;


import org.apache.poi.hslf.model.TextBox;


class colorfulSlideTitle
{

private BufferedImage img;

public static void main(String a[])
{
try
{

//create ppt slide

SlideShow slideShow = new SlideShow();
Slide slide1 = slideShow.createSlide();
TextBox title = slide1.addTitle();
title.setText("I am HADI, i like to do programming ");
RichTextRun richtextrun = title.getTextRun().getRichTextRuns()[0];
richtextrun.setFontSize(100);
richtextrun.setFontName("Arial");
richtextrun.setBold(true);
richtextrun.setItalic(true);
//richtextrun.setUnderlined(true);
richtextrun.setFontColor(Color.blue);
richtextrun.setAlignment(TextBox.AlignLeft);

FileOutputStream out1 = new FileOutputStream("testing.ppt");
slideShow.write(out1);
out1.close();



//convert ppt slide into image

FileInputStream is = new FileInputStream("testing.ppt");
SlideShow ppt = new SlideShow(is);
is.close();

Dimension pgsize = ppt.getPageSize();

Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {

BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
//graphics.setBackground(Color.red);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

//render
slide[i].draw(graphics);

//save the output
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}


}catch(Exception e){}
}
}


anyone have any idea how to display the powerpoint slide in java applet?

the displaying ppt in java applet almost same as this website : http://www.authorstream.com/


With thanks and regards
verontan
veron tan
Greenhorn

Joined: Jun 29, 2009
Posts: 2
does anyone know how to display powerpoint slide in java applet?


Thanks


regards,
verontan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
I'm not aware of a Java library that can display PPT files, but if you can convert the slides to images -and it sounds as if you have managed to do that-, then you display the images in the applet, no?

}catch(Exception e){}

Don't completely ignore exceptions - how will you know if there is a problem? At least print a message to a console or log file.


Ping & DNS - my free Android networking tools app
Rio Purwanggono
Greenhorn

Joined: Feb 09, 2010
Posts: 1
I just modified your code. Try this simple PPTViewer with GUI :


import javax.swing.Icon;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hslf.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.usermodel.*;

// Import Java classes
import java.io.*;
import java.util.*;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

/**
*
* @author PC182
*/
public class PowerPointSampleReader1 extends javax.swing.JFrame {

/** Creates new form PowerPointSampleReader1 */
int current = 0;
int all=0;
String filename;;
public PowerPointSampleReader1() {
initComponents();
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
filename = "C:/Users/PC182/Documents/NetBeansProjects/tes/OSUM.ppt";
btnBack.setEnabled(false);
Display(1, filename);
}
public void Display(int currentPage, String source)
{
try {
// Create a slideshow object; this creates an underlying POIFSFileSystem object for us
SlideShow ppt = new SlideShow(new HSLFSlideShow(source));
current=currentPage;
// Get all of the slides from the PPT file
Slide[] slides = ppt.getSlides();
Dimension pgsize = ppt.getPageSize();
all = slides.length;
String temp="";
lblPage.setText(currentPage+" / "+all);

BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
//graphics.setBackground(Color.red);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

//render
slides[currentPage-1].draw(graphics);
//save the output
/*FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
//ImageIcon icon = new ImageIcon("slide-" + (i + 1) + ".png");*/
ImageIcon icon = new ImageIcon(img);
lblPresentasi.setIcon(icon);
// Obtain metrics about the slide: its number and name
int number = slides[currentPage-1].getSlideNumber();
String title = slides[currentPage-1].getTitle();

// Obtain the embedded text in the slide
TextRun[] textRuns = slides[currentPage-1].getTextRuns();
System.out.println("Slide " + number + ": " + title);
System.out.println("\tText Runs");
txtArea.setText("Slide : " + number + " Title : " + title + "\n");
for (int j = 0; j < textRuns.length; j++) {
// Display each of the text runs present on the slide
System.out.println("\t\t" + j + ": " + textRuns[j].getText());
temp=txtArea.getText();
txtArea.setText(temp+"\t\t" + textRuns[j].getText() + "\n");
}

// Obtain the notes for this slide
System.out.println("\tNotes: ");
Notes notes = slides[currentPage-1].getNotesSheet();
if (notes != null) {
// Notes are comprised of an array of text runs
TextRun[] notesTextRuns = notes.getTextRuns();
for (int j = 0; j < notesTextRuns.length; j++) {
System.out.println("\t\t" + notesTextRuns[j].getText());
}
}



} catch (Exception e) {
e.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
txtArea = new javax.swing.JTextArea();
lblPresentasi = new javax.swing.JLabel();
lblPage = new javax.swing.JLabel();
btnNext = new javax.swing.JButton();
btnBack = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Rio Simple PPTViewer");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setLocationByPlatform(true);

txtArea.setColumns(20);
txtArea.setRows(5);
jScrollPane1.setViewportView(txtArea);

lblPage.setText("n / all");

btnNext.setText("Next >>");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});

btnBack.setText("<< Back");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(lblPresentasi, javax.swing.GroupLayout.PREFERRED_SIZE, 720, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(403, 403, 403)
.addComponent(lblPage)
.addGap(266, 266, 266)
.addComponent(btnBack)
.addGap(40, 40, 40)
.addComponent(btnNext)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblPresentasi, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnNext)
.addComponent(btnBack))
.addComponent(lblPage))
.addContainerGap(30, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
current++;
btnBack.setEnabled(true);
if(current == all)
{
btnNext.setEnabled(false);
}
Display(current,filename);
}

private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
current--;
btnNext.setEnabled(true);
if(current == 1)
{
btnBack.setEnabled(false);
}
Display(current,filename);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new PowerPointSampleReader1().setVisible(true);

}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnBack;
private javax.swing.JButton btnNext;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblPage;
private javax.swing.JLabel lblPresentasi;
private javax.swing.JTextArea txtArea;
// End of variables declaration
}
 
 
subject: How to display powerpoint slide in java applet using org.apache.poi?