Hi All,
This question is mostly directed at Yegor Kozlov and Nick Burch since they
wrote most of the classes involved.
I've rewritten the Groovy code I emailed out before in Java in case that
helps anyone. It's attached.
Everything works great. I was very pleased that the old emails floating
around the Internet saying that POI couldn't merge slideshows were
out-of-date. EXCEPT when you get to line 85 of my class. The RichTextRuns
report that their font.color property is white. Running this through a
debugger drops me all the way into the SlideMaster's text runs to find
that white color. Setting the color against the RichTextRun has no effect
on the final outcome. I tried looping through the Slide's MasterSheet's
TextRuns' RichTextRuns and setting the font.color there before starting
the merge but that throws a null pointer exception in
TextRun.ensureStyleAtomPresent() at this line:
runAtomsParent.addChildAfter(_styleAtom, addAfter);
Unlike things like font.name or the list of Pictures which are stored at
the SlideShow level and referenced other places, the font.color is
specifically set for the RichTextRun. Is it possible that when the
SlideShow is written out that this property isn't referenced? I'm really
at a loss to figure this out much further.
Sincerely,
Jeff Lavezzo
package com.saic.hub.utils;
/**
* Created by IntelliJ IDEA.
*
* User: jlavezzo
* Date: Mar 10, 2010
* Time: 2:10:24 PM
* To change this template use File | Settings | File Templates.
*/
import java.awt.Color;
//import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.PictureData;
public class MergeSlideshows {
SlideShow templateSlideShow;
SlideShow mergerSlideShow;
File outputFile;
public MergeSlideshows(SlideShow templateSlideShow, SlideShow mergerSlideShow) {
this.templateSlideShow = templateSlideShow;
this.mergerSlideShow = mergerSlideShow;
}
public MergeSlideshows(String templateSlideShow, String mergerSlideShow, String outputFile) {
File templateFile = new File(templateSlideShow);
File mergerFile = new File(mergerSlideShow);
this.outputFile = new File(outputFile);
try{
FileInputStream templateFIS = new FileInputStream(templateFile);
FileInputStream mergerFIS = new FileInputStream(mergerFile);
this.templateSlideShow = new SlideShow(templateFIS);
this.mergerSlideShow = new SlideShow(mergerFIS);
} catch (IOException ioe) {
//blah
}
}
public void doMerge(){
PictureData[] mergerPictureData = mergerSlideShow.getPictureData();
for (Slide mergerSlide : mergerSlideShow.getSlides()) {
int slideIndex = templateSlideShow.getSlides().length-1;
Slide lastTemplateSlide = templateSlideShow.getSlides()[slideIndex];
// println "adding to slide ${slideIndex} of ${ppt.getSlides().size()}"
for (Shape mergeShape : mergerSlide.getShapes()) {
if (mergeShape instanceof Picture) {
// cast to picture
Picture mergerPicture = (Picture) mergeShape;
// get picture format from picture
int pictureFormat = mergerPictureData[mergerPicture.getPictureIndex()-1].getType();
// get byte[] from picture
// add to ppt with addPicture(byte[] data, int format) also may try getData() instead
int picDataIndex = (mergerPicture.getPictureIndex()-1);
int newPictureIndex = 0;
byte[] picData = mergerPictureData[picDataIndex].getData();
try {
newPictureIndex = templateSlideShow.addPicture(picData, pictureFormat);
} catch (IOException ioe){
// who cares?
}
// use picture index to create a new Picture
Picture newPicture = new Picture(newPictureIndex);
// copy the anchor from the doi picture to the new picture
newPicture.setAnchor(mergerPicture.getAnchor());
//heightAdjustShape(newPicture) //ignore this till we get text color working
// add new picture to templateSlideFour
lastTemplateSlide.addShape(newPicture);
}
if (mergeShape instanceof TextBox) {
TextBox mergeTextBox = (TextBox) mergeShape;
mergeTextBox.setSheet(lastTemplateSlide);
mergeTextBox.getTextRun().supplySlideShow(templateSlideShow);
mergeTextBox.getTextRun().setSheet(lastTemplateSlide);
for (RichTextRun mergeRichTextRun : mergeTextBox.getTextRun().getRichTextRuns()) {
//println "current text run's color is ${it.getFontColor()}"
mergeRichTextRun.setFontColor(Color.BLACK);//todo: get color to set correctly
//println "after setting current text run's color is ${it.getFontColor()}"
}
//heightAdjustShape(doiTextBox)
lastTemplateSlide.addShape(mergeTextBox);
}
}
templateSlideShow.createSlide();
}
}
public void writeOutPpt() {
if (outputFile == null) {
outputFile = new File("defaultOutput.ppt");
//log.error("No output file set. Setting to ${slideShow.getAbsolutePath()}")
}
try {
FileOutputStream out = new FileOutputStream(outputFile);
templateSlideShow.write(out);
out.close();
} catch (IOException ioe){
//blah
}
}
public static void main(String[] args) {
MergeSlideshows merger = new MergeSlideshows(args[0], args[1], args[2]);
merger.doMerge();
merger.writeOutPpt();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]