use of org.apache.poi.hslf.usermodel.HSLFShape in project poi by apache.
the class TestShapes method graphics.
@Test
public void graphics() throws IOException {
HSLFSlide slide = ppt.createSlide();
HSLFLine line = new HSLFLine();
java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100, 200, 50, 60);
line.setAnchor(lineAnchor);
line.setLineWidth(3);
line.setLineDash(LineDash.DASH);
line.setLineColor(Color.red);
slide.addShape(line);
HSLFAutoShape ellipse = new HSLFAutoShape(ShapeType.ELLIPSE);
Rectangle2D ellipseAnchor = new Rectangle2D.Double(320, 154, 55, 111);
ellipse.setAnchor(ellipseAnchor);
ellipse.setLineWidth(2);
ellipse.setLineDash(LineDash.SOLID);
ellipse.setLineColor(Color.green);
ellipse.setFillColor(Color.lightGray);
slide.addShape(ellipse);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
//read ppt from byte array
HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
assertEquals(1, ppt2.getSlides().size());
slide = ppt2.getSlides().get(0);
List<HSLFShape> shape = slide.getShapes();
assertEquals(2, shape.size());
//group shape
assertTrue(shape.get(0) instanceof HSLFLine);
//group shape
assertEquals(lineAnchor, shape.get(0).getAnchor());
//group shape
assertTrue(shape.get(1) instanceof HSLFAutoShape);
//group shape
assertEquals(ellipseAnchor, shape.get(1).getAnchor());
ppt2.close();
}
use of org.apache.poi.hslf.usermodel.HSLFShape in project poi by apache.
the class TestBackground method defaults.
/**
* Default background for slide, shape and slide master.
*/
@Test
public void defaults() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
assertEquals(HSLFFill.FILL_SOLID, ppt.getSlideMasters().get(0).getBackground().getFill().getFillType());
HSLFSlide slide = ppt.createSlide();
assertTrue(slide.getFollowMasterBackground());
assertEquals(HSLFFill.FILL_SOLID, slide.getBackground().getFill().getFillType());
HSLFShape shape = new HSLFAutoShape(ShapeType.RECT);
assertEquals(HSLFFill.FILL_SOLID, shape.getFill().getFillType());
ppt.close();
}
use of org.apache.poi.hslf.usermodel.HSLFShape in project poi by apache.
the class TestOleEmbedding method testOLEShape.
@Test
public void testOLEShape() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("ole2-embedding-2003.ppt"));
HSLFSlide slide = ppt.getSlides().get(0);
int cnt = 0;
for (HSLFShape sh : slide.getShapes()) {
if (sh instanceof OLEShape) {
cnt++;
OLEShape ole = (OLEShape) sh;
HSLFObjectData data = ole.getObjectData();
if ("Worksheet".equals(ole.getInstanceName())) {
//Voila! we created a workbook from the embedded OLE data
HSSFWorkbook wb = new HSSFWorkbook(data.getData());
HSSFSheet sheet = wb.getSheetAt(0);
//verify we can access the xls data
assertEquals(1, sheet.getRow(0).getCell(0).getNumericCellValue(), 0);
assertEquals(1, sheet.getRow(1).getCell(0).getNumericCellValue(), 0);
assertEquals(2, sheet.getRow(2).getCell(0).getNumericCellValue(), 0);
assertEquals(3, sheet.getRow(3).getCell(0).getNumericCellValue(), 0);
assertEquals(8, sheet.getRow(5).getCell(0).getNumericCellValue(), 0);
wb.close();
} else if ("Document".equals(ole.getInstanceName())) {
//creating a HWPF document
HWPFDocument doc = new HWPFDocument(data.getData());
String txt = doc.getRange().getParagraph(0).text();
assertEquals("OLE embedding is thoroughly unremarkable.\r", txt);
doc.close();
}
}
}
assertEquals("Expected 2 OLE shapes", 2, cnt);
ppt.close();
}
use of org.apache.poi.hslf.usermodel.HSLFShape in project poi by apache.
the class TestTable method testShapeFactory.
/**
* Test that ShapeFactory works properly and returns <code>Table</code>
*/
@Test
public void testShapeFactory() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
HSLFTable tbl = slide.createTable(2, 5);
HSLFTableCell cell = tbl.getCell(0, 0);
//table cells have type=TextHeaderAtom.OTHER_TYPE, see bug #46033
assertEquals(TextHeaderAtom.OTHER_TYPE, cell.getTextParagraphs().get(0).getRunType());
HSLFShape tblSh = slide.getShapes().get(0);
assertTrue(tblSh instanceof HSLFTable);
HSLFTable tbl2 = (HSLFTable) tblSh;
assertEquals(tbl.getNumberOfColumns(), tbl2.getNumberOfColumns());
assertEquals(tbl.getNumberOfRows(), tbl2.getNumberOfRows());
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
ppt.close();
ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
slide = ppt.getSlides().get(0);
assertTrue(slide.getShapes().get(0) instanceof HSLFTable);
HSLFTable tbl3 = (HSLFTable) slide.getShapes().get(0);
assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
ppt.close();
}
use of org.apache.poi.hslf.usermodel.HSLFShape in project poi by apache.
the class PowerPointExtractor method getText.
public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText, boolean getMasterText) {
StringBuffer ret = new StringBuffer();
if (getSlideText) {
if (getMasterText) {
for (HSLFSlideMaster master : _show.getSlideMasters()) {
for (HSLFShape sh : master.getShapes()) {
if (sh instanceof HSLFTextShape) {
HSLFTextShape hsh = (HSLFTextShape) sh;
final String text = hsh.getText();
if (text == null || "".equals(text) || "*".equals(text)) {
continue;
}
if (HSLFMasterSheet.isPlaceholder(sh)) {
// check for metro shape of complex placeholder
boolean isMetro = new HSLFMetroShape<HSLFShape>(sh).hasMetroBlob();
if (!isMetro) {
// don't bother about boiler plate text on master sheets
LOG.log(POILogger.INFO, "Ignoring boiler plate (placeholder) text on slide master:", text);
continue;
}
}
ret.append(text);
if (!text.endsWith("\n")) {
ret.append("\n");
}
}
}
}
}
for (HSLFSlide slide : _slides) {
String headerText = "";
String footerText = "";
HeadersFooters hf = slide.getHeadersFooters();
if (hf != null) {
if (hf.isHeaderVisible()) {
headerText = safeLine(hf.getHeaderText());
}
if (hf.isFooterVisible()) {
footerText = safeLine(hf.getFooterText());
}
}
// Slide header, if set
ret.append(headerText);
// Slide text
textRunsToText(ret, slide.getTextParagraphs());
// Table text
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof HSLFTable) {
extractTableText(ret, (HSLFTable) shape);
}
}
// Slide footer, if set
ret.append(footerText);
// Comments, if requested and present
if (getCommentText) {
for (Comment comment : slide.getComments()) {
ret.append(comment.getAuthor() + " - " + comment.getText() + "\n");
}
}
}
if (getNoteText) {
ret.append('\n');
}
}
if (getNoteText) {
// Not currently using _notes, as that can have the notes of
// master sheets in. Grab Slide list, then work from there,
// but ensure no duplicates
Set<Integer> seenNotes = new HashSet<Integer>();
String headerText = "";
String footerText = "";
HeadersFooters hf = _show.getNotesHeadersFooters();
if (hf != null) {
if (hf.isHeaderVisible()) {
headerText = safeLine(hf.getHeaderText());
}
if (hf.isFooterVisible()) {
footerText = safeLine(hf.getFooterText());
}
}
for (HSLFSlide slide : _slides) {
HSLFNotes notes = slide.getNotes();
if (notes == null) {
continue;
}
Integer id = Integer.valueOf(notes._getSheetNumber());
if (seenNotes.contains(id)) {
continue;
}
seenNotes.add(id);
// Repeat the Notes header, if set
ret.append(headerText);
// Notes text
textRunsToText(ret, notes.getTextParagraphs());
// Repeat the notes footer, if set
ret.append(footerText);
}
}
return ret.toString();
}
Aggregations