HSLF table and example fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/common_sl@1677703 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-05-04 22:31:45 +00:00
parent 02e70fdbfe
commit 2aea517704
6 changed files with 110 additions and 138 deletions

View File

@ -17,14 +17,15 @@
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.sl.usermodel.ShapeType;
import java.io.IOException;
import java.io.FileOutputStream;
import java.awt.*;
import org.apache.poi.sl.usermodel.VerticalAlignment;
/**
* Presentation for Fast Feather Track on ApacheconEU 2008
@ -60,26 +61,23 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
tr1.setText("POI-HSLF");
box1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
box1.setText("POI-HSLF");
box1.setAnchor(new Rectangle(54, 78, 612, 115));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
tr2.setText("Java API To Access Microsoft PowerPoint Format Files");
box2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
box2.setText("Java API To Access Microsoft PowerPoint Format Files");
box2.setAnchor(new Rectangle(108, 204, 504, 138));
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
HSLFTextParagraph tr3 = box3.getTextParagraphs();
tr3.getTextRuns()[0].setFontSize(32);
box3.setHorizontalAlignment(HSLFTextBox.AlignCenter);
tr3.setText(
box3.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(32);
box3.setText(
"Yegor Kozlov\r" +
"yegor - apache - org");
box3.setHorizontalCentered(true);
box3.setAnchor(new Rectangle(206, 348, 310, 84));
slide.addShape(box3);
}
@ -88,16 +86,14 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("What is HSLF?");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("What is HSLF?");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText("HorribleSLideshowFormat is the POI Project's pure Java implementation " +
box2.setRunType(TextHeaderAtom.BODY_TYPE);
box2.setText("HorribleSLideshowFormat is the POI Project's pure Java implementation " +
"of the Powerpoint binary file format. \r" +
"POI sub-project since 2005\r" +
"Started by Nick Birch, Yegor Kozlov joined soon after");
@ -111,41 +107,37 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in a Nutshell");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("HSLF in a Nutshell");
box1.setAnchor(new Rectangle(36, 15, 648, 65));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(28);
box2.setRunType(TextHeaderAtom.BODY_TYPE);
box2.setText(
"HSLF provides a way to read, create and modify MS PowerPoint presentations\r" +
"Pure Java API - you don't need PowerPoint to read and write *.ppt files\r" +
"Comprehensive support of PowerPoint objects");
tr2.getTextRuns()[0].setFontSize(28);
box2.setAnchor(new Rectangle(36, 80, 648, 200));
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
HSLFTextParagraph tr3 = box3.getTextParagraphs();
tr3.setRunType(TextHeaderAtom.BODY_TYPE);
tr3.setText(
box2.getTextParagraphs().get(0).setIndentLevel(1);
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24);
box3.setRunType(TextHeaderAtom.BODY_TYPE);
box3.setText(
"Rich text\r" +
"Tables\r" +
"Shapes\r" +
"Pictures\r" +
"Master slides");
tr3.getTextRuns()[0].setFontSize(24);
tr3.getTextRuns()[0].setIndentLevel(1);
box3.setAnchor(new Rectangle(36, 265, 648, 150));
slide.addShape(box3);
HSLFTextBox box4 = new HSLFTextBox();
HSLFTextParagraph tr4 = box4.getTextParagraphs();
tr4.setRunType(TextHeaderAtom.BODY_TYPE);
tr4.setText("Access to low level data structures");
box4.setRunType(TextHeaderAtom.BODY_TYPE);
box4.setText("Access to low level data structures");
box4.setAnchor(new Rectangle(36, 430, 648, 50));
slide.addShape(box4);
}
@ -162,8 +154,8 @@ public final class ApacheconEU08 {
for (int j = 0; j < txt1[i].length; j++) {
TableCell cell = table1.getCell(i, j);
cell.setText(txt1[i][j]);
cell.getTextParagraphs().getTextRuns()[0].setFontSize(10);
HSLFTextRun rt = cell.getTextParagraphs().getTextRuns()[0];
HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
rt.setFontSize(10);
rt.setFontName("Arial");
rt.setBold(true);
if(i == 0){
@ -174,7 +166,7 @@ public final class ApacheconEU08 {
rt.setFontSize(28);
cell.getFill().setForegroundColor(new Color(235, 239, 241));
}
cell.setVerticalAlignment(HSLFTextBox.AnchorMiddle);
cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
}
}
@ -196,12 +188,10 @@ public final class ApacheconEU08 {
table1.moveTo(100, 100);
HSLFTextBox box1 = new HSLFTextBox();
box1.setHorizontalAlignment(HSLFTextBox.AlignCenter);
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setText("The source code is available at\r" +
box1.setHorizontalCentered(true);
box1.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24);
box1.setText("The source code is available at\r" +
"http://people.apache.org/~yegor/apachecon_eu08/");
HSLFTextRun rt = tr1.getTextRuns()[0];
rt.setFontSize(24);
box1.setAnchor(new Rectangle(80, 356, 553, 65));
slide.addShape(box1);
@ -211,16 +201,14 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 1\rData Extraction");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("HSLF in Action - 1\rData Extraction");
box1.setAnchor(new Rectangle(36, 21, 648, 100));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
box2.setRunType(TextHeaderAtom.BODY_TYPE);
box2.setText(
"Text from slides and notes\r" +
"Images\r" +
"Shapes and their properties (type, position in the slide, color, font, etc.)");
@ -234,25 +222,22 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 2");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("HSLF in Action - 2");
box1.setAnchor(new Rectangle(36, 20, 648, 90));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.getTextRuns()[0].setFontSize(18);
tr2.setText("Creating a simple presentation from scratch");
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(18);
box2.setText("Creating a simple presentation from scratch");
box2.setAnchor(new Rectangle(170, 100, 364, 30));
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
HSLFTextParagraph tr3 = box3.getTextParagraphs();
HSLFTextRun rt3 = tr3.getTextRuns()[0];
HSLFTextRun rt3 = box3.getTextParagraphs().get(0).getTextRuns().get(0);
rt3.setFontName("Courier New");
rt3.setFontSize(8);
tr3.setText(
box3.setText(
" SlideShow ppt = new SlideShow();\r" +
" Slide slide = ppt.createSlide();\r" +
"\r" +
@ -294,9 +279,9 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box2 = new HSLFTextBox();
box2.setHorizontalAlignment(HSLFTextBox.AlignCenter);
box2.setVerticalAlignment(HSLFTextBox.AnchorMiddle);
box2.getTextParagraphs().setText("Java Code");
box2.setHorizontalCentered(true);
box2.setVerticalAlignment(VerticalAlignment.MIDDLE);
box2.setText("Java Code");
box2.getFill().setForegroundColor(new Color(187, 224, 227));
box2.setLineColor(Color.black);
box2.setLineWidth(0.75);
@ -304,9 +289,9 @@ public final class ApacheconEU08 {
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
box3.setHorizontalAlignment(HSLFTextBox.AlignCenter);
box3.setVerticalAlignment(HSLFTextBox.AnchorMiddle);
box3.getTextParagraphs().setText("*.ppt file");
box3.setHorizontalCentered(true);
box3.setVerticalAlignment(VerticalAlignment.MIDDLE);
box3.setText("*.ppt file");
box3.setLineWidth(0.75);
box3.setLineColor(Color.black);
box3.getFill().setForegroundColor(new Color(187, 224, 227));
@ -325,16 +310,14 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("Wait, there is more!");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("Wait, there is more!");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
box2.setRunType(TextHeaderAtom.BODY_TYPE);
box2.setText(
"Rich text\r" +
"Tables\r" +
"Pictures (JPEG, PNG, BMP, WMF, PICT)\r" +
@ -347,25 +330,22 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 3");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("HSLF in Action - 3");
box1.setAnchor(new Rectangle(36, 20, 648, 50));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.getTextRuns()[0].setFontSize(18);
tr2.setText("PPGraphics2D: PowerPoint Graphics2D driver");
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(18);
box2.setText("PPGraphics2D: PowerPoint Graphics2D driver");
box2.setAnchor(new Rectangle(178, 70, 387, 30));
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
HSLFTextParagraph tr3 = box3.getTextParagraphs();
HSLFTextRun rt3 = tr3.getTextRuns()[0];
HSLFTextRun rt3 = box3.getTextParagraphs().get(0).getTextRuns().get(0);
rt3.setFontName("Courier New");
rt3.setFontSize(8);
tr3.setText(
box3.setText(
" //bar chart data. The first value is the bar color, the second is the width\r" +
" Object[] def = new Object[]{\r" +
" Color.yellow, new Integer(100),\r" +
@ -449,45 +429,40 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF Development Plans");
box1.setRunType(TextHeaderAtom.TITLE_TYPE);
box1.setText("HSLF Development Plans");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.getTextRuns()[0].setFontSize(32);
tr2.setText(
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(32);
box2.setRunType(TextHeaderAtom.BODY_TYPE);
box2.setText(
"Support for more PowerPoint functionality\r" +
"Rendering slides into java.awt.Graphics2D");
box2.setAnchor(new Rectangle(36, 126, 648, 100));
slide.addShape(box2);
HSLFTextBox box3 = new HSLFTextBox();
HSLFTextParagraph tr3 = box3.getTextParagraphs();
tr3.setRunType(TextHeaderAtom.BODY_TYPE);
tr3.getTextRuns()[0].setIndentLevel(1);
tr3.setText(
box3.getTextParagraphs().get(0).setIndentLevel(1);
box3.setRunType(TextHeaderAtom.BODY_TYPE);
box3.setText(
"A way to export slides into images or other formats");
box3.setAnchor(new Rectangle(36, 220, 648, 70));
slide.addShape(box3);
HSLFTextBox box4 = new HSLFTextBox();
HSLFTextParagraph tr4 = box4.getTextParagraphs();
tr4.setRunType(TextHeaderAtom.BODY_TYPE);
tr4.getTextRuns()[0].setFontSize(32);
tr4.setText(
box4.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(32);
box4.setRunType(TextHeaderAtom.BODY_TYPE);
box4.setText(
"Integration with Apache FOP - Formatting Objects Processor");
box4.setAnchor(new Rectangle(36, 290, 648, 90));
slide.addShape(box4);
HSLFTextBox box5 = new HSLFTextBox();
HSLFTextParagraph tr5 = box5.getTextParagraphs();
tr5.setRunType(TextHeaderAtom.BODY_TYPE);
tr5.getTextRuns()[0].setIndentLevel(1);
tr5.setText(
box5.getTextParagraphs().get(0).setIndentLevel(1);
box5.setRunType(TextHeaderAtom.BODY_TYPE);
box5.setText(
"Transformation of XSL-FO into PPT\r" +
"PPT2PDF transcoder");
box5.setAnchor(new Rectangle(36, 380, 648, 100));
@ -498,16 +473,14 @@ public final class ApacheconEU08 {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox box1 = new HSLFTextBox();
HSLFTextParagraph tr1 = box1.getTextParagraphs();
tr1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
tr1.setText("Questions?");
box1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
box1.setText("Questions?");
box1.setAnchor(new Rectangle(54, 167, 612, 115));
slide.addShape(box1);
HSLFTextBox box2 = new HSLFTextBox();
HSLFTextParagraph tr2 = box2.getTextParagraphs();
tr2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
tr2.setText(
box2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
box2.setText(
"http://poi.apache.org/hslf/\r" +
"http://people.apache.org/~yegor");
box2.setAnchor(new Rectangle(108, 306, 504, 138));

View File

@ -36,17 +36,17 @@ public final class BulletsDemo {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox shape = new HSLFTextBox();
HSLFTextRun rt = shape.getTextParagraphs().getTextRuns()[0];
HSLFTextParagraph rt = shape.getTextParagraphs().get(0);
rt.getTextRuns().get(0).setFontSize(42);
rt.setBullet(true);
rt.setIndent(0); //bullet offset
rt.setLeftMargin(50); //text offset (should be greater than bullet offset)
rt.setBulletChar('\u263A'); //bullet character
shape.setText(
"January\r" +
"February\r" +
"March\r" +
"April");
rt.setFontSize(42);
rt.setBullet(true);
rt.setBulletOffset(0); //bullet offset
rt.setTextOffset(50); //text offset (should be greater than bullet offset)
rt.setBulletChar('\u263A'); //bullet character
slide.addShape(shape);
shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); //position of the text box in the slide

View File

@ -57,13 +57,13 @@ public final class DataExtraction {
out.close();
}
int oleIdx=-1, picIdx=-1;
for (HSLFSlide slide : ppt.getSlides()) {
//extract embedded OLE documents
HSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
HSLFShape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof OLEShape) {
OLEShape ole = (OLEShape) shape[j];
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof OLEShape) {
oleIdx++;
OLEShape ole = (OLEShape) shape;
HSLFObjectData data = ole.getObjectData();
String name = ole.getInstanceName();
if ("Worksheet".equals(name)) {
@ -81,11 +81,11 @@ public final class DataExtraction {
}
//save on disk
FileOutputStream out = new FileOutputStream(name + "-("+(j)+").doc");
FileOutputStream out = new FileOutputStream(name + "-("+(oleIdx)+").doc");
doc.write(out);
out.close();
} else {
FileOutputStream out = new FileOutputStream(ole.getProgID() + "-"+(j+1)+".dat");
FileOutputStream out = new FileOutputStream(ole.getProgID() + "-"+(oleIdx+1)+".dat");
InputStream dis = data.getData();
byte[] chunk = new byte[2048];
int count;
@ -97,15 +97,10 @@ public final class DataExtraction {
}
}
}
}
//Pictures
for (int i = 0; i < slide.length; i++) {
HSLFShape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof HSLFPictureShape) {
HSLFPictureShape p = (HSLFPictureShape) shape[j];
else if (shape instanceof HSLFPictureShape) {
picIdx++;
HSLFPictureShape p = (HSLFPictureShape) shape;
HSLFPictureData data = p.getPictureData();
String name = p.getPictureName();
int type = data.getType();
@ -132,15 +127,14 @@ public final class DataExtraction {
default:
continue;
}
FileOutputStream out = new FileOutputStream("pict-" + j + ext);
FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
out.write(data.getData());
out.close();
}
}
}
}
}
private static void usage(){
System.out.println("Usage: DataExtraction ppt");

View File

@ -78,7 +78,7 @@ public final class Table extends HSLFGroupShape {
EscherOptRecord opt = new EscherOptRecord();
opt.setRecordId((short)0xF122);
opt.addEscherProperty(new EscherSimpleProperty((short)0x39F, 1));
EscherArrayProperty p = new EscherArrayProperty((short)0x43A0, false, null);
EscherArrayProperty p = new EscherArrayProperty((short)(0x4000 | 0x3A0), false, null);
p.setSizeOfElements(0x0004);
p.setNumberOfElementsInArray(numrows);
p.setNumberOfElementsInMemory(numrows);
@ -122,7 +122,7 @@ public final class Table extends HSLFGroupShape {
EscherContainerRecord spCont = (EscherContainerRecord) getSpContainer().getChild(0);
List<EscherRecord> lst = spCont.getChildRecords();
EscherOptRecord opt = (EscherOptRecord)lst.get(lst.size()-2);
EscherArrayProperty p = (EscherArrayProperty)opt.getEscherProperty(1);
EscherArrayProperty p = opt.lookup(0x3A0);
for (int i = 0; i < cells.length; i++) {
TableCell cell = cells[i][0];
int rowHeight = cell.getAnchor().height*MASTER_DPI/POINT_DPI;

View File

@ -24,6 +24,7 @@ import java.util.Iterator;
import org.apache.poi.ddf.*;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.util.*;
@ -484,7 +485,11 @@ public abstract class HSLFShape implements Shape {
}
public EscherOptRecord getEscherOptRecord() {
return getEscherChild(EscherOptRecord.RECORD_ID);
EscherOptRecord opt = getEscherChild(EscherOptRecord.RECORD_ID);
if (opt == null) {
opt = getEscherChild(RecordTypes.EscherUserDefined);
}
return opt;
}
public boolean getFlipHorizontal(){

View File

@ -230,8 +230,8 @@ public abstract class HSLFTextShape extends HSLFSimpleShape implements TextShape
* fetch the master shape and search for the align property there.
*/
int type = getRunType();
if(getSheet() != null && getSheet().getMasterSheet() != null){
HSLFMasterSheet master = getSheet().getMasterSheet();
if(master != null){
HSLFTextShape masterShape = master.getPlaceholderByTextType(type);
if(masterShape != null) align = masterShape.getAlignment();
} else {