2009年6月16日

Apache POI HSLF Create PowerPoint Slide

POI-HSLF - Java API To Access Microsoft Powerpoint Format Files
Busy Developers' Guide to HSLF drawing layer

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.ShapeTypes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.StyleTextPropAtom;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

public class Main {

private static void buildRichTextRuns(TextRun textRun) {
StyleTextPropAtom styleAtom = null;
Record[] records = textRun.getRecords();
for (Record record : records) {
if (record instanceof StyleTextPropAtom) {
styleAtom = (StyleTextPropAtom) record;
}
}

styleAtom.getParagraphStyles().clear();
styleAtom.getCharacterStyles().clear();
String text = textRun.getText();
String[] lines = text.split("[\\n]");
for (String line : lines) {
int length = line.length() + 1;
styleAtom.addParagraphTextPropCollection(length);
styleAtom.addCharacterTextPropCollection(length);
}

textRun.buildRichTextRuns(styleAtom.getParagraphStyles(),
styleAtom.getCharacterStyles(), text);
}

public static void main(String[] args) throws IOException {
SlideShow ppt = new SlideShow();
{
Slide slide = ppt.createSlide();
{
AutoShape title = new AutoShape(ShapeTypes.Rectangle);
title.setAnchor(new java.awt.Rectangle(54, 167, 612, 117));
title.setFillColor(null);
title.setLineColor(null);
title.setText("Title");
TextRun textRun = title.getTextRun();
textRun.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
textRun.getRichTextRuns()[0].setFontSize(44);
slide.addShape(title);
}
{
AutoShape subTitle = new AutoShape(ShapeTypes.Rectangle);
subTitle.setAnchor(new java.awt.Rectangle(108, 306, 504, 138));
subTitle.setFillColor(null);
subTitle.setLineColor(null);
subTitle.setText("SubTitle\rHello World");
TextRun textRun = subTitle.getTextRun();
textRun.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
textRun.getRichTextRuns()[0].setFontSize(32);
subTitle.setVerticalAlignment(TextBox.AnchorTop);
slide.addShape(subTitle);
}
}
{
Slide slide = ppt.createSlide();
{
AutoShape title = new AutoShape(ShapeTypes.Rectangle);

title.setAnchor(new java.awt.Rectangle(36, 21, 648, 91));
title.setFillColor(null);
title.setLineColor(null);
title.setText("Title");
TextRun textRun = title.getTextRun();
textRun.setRunType(TextHeaderAtom.TITLE_TYPE);
textRun.getRichTextRuns()[0].setFontSize(44);
slide.addShape(title);
}
{
AutoShape textShape = new AutoShape(ShapeTypes.Rectangle);
textShape.setText("Item 1\rSub Item 1\rItem 1\rSub Item 1");
TextRun textRun = textShape.getTextRun();
textRun.setRunType(TextHeaderAtom.BODY_TYPE);
buildRichTextRuns(textRun);

RichTextRun[] richTextRuns = textRun.getRichTextRuns();
for (int i = 0; i < richTextRuns.length; i++) {
RichTextRun richTextRun = richTextRuns[i];
richTextRun.setIndentLevel(i);
richTextRun.setAlignment(TextBox.AlignLeft);
}
textShape.setAnchor(new java.awt.Rectangle(36, 126, 648, 357));
textShape.setFillColor(null);
textShape.setLineColor(null);
textShape.setVerticalAlignment(TextBox.AnchorTop);
slide.addShape(textShape);
}
}
FileOutputStream out = new FileOutputStream(args[0]);
try {
ppt.write(out);
} finally {
out.close();
}
}

}

沒有留言:

網誌存檔