Disadvantage with Open source Apache POI-HSLF and POI-XLSF:
- POI still in early development and is a subject
to incompatible changes in future.
to incompatible changes in future.
- Current version of POI (POI-HSLF and POI-XSLF)
supports only implementation of Powerpoint
2007 format.
supports only implementation of Powerpoint
2007 format.
- HSLF and XSLF though has similar features, the
interface among them is not common; making maintenance difficult; if same
feature need to be provided in different format.
interface among them is not common; making maintenance difficult; if same
feature need to be provided in different format.
- POI provides API at very elementary level. For a task
as easy as copying a slide from one presentation to another presentation;
requires hundreds of lines of code and makes maintenance difficult.
as easy as copying a slide from one presentation to another presentation;
requires hundreds of lines of code and makes maintenance difficult.
- We didn’t find even Jpeg conversions of powerpoint an easy task with POI.
A better option is use to the scripting provided by Microsoft itself and to use it within Java for powerpoint presentation. We have chosen to use vb script and run it using cscript (a command line version of wscript). One of the advantages with cscript over other types of programming is that for cscript,
everything you need is built into the Windows operating system. It is easy and doesn’t need any installation. Though it makes programming platform dependent; but it make sense; after all; we are going to use MSOffice on Windows machines only.
everything you need is built into the Windows operating system. It is easy and doesn’t need any installation. Though it makes programming platform dependent; but it make sense; after all; we are going to use MSOffice on Windows machines only.
We would now take few examples of writing VB script for routine work required with powerpoint
presentations:
presentations:
a) Example of VB Script for copying a slide from one presentation to end of another presentation
Set pptDoc = CreateObject("PowerPoint.Application")
pptDoc.Visible = TRUE
Set objPresentation = pptDoc.Presentations.Open("D:\Workspace\vbscript\ppt\MyPresentationTo.pptx")
objPresentation.Slides.InsertFromFile _
"D:\Workspace\vbscript\ppt\MyPresentationFrom.pptx",objPresentation.Slides.Count , 1, 1
objPresentation.Save
objPresentation.close
pptDoc.Quit
Open method in above example opens presentation “MyPresentationTo.pptx” in which slide is
to be copied. InsertFromFile method copies one slide from “MyPresentationFrom.pptx” at the end of
“MyPresentationTo.pptx”.
to be copied. InsertFromFile method copies one slide from “MyPresentationFrom.pptx” at the end of
“MyPresentationTo.pptx”.
First argument of InsertFromFile specifies the name of presentation from which slides are to be copied. Second argument specifies the index after which you want to insert the copied slides in the opened presentation. Third argument specifies the index of first slide that you want to copy from presentation. Fourth argument specifies the index of last slide that you want to copy from presentation.
After copying slide; we saved the file and closed the presentation within script.
To try this; copy above example in a notepad; save it as “.vbs extention”
e.g. “CopySlide.vbs” and double click on it. Remember to create two ppt (MyPresentationTo.pptx, MyPresentationFrom.pptx) in the specified folder before executing script.
e.g. “CopySlide.vbs” and double click on it. Remember to create two ppt (MyPresentationTo.pptx, MyPresentationFrom.pptx) in the specified folder before executing script.
b) Following example of VB Script shows how to add shapes to a slide:
Set pptDoc = CreateObject("PowerPoint.Application")
pptDoc.Visible = TRUE
Set objPresentation = pptDoc.Presentations.Open("D:\Workspace\vbscript\ppt\MyPresentationTo.pptx")
Set myDocument = objPresentation.Slides(1)
Set line = myDocument.Shapes.AddLine(40, 40, 80, 80)
objPresentation.Save
objPresentation.close
pptDoc.Quit
AddLine method in above example adds a line to Slide 1 in above example.
c) Following example shows how to export a presentation in jpg format:
Set pptDoc = CreateObject("PowerPoint.Application")
pptDoc.Visible = TRUE
Set objPresentation = pptDoc.Presentations.Open
("D:\Workspace\vbscript\ppt\MyPresentationTo.pptx")
Set myDocument = objPresentation.Slides(1) myDocument.Export
"D:\Workspace\vbscript\ppt\MyPresentation.jpg","JPG"
objPresentation.Save
objPresentation.close
pptDoc.Quit
Export command in above program exports presentation to jpg format. First argument to the method is the path where to export the file and second argument is the format in which presentation is to be exported.
Once you have learnt and played with scripts; you can write a small program to execute these scripts
from Java.
from Java.
import java.io.IOException;
public class RunScript {
public static void main (String args[]) throws IOException {
public static void main (String args[]) throws IOException {
if (args == null || args.length == 0) {
System.out.println("Usage: java RunScript <scriptfile>" );
System.exit(1);
}
runScript(args[0]);
}
}
public static void runScript(String script) throws IOException {
String command = "cmd /c cscript " + script;
Runtime.getRuntime().exec(command);
}
}
}
In above example you may call runScript method from any of your program.
E.g. the script shown in example a) above was stored as “Copy Slide.vbs” and executed using following
command after compilation:
command after compilation:
java RunScript D:\Workspace\vbscript\scripts\CopySlide.vbs
It copied Slide 1 from MyPresentationFrom.pptx at the end of MyPresentationFrom.pptx
The real power lies in making the script dynamic thru Java programming and then using it at runtime. Though this article has been focused on PowerPoint presentations; but you may use this technique with MSWord and MSExcel as well.
Advantage:
- Provides code compatibility among various versions
of MSOffice
of MSOffice
- Easy to use commands
- Tasks like copying slides and exporting can be
done within few lines of code.
done within few lines of code.
Limitation:
- Not compatible over platform