/** Tests getting and setting the font size on rich and non rich text runs */ public void testFontSize() { Slide slideOne = ss.getSlides()[0]; TextRun[] textRuns = slideOne.getTextRuns(); RichTextRun rtr = textRuns[0].getRichTextRuns()[0]; Slide slideOneR = ssRichB.getSlides()[0]; TextRun[] textRunsR = slideOneR.getTextRuns(); RichTextRun rtrRa = textRunsR[0].getRichTextRuns()[0]; RichTextRun rtrRb = textRunsR[1].getRichTextRuns()[0]; RichTextRun rtrRc = textRunsR[1].getRichTextRuns()[3]; String defaultFont = "Arial"; // Start off with rich one // First run has defaults assertEquals(44, rtrRa.getFontSize()); assertEquals(defaultFont, rtrRa.getFontName()); // Second is size 20, default font assertEquals(20, rtrRb.getFontSize()); assertEquals(defaultFont, rtrRb.getFontName()); // Third is size 24, alt font assertEquals(24, rtrRc.getFontSize()); assertEquals("Times New Roman", rtrRc.getFontName()); // Change 2nd to different size and font assertEquals(2, ssRichB.getFontCollection().getChildRecords().length); // Default + TNR rtrRb.setFontSize(18); rtrRb.setFontName("Courier"); assertEquals( 3, ssRichB.getFontCollection().getChildRecords().length); // Default + TNR + Courier assertEquals(18, rtrRb.getFontSize()); assertEquals("Courier", rtrRb.getFontName()); // Now do non rich one assertEquals(44, rtr.getFontSize()); assertEquals(defaultFont, rtr.getFontName()); assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default assertNull(rtr._getRawCharacterStyle()); assertNull(rtr._getRawParagraphStyle()); // Change Font size rtr.setFontSize(99); assertEquals(99, rtr.getFontSize()); assertEquals(defaultFont, rtr.getFontName()); assertNotNull(rtr._getRawCharacterStyle()); assertNotNull(rtr._getRawParagraphStyle()); assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default // Change Font size and name rtr.setFontSize(25); rtr.setFontName("Times New Roman"); assertEquals(25, rtr.getFontSize()); assertEquals("Times New Roman", rtr.getFontName()); assertNotNull(rtr._getRawCharacterStyle()); assertNotNull(rtr._getRawParagraphStyle()); assertEquals(2, ss.getFontCollection().getChildRecords().length); }
/** * Adds a slide containing the metric's graph to the PowerPoint slide deck. The title is usually * the metric's name and is usually in camelCase format. This will be converted to individual, * capitalized words to the slide's title. The metric's data is used to determine the total count * across all of the metric's data, which is displayed at the bottom of the slide, under the * graph. * * @param ppt the PowerPoint slide deck to add this slide to * @param title the title for this slide * @param graph the metric's graph to be added to this slide * @param metricData the metric's data * @throws IOException * @throws MetricsGraphException */ private void createSlide(SlideShow ppt, String title, byte[] graph, MetricData metricData) throws IOException, MetricsGraphException { LOGGER.trace("ENTERING: createSlide"); if (LOGGER.isDebugEnabled()) { java.awt.Dimension pgsize = ppt.getPageSize(); int pgx = pgsize.width; // slide width (720) int pgy = pgsize.height; // slide height (540) LOGGER.debug("ppt page width = " + pgx); LOGGER.debug("ppt page height = " + pgy); } // Convert title, if it is in camelCase, to individual words with each word // starting with a capital letter String slideTitle = convertCamelCase(title); // String[] titleParts = StringUtils.splitByCharacterTypeCamelCase(title); // String slideTitle = ""; // for (String titlePart : titleParts) // { // slideTitle += titlePart + " "; // } // slideTitle = StringUtils.capitalize(slideTitle); Slide slide = ppt.createSlide(); // Add the title to the slide TextBox titleTextBox = slide.addTitle(); TextRun textRun = titleTextBox.getTextRun(); textRun.getRichTextRuns()[0].setFontSize(32); titleTextBox.setText(slideTitle); titleTextBox.setHorizontalAlignment(TextBox.AlignCenter); // Add the metric's graph to the slide int idx = ppt.addPicture(graph, Picture.PNG); Picture pict = new Picture(idx); // set graph's position and size in the slide // (Be sure to maintain aspect ratio for the image when specifying the // width and height. Refer to width and height values used in createGraph()) pict.setAnchor(new Rectangle(20, 100, 650, 325)); slide.addShape(pict); // If metric has a total count, add it under the graph on the slide if (metricData.hasTotalCount()) { TextBox totalCountTextBox = new TextBox(); textRun = totalCountTextBox.getTextRun(); textRun.getRichTextRuns()[0].setFontSize(14); totalCountTextBox.setText("Total Count: " + metricData.getTotalCount()); totalCountTextBox.setHorizontalAlignment(TextBox.AlignLeft); // x,y values determined relative to x,y of graph's anchor position // and the height of the graph totalCountTextBox.setAnchor(new Rectangle(20, 450, 250, 80)); slide.addShape(totalCountTextBox); } LOGGER.trace("EXITING: createSlide"); }
public static void main(String[] args) throws Exception { SlideShow ss = new SlideShow(); Slide s1 = ss.createSlide(); TextBox title = s1.addTitle(); title.setText("サンプル文書"); int picIndex = ss.addPicture(new File("project-logo.jpg"), Picture.JPEG); Picture logo = new Picture(picIndex); s1.addShape(logo); ss.write(new FileOutputStream(new File("sample.ppt"))); }
public void testChangeWriteRead() throws Exception { HSLFSlideShow[] h = new HSLFSlideShow[] {hss, hssRichA, hssRichB}; Slide[] s = new Slide[] {ss.getSlides()[0], ssRichA.getSlides()[0], ssRichB.getSlides()[0]}; for (int i = 0; i < h.length; i++) { // Change Slide slideOne = s[i]; TextRun[] textRuns = slideOne.getTextRuns(); RichTextRun rtr = textRuns[0].getRichTextRuns()[0]; rtr.setBold(true); rtr.setFontSize(18); rtr.setFontName("Courier"); // Check it took those assertEquals(true, rtr.isBold()); assertEquals(18, rtr.getFontSize()); assertEquals("Courier", rtr.getFontName()); // Write out and back in ByteArrayOutputStream baos = new ByteArrayOutputStream(); h[i].write(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); HSLFSlideShow readHSLF = new HSLFSlideShow(bais); SlideShow readS = new SlideShow(readHSLF); // Tweak existing one again, to ensure really worked rtr.setBold(false); rtr.setFontSize(17); rtr.setFontName("CourierZZ"); // Check it took those changes assertEquals(false, rtr.isBold()); assertEquals(17, rtr.getFontSize()); assertEquals("CourierZZ", rtr.getFontName()); // Now, look at the one we changed, wrote out, and read back in // Ensure it does contain our original modifications Slide slideOneRR = readS.getSlides()[0]; TextRun[] textRunsRR = slideOneRR.getTextRuns(); RichTextRun rtrRRa = textRunsRR[0].getRichTextRuns()[0]; assertEquals(true, rtrRRa.isBold()); assertEquals(18, rtrRRa.getFontSize()); assertEquals("Courier", rtrRRa.getFontName()); } }
/** * Creates an BufferedImage of the passed slide. * * @param tmpslide slide to copied to a BufferedImage. * @param dimension Dimension, the dimension of the slide * @param tmpimgWidth int, width which the slide will be scaled to. * @param tmpImgHeight int, height which the slide will be scaled to. * @return BufferedImage, the image scaled image copy of the slide. */ private BufferedImage slideToImage(Slide tmpslide, Dimension dimension) { BufferedImage img = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_RGB); // create the graphic "painter" Graphics2D graphics = img.createGraphics(); graphics.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); graphics.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setPaint(Color.white); // creates a square graphics.fill(new Rectangle2D.Float(0, 0, dimension.width, dimension.height)); // render image in the square tmpslide.draw(graphics); return img; }
public void testSetParagraphStyles() throws Exception { SlideShow ppt = new SlideShow(); Slide slide = ppt.createSlide(); TextBox shape = new TextBox(); RichTextRun rt = shape.getTextRun().getRichTextRuns()[0]; shape.setText("Hello, World!\r" + "This should be\r" + "Multiline text"); rt.setFontSize(42); rt.setBullet(true); rt.setTextOffset(50); rt.setBulletOffset(0); rt.setBulletChar('\u263A'); slide.addShape(shape); assertEquals(42, rt.getFontSize()); assertEquals(true, rt.isBullet()); assertEquals(50, rt.getTextOffset()); assertEquals(0, rt.getBulletOffset()); assertEquals('\u263A', rt.getBulletChar()); shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); slide.addShape(shape); // serialize and read again ByteArrayOutputStream out = new ByteArrayOutputStream(); ppt.write(out); out.close(); ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray())); slide = ppt.getSlides()[0]; shape = (TextBox) slide.getShapes()[0]; rt = shape.getTextRun().getRichTextRuns()[0]; assertEquals(42, rt.getFontSize()); assertEquals(true, rt.isBullet()); assertEquals(50, rt.getTextOffset()); assertEquals(0, rt.getBulletOffset()); assertEquals('\u263A', rt.getBulletChar()); }
/** Test the stuff about getting/setting bold on a rich text run */ public void testBoldRich() { Slide slideOneR = ssRichA.getSlides()[0]; TextRun[] textRunsR = slideOneR.getTextRuns(); RichTextRun[] rtrs = textRunsR[1].getRichTextRuns(); assertEquals(3, rtrs.length); assertTrue(rtrs[0].isBold()); assertFalse(rtrs[1].isBold()); assertFalse(rtrs[2].isBold()); rtrs[0].setBold(true); rtrs[1].setBold(true); assertTrue(rtrs[0].isBold()); assertTrue(rtrs[1].isBold()); rtrs[0].setBold(false); rtrs[1].setBold(false); assertFalse(rtrs[0].isBold()); assertFalse(rtrs[1].isBold()); }
/** Test the stuff about getting/setting bold on a non rich text run */ public void testBoldNonRich() { Slide slideOne = ss.getSlides()[0]; TextRun[] textRuns = slideOne.getTextRuns(); RichTextRun rtr = textRuns[0].getRichTextRuns()[0]; assertNull(rtr._getRawCharacterStyle()); assertNull(rtr._getRawParagraphStyle()); assertFalse(rtr.isBold()); // Now set it to not bold rtr.setBold(false); // setting bold=false doesn't change the internal state assertNull(rtr._getRawCharacterStyle()); assertNull(rtr._getRawParagraphStyle()); assertFalse(rtr.isBold()); // And now make it bold rtr.setBold(true); assertNotNull(rtr._getRawCharacterStyle()); assertNotNull(rtr._getRawParagraphStyle()); assertTrue(rtr.isBold()); }
/** * Test that we can do the right things when the paragraph styles run out before the character * styles do, when we tweak something and write back out. */ public void testParagraphStylesShorterTheCharStylesWrite() throws Exception { assertMatchesSLTWC(ssRichC); assertMatchesFileC(ssRichC); Slide slideSevenC = ssRichC.getSlides()[6]; TextRun[] s7tr = slideSevenC.getTextRuns(); RichTextRun[] s7rtr0 = s7tr[0].getRichTextRuns(); RichTextRun[] s7rtr1 = s7tr[1].getRichTextRuns(); RichTextRun[] s7rtr2 = s7tr[2].getRichTextRuns(); String oldText; // Reset the text on the last run // Need to ensure it's a run that really has styles! oldText = s7rtr2[0].getRawText(); s7rtr2[0].setText(oldText); assertEquals(oldText, s7rtr2[0].getText()); assertEquals(oldText, s7tr[2].getText()); assertEquals(oldText.length() + 1, s7rtr2[0]._getRawCharacterStyle().getCharactersCovered()); assertEquals(oldText.length() + 1, s7rtr2[0]._getRawParagraphStyle().getCharactersCovered()); assertMatchesSLTWC(ssRichC); assertMatchesFileC(ssRichC); // Reset the text on a shared paragraph oldText = s7rtr1[2].getRawText(); s7rtr1[2].setText(oldText); assertEquals(oldText, s7rtr1[2].getText()); assertEquals(oldText.length() + 1, s7rtr1[2]._getRawCharacterStyle().getCharactersCovered()); assertMatchesSLTWC(ssRichC); assertMatchesFileC(ssRichC); // Reset the text on a shared paragraph+character s7rtr1[1].setText(s7rtr1[1].getRawText()); assertMatchesSLTWC(ssRichC); assertMatchesFileC(ssRichC); }
public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText) { StringBuffer ret = new StringBuffer(); if (getSlideText) { for (int i = 0; i < _slides.length; i++) { Slide slide = _slides[i]; // Slide header, if set HeadersFooters hf = slide.getHeadersFooters(); if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) { ret.append(hf.getHeaderText() + "\n"); } // Slide text TextRun[] runs = slide.getTextRuns(); for (int j = 0; j < runs.length; j++) { TextRun run = runs[j]; if (run != null) { String text = run.getText(); ret.append(text); if (!text.endsWith("\n")) { ret.append("\n"); } } } // Slide footer, if set if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) { ret.append(hf.getFooterText() + "\n"); } // Comments, if requested and present if (getCommentText) { Comment[] comments = slide.getComments(); for (int j = 0; j < comments.length; j++) { ret.append(comments[j].getAuthor() + " - " + comments[j].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 HashSet seenNotes = new HashSet(); HeadersFooters hf = _show.getNotesHeadersFooters(); for (int i = 0; i < _slides.length; i++) { Notes notes = _slides[i].getNotesSheet(); if (notes == null) { continue; } Integer id = new Integer(notes._getSheetNumber()); if (seenNotes.contains(id)) { continue; } seenNotes.add(id); // Repeat the Notes header, if set if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) { ret.append(hf.getHeaderText() + "\n"); } // Notes text TextRun[] runs = notes.getTextRuns(); if (runs != null && runs.length > 0) { for (int j = 0; j < runs.length; j++) { TextRun run = runs[j]; String text = run.getText(); ret.append(text); if (!text.endsWith("\n")) { ret.append("\n"); } } } // Repeat the notes footer, if set if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) { ret.append(hf.getFooterText() + "\n"); } } } return ret.toString(); }
/** * Test that we can do the right things when the paragraph styles run out before the character * styles do */ public void testParagraphStylesShorterTheCharStyles() { // Check we have the right number of sheets Slide[] slides = ssRichC.getSlides(); assertEquals(14, slides.length); // Check the number of text runs on interesting sheets Slide slideThreeC = ssRichC.getSlides()[2]; Slide slideSevenC = ssRichC.getSlides()[6]; assertEquals(3, slideThreeC.getTextRuns().length); assertEquals(5, slideSevenC.getTextRuns().length); // On slide three, we should have: // TR: // You are an important supplier of various items that I need // . // TR: // Source: Internal focus groups // TR: // Illustrative Example // . TextRun[] s3tr = slideThreeC.getTextRuns(); RichTextRun[] s3rtr0 = s3tr[0].getRichTextRuns(); RichTextRun[] s3rtr1 = s3tr[1].getRichTextRuns(); RichTextRun[] s3rtr2 = s3tr[2].getRichTextRuns(); assertEquals(2, s3rtr0.length); assertEquals(1, s3rtr1.length); assertEquals(2, s3rtr2.length); assertEquals("You are an important supplier of various items that I need", s3rtr0[0].getText()); assertEquals("", s3rtr0[1].getText()); assertEquals("Source: Internal focus groups", s3rtr1[0].getText()); assertEquals("Illustrative Example", s3rtr2[0].getText()); assertEquals("", s3rtr2[1].getText()); assertTrue(s3rtr0[0]._isParagraphStyleShared()); assertTrue(s3rtr0[1]._isParagraphStyleShared()); assertFalse(s3rtr1[0]._isParagraphStyleShared()); assertTrue(s3rtr2[0]._isParagraphStyleShared()); assertTrue(s3rtr2[1]._isParagraphStyleShared()); assertFalse(s3rtr0[0]._isCharacterStyleShared()); assertFalse(s3rtr0[1]._isCharacterStyleShared()); assertFalse(s3rtr1[0]._isCharacterStyleShared()); assertFalse(s3rtr2[0]._isCharacterStyleShared()); assertFalse(s3rtr2[1]._isCharacterStyleShared()); // On slide seven, we have: // TR: // (text) // TR: // <ps>(text a)</ps><ps>(text a)(text b)</ps> // TR: // (text) TextRun[] s7tr = slideSevenC.getTextRuns(); RichTextRun[] s7rtr0 = s7tr[0].getRichTextRuns(); RichTextRun[] s7rtr1 = s7tr[1].getRichTextRuns(); RichTextRun[] s7rtr2 = s7tr[2].getRichTextRuns(); assertEquals(1, s7rtr0.length); assertEquals(3, s7rtr1.length); assertEquals(1, s7rtr2.length); assertFalse(s7rtr0[0]._isParagraphStyleShared()); assertFalse(s7rtr1[0]._isParagraphStyleShared()); assertTrue(s7rtr1[1]._isParagraphStyleShared()); assertTrue(s7rtr1[2]._isParagraphStyleShared()); assertFalse(s7rtr2[0]._isParagraphStyleShared()); assertFalse(s7rtr0[0]._isCharacterStyleShared()); assertTrue(s7rtr1[0]._isCharacterStyleShared()); assertTrue(s7rtr1[1]._isCharacterStyleShared()); assertFalse(s7rtr1[2]._isCharacterStyleShared()); assertFalse(s7rtr2[0]._isCharacterStyleShared()); }