use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.
the class TestShapes method testParagraphs.
@SuppressWarnings("unused")
@Test
public void testParagraphs() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide();
HSLFTextBox shape = new HSLFTextBox();
HSLFTextRun p1r1 = shape.setText("para 1 run 1. ");
HSLFTextRun p1r2 = shape.appendText("para 1 run 2.", false);
HSLFTextRun p2r1 = shape.appendText("para 2 run 1. ", true);
HSLFTextRun p2r2 = shape.appendText("para 2 run 2. ", false);
p1r1.setFontColor(Color.black);
p1r2.setFontColor(Color.red);
p2r1.setFontColor(Color.yellow);
p2r2.setStrikethrough(true);
// run 3 has same text properties as run 2 and will be merged when saving
HSLFTextRun p2r3 = shape.appendText("para 2 run 3.", false);
shape.setAnchor(new Rectangle2D.Double(100, 100, 100, 10));
slide.addShape(shape);
shape.resizeToFitText();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ss.write(bos);
ss = new HSLFSlideShow(new ByteArrayInputStream(bos.toByteArray()));
slide = ss.getSlides().get(0);
HSLFTextBox tb = (HSLFTextBox) slide.getShapes().get(0);
List<HSLFTextParagraph> para = tb.getTextParagraphs();
HSLFTextRun tr = para.get(0).getTextRuns().get(0);
assertEquals("para 1 run 1. ", tr.getRawText());
assertTrue(sameColor(Color.black, tr.getFontColor()));
tr = para.get(0).getTextRuns().get(1);
assertEquals("para 1 run 2.\r", tr.getRawText());
assertTrue(sameColor(Color.red, tr.getFontColor()));
tr = para.get(1).getTextRuns().get(0);
assertEquals("para 2 run 1. ", tr.getRawText());
assertTrue(sameColor(Color.yellow, tr.getFontColor()));
tr = para.get(1).getTextRuns().get(1);
assertEquals("para 2 run 2. para 2 run 3.", tr.getRawText());
assertTrue(sameColor(Color.black, tr.getFontColor()));
assertTrue(tr.isStrikethrough());
}
use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.
the class TestShapes method textBoxWriteBytes.
/**
* Verify that we can add TextBox shapes to a slide
* and set some of the style attributes
*/
@Test
public void textBoxWriteBytes() throws IOException {
ppt = new HSLFSlideShow();
HSLFSlide sl = ppt.createSlide();
HSLFTextRun rt;
String val = "Hello, World!";
// Create a new textbox, and give it lots of properties
HSLFTextBox txtbox = new HSLFTextBox();
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
txtbox.setText(val);
rt.setFontFamily("Arial");
rt.setFontSize(42d);
rt.setBold(true);
rt.setItalic(true);
rt.setUnderlined(false);
rt.setFontColor(Color.red);
sl.addShape(txtbox);
// Check it before save
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals(val, rt.getRawText());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
assertTrue(sameColor(Color.red, rt.getFontColor()));
// Serialize and read again
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
sl = ppt2.getSlides().get(0);
txtbox = (HSLFTextBox) sl.getShapes().get(0);
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
// Check after save
assertEquals(val, rt.getRawText());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
assertTrue(sameColor(Color.red, rt.getFontColor()));
ppt2.close();
}
use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.
the class TestHyperlink method bug47291.
@Test
public void bug47291() throws IOException {
HSLFSlideShow ppt1 = new HSLFSlideShow();
HSLFSlide slide1 = ppt1.createSlide();
HSLFTextRun r1 = slide1.createTextBox().setText("page1");
HSLFHyperlink hl1 = r1.createHyperlink();
hl1.linkToEmail("dev@poi.apache.org");
HSLFTextRun r2 = ppt1.createSlide().createTextBox().setText("page2");
HSLFHyperlink hl2 = r2.createHyperlink();
hl2.linkToLastSlide();
HSLFSlide sl1 = ppt1.createSlide();
HSLFTextBox tb1 = sl1.createTextBox();
tb1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
tb1.appendText("text1 ", false);
HSLFTextRun r3 = tb1.appendText("link", false);
tb1.appendText(" text2", false);
HSLFHyperlink hl3 = r3.createHyperlink();
hl3.linkToSlide(slide1);
HSLFTextRun r4 = ppt1.createSlide().createTextBox().setText("page4");
HSLFHyperlink hl4 = r4.createHyperlink();
hl4.linkToUrl("http://poi.apache.org");
HSLFTextBox tb5 = ppt1.createSlide().createTextBox();
tb5.setText("page5");
HSLFHyperlink hl5 = tb5.createHyperlink();
hl5.linkToFirstSlide();
HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
ppt1.close();
List<HSLFSlide> slides = ppt2.getSlides();
tb1 = (HSLFTextBox) slides.get(0).getShapes().get(0);
hl1 = tb1.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
assertNotNull(hl1);
assertEquals("dev@poi.apache.org", hl1.getLabel());
assertEquals(HyperlinkType.EMAIL, hl1.getTypeEnum());
HSLFTextBox tb2 = (HSLFTextBox) slides.get(1).getShapes().get(0);
hl2 = tb2.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
assertNotNull(hl2);
assertEquals(InteractiveInfoAtom.LINK_LastSlide, hl2.getInfo().getInteractiveInfoAtom().getHyperlinkType());
assertEquals(HyperlinkType.DOCUMENT, hl2.getTypeEnum());
HSLFTextBox tb3 = (HSLFTextBox) slides.get(2).getShapes().get(0);
hl3 = tb3.getTextParagraphs().get(0).getTextRuns().get(1).getHyperlink();
assertNotNull(hl3);
assertEquals(ppt2.getSlides().get(0)._getSheetNumber(), Integer.parseInt(hl3.getAddress().split(",")[0]));
assertEquals(HyperlinkType.DOCUMENT, hl3.getTypeEnum());
HSLFTextBox tb4 = (HSLFTextBox) slides.get(3).getShapes().get(0);
hl4 = tb4.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
assertNotNull(hl4);
assertEquals("http://poi.apache.org", hl4.getLabel());
assertEquals(HyperlinkType.URL, hl4.getTypeEnum());
tb5 = (HSLFTextBox) slides.get(4).getShapes().get(0);
hl5 = tb5.getHyperlink();
assertNotNull(hl5);
assertEquals(InteractiveInfoAtom.LINK_FirstSlide, hl5.getInfo().getInteractiveInfoAtom().getHyperlinkType());
assertEquals(HyperlinkType.DOCUMENT, hl5.getTypeEnum());
ppt2.close();
}
use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.
the class TestSlideMaster method testTitleMaster.
/**
* Slide 3 has title layout and follows the TitleMaster. Verify that.
*/
@Test
public void testTitleMaster() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
HSLFSlide slide = ppt.getSlides().get(2);
HSLFMasterSheet masterSheet = slide.getMasterSheet();
assertTrue(masterSheet instanceof HSLFTitleMaster);
for (List<HSLFTextParagraph> txt : slide.getTextParagraphs()) {
HSLFTextRun rt = txt.get(0).getTextRuns().get(0);
switch(txt.get(0).getRunType()) {
case TextHeaderAtom.CENTER_TITLE_TYPE:
assertEquals("Arial", rt.getFontFamily());
assertEquals(32, rt.getFontSize(), 0);
assertEquals(true, rt.isBold());
assertEquals(true, rt.isUnderlined());
break;
case TextHeaderAtom.CENTRE_BODY_TYPE:
assertEquals("Courier New", rt.getFontFamily());
assertEquals(20, rt.getFontSize(), 0);
assertEquals(true, rt.isBold());
assertEquals(false, rt.isUnderlined());
break;
}
}
ppt.close();
}
Aggregations