use of org.apache.poi.hslf.usermodel.HSLFSlideMaster in project poi by apache.
the class TestSlideMaster method testSlideMaster.
/**
* The reference ppt has two masters.
* Check we can read their attributes.
*/
@Test
public void testSlideMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
Environment env = ppt.getDocumentRecord().getEnvironment();
List<HSLFSlideMaster> master = ppt.getSlideMasters();
assertEquals(2, master.size());
//character attributes
assertEquals(40, master.get(0).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.size", true).getValue());
assertEquals(48, master.get(1).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.size", true).getValue());
int font1 = master.get(0).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.index", true).getValue();
int font2 = master.get(1).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.index", true).getValue();
assertEquals("Arial", env.getFontCollection().getFontWithId(font1));
assertEquals("Georgia", env.getFontCollection().getFontWithId(font2));
CharFlagsTextProp prop1 = (CharFlagsTextProp) master.get(0).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "char_flags", true);
assertEquals(false, prop1.getSubValue(CharFlagsTextProp.BOLD_IDX));
assertEquals(false, prop1.getSubValue(CharFlagsTextProp.ITALIC_IDX));
assertEquals(true, prop1.getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
CharFlagsTextProp prop2 = (CharFlagsTextProp) master.get(1).getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "char_flags", true);
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.BOLD_IDX));
assertEquals(true, prop2.getSubValue(CharFlagsTextProp.ITALIC_IDX));
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
//now paragraph attributes
assertEquals(0x266B, master.get(0).getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false).getValue());
assertEquals(0x2022, master.get(1).getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false).getValue());
int b1 = master.get(0).getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.font", false).getValue();
int b2 = master.get(1).getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.font", false).getValue();
assertEquals("Arial", env.getFontCollection().getFontWithId(b1));
assertEquals("Georgia", env.getFontCollection().getFontWithId(b2));
ppt.close();
}
use of org.apache.poi.hslf.usermodel.HSLFSlideMaster 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();
}
use of org.apache.poi.hslf.usermodel.HSLFSlideMaster in project poi by apache.
the class TestSlideMaster method testChangeSlideMaster.
/**
* Check we can dynamically assign a slide master to a slide.
*/
@Test
public void testChangeSlideMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
List<HSLFSlideMaster> master = ppt.getSlideMasters();
List<HSLFSlide> slide = ppt.getSlides();
int sheetNo;
//each slide uses its own master
assertEquals(slide.get(0).getMasterSheet()._getSheetNumber(), master.get(0)._getSheetNumber());
assertEquals(slide.get(1).getMasterSheet()._getSheetNumber(), master.get(1)._getSheetNumber());
//all slides use the first master slide
sheetNo = master.get(0)._getSheetNumber();
for (HSLFSlide s : slide) {
s.setMasterSheet(master.get(0));
}
ByteArrayOutputStream out;
out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
master = ppt.getSlideMasters();
slide = ppt.getSlides();
for (HSLFSlide s : slide) {
assertEquals(sheetNo, s.getMasterSheet()._getSheetNumber());
}
ppt.close();
}
Aggregations