Search in sources :

Example 6 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.

the class TableDemo method create1stTable.

static void create1stTable(HSLFSlide slide) {
    //six rows, two columns
    HSLFTable table1 = slide.createTable(6, 2);
    for (int i = 0; i < txt1.length; i++) {
        for (int j = 0; j < txt1[i].length; j++) {
            HSLFTableCell cell = table1.getCell(i, j);
            HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
            rt.setFontFamily("Arial");
            rt.setFontSize(10d);
            if (i == 0) {
                cell.getFill().setForegroundColor(new Color(227, 227, 227));
            } else {
                rt.setBold(true);
            }
            cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            cell.setHorizontalCentered(true);
            cell.setText(txt1[i][j]);
        }
    }
    DrawTableShape dts1 = new DrawTableShape(table1);
    dts1.setAllBorders(1.0, Color.black);
    table1.setColumnWidth(0, 300);
    table1.setColumnWidth(1, 150);
    int pgWidth = slide.getSlideShow().getPageSize().width;
    table1.moveTo((pgWidth - table1.getAnchor().getWidth()) / 2., 100.);
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) Color(java.awt.Color) HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell) DrawTableShape(org.apache.poi.sl.draw.DrawTableShape)

Example 7 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.

the class Hyperlinks method main.

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; i++) {
        FileInputStream is = new FileInputStream(args[i]);
        HSLFSlideShow ppt = new HSLFSlideShow(is);
        is.close();
        for (HSLFSlide slide : ppt.getSlides()) {
            System.out.println("\nslide " + slide.getSlideNumber());
            // read hyperlinks from the slide's text runs
            System.out.println("- reading hyperlinks from the text runs");
            for (List<HSLFTextParagraph> paras : slide.getTextParagraphs()) {
                for (HSLFTextParagraph para : paras) {
                    for (HSLFTextRun run : para) {
                        HSLFHyperlink link = run.getHyperlink();
                        if (link != null) {
                            System.out.println(toStr(link, run.getRawText()));
                        }
                    }
                }
            }
            // in PowerPoint you can assign a hyperlink to a shape without text,
            // for example to a Line object. The code below demonstrates how to
            // read such hyperlinks
            System.out.println("- reading hyperlinks from the slide's shapes");
            for (HSLFShape sh : slide.getShapes()) {
                if (sh instanceof HSLFSimpleShape) {
                    HSLFHyperlink link = ((HSLFSimpleShape) sh).getHyperlink();
                    if (link != null) {
                        System.out.println(toStr(link, null));
                    }
                }
            }
        }
        ppt.close();
    }
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) HSLFHyperlink(org.apache.poi.hslf.usermodel.HSLFHyperlink) HSLFSimpleShape(org.apache.poi.hslf.usermodel.HSLFSimpleShape) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) FileInputStream(java.io.FileInputStream)

Example 8 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.

the class PPGraphics2D method drawString.

/**
     * Renders the text specified by the specified <code>String</code>,
     * using the current text attribute state in the <code>Graphics2D</code> context.
     * The baseline of the first character is at position
     * (<i>x</i>,&nbsp;<i>y</i>) in the User Space.
     * The rendering attributes applied include the <code>Clip</code>,
     * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
     * <code>Composite</code> attributes. For characters in script systems
     * such as Hebrew and Arabic, the glyphs can be rendered from right to
     * left, in which case the coordinate supplied is the location of the
     * leftmost character on the baseline.
     * @param s the <code>String</code> to be rendered
     * @param x the x coordinate of the location where the
     * <code>String</code> should be rendered
     * @param y the y coordinate of the location where the
     * <code>String</code> should be rendered
     * @throws NullPointerException if <code>str</code> is
     *         <code>null</code>
     * @see #setPaint
     * @see java.awt.Graphics#setColor
     * @see java.awt.Graphics#setFont
     * @see #setTransform
     * @see #setComposite
     * @see #setClip
     */
public void drawString(String s, float x, float y) {
    HSLFTextBox txt = new HSLFTextBox(_group);
    txt.setSheet(_group.getSheet());
    txt.setText(s);
    HSLFTextRun rt = txt.getTextParagraphs().get(0).getTextRuns().get(0);
    rt.setFontSize((double) _font.getSize());
    rt.setFontFamily(_font.getFamily());
    if (getColor() != null)
        rt.setFontColor(DrawPaint.createSolidPaint(getColor()));
    if (_font.isBold())
        rt.setBold(true);
    if (_font.isItalic())
        rt.setItalic(true);
    txt.setBottomInset(0);
    txt.setTopInset(0);
    txt.setLeftInset(0);
    txt.setRightInset(0);
    txt.setWordWrap(false);
    txt.setHorizontalCentered(false);
    txt.setVerticalAlignment(VerticalAlignment.MIDDLE);
    TextLayout layout = new TextLayout(s, _font, getFontRenderContext());
    float ascent = layout.getAscent();
    float width = (float) Math.floor(layout.getAdvance());
    /**
         * Even if top and bottom margins are set to 0 PowerPoint
         * always sets extra space between the text and its bounding box.
         *
         * The approximation height = ascent*2 works good enough in most cases
         */
    float height = ascent * 2;
    /*
          In powerpoint anchor of a shape is its top left corner.
          Java graphics sets string coordinates by the baseline of the first character
          so we need to shift up by the height of the textbox
        */
    y -= height / 2 + ascent / 2;
    /*
          In powerpoint anchor of a shape is its top left corner.
          Java graphics sets string coordinates by the baseline of the first character
          so we need to shift down by the height of the textbox
        */
    txt.setAnchor(new Rectangle((int) x, (int) y, (int) width, (int) height));
    _group.addShape(txt);
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) Rectangle(java.awt.Rectangle) TextLayout(java.awt.font.TextLayout)

Example 9 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.

the class TestShapes method textBoxSet.

private void textBoxSet(String filename) throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow(_slTests.openResourceAsStream(filename));
    for (HSLFSlide sld : ss.getSlides()) {
        ArrayList<String> lst1 = new ArrayList<String>();
        for (List<HSLFTextParagraph> txt : sld.getTextParagraphs()) {
            for (HSLFTextParagraph p : txt) {
                for (HSLFTextRun r : p) {
                    lst1.add(r.getRawText());
                }
            }
        }
        ArrayList<String> lst2 = new ArrayList<String>();
        for (HSLFShape sh : sld.getShapes()) {
            if (sh instanceof HSLFTextShape) {
                HSLFTextShape tbox = (HSLFTextShape) sh;
                for (HSLFTextParagraph p : tbox.getTextParagraphs()) {
                    for (HSLFTextRun r : p) {
                        lst2.add(r.getRawText());
                    }
                }
            }
        }
        assertTrue(lst1.containsAll(lst2));
        assertTrue(lst2.containsAll(lst1));
    }
    ss.close();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) ArrayList(java.util.ArrayList) HSLFTextShape(org.apache.poi.hslf.usermodel.HSLFTextShape) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 10 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project poi by apache.

the class TestShapes method textBoxRead.

/**
     * Verify that we can read TextBox shapes
     * @throws Exception
     */
@Test
public void textBoxRead() throws IOException {
    ppt = new HSLFSlideShow(_slTests.openResourceAsStream("with_textbox.ppt"));
    HSLFSlide sl = ppt.getSlides().get(0);
    for (HSLFShape sh : sl.getShapes()) {
        assertTrue(sh instanceof HSLFTextBox);
        HSLFTextBox txtbox = (HSLFTextBox) sh;
        String text = txtbox.getText();
        assertNotNull(text);
        assertEquals(txtbox.getTextParagraphs().get(0).getTextRuns().size(), 1);
        HSLFTextRun rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
        if (text.equals("Hello, World!!!")) {
            assertEquals(32, rt.getFontSize(), 0);
            assertTrue(rt.isBold());
            assertTrue(rt.isItalic());
        } else if (text.equals("I am just a poor boy")) {
            assertEquals(44, rt.getFontSize(), 0);
            assertTrue(rt.isBold());
        } else if (text.equals("This is Times New Roman")) {
            assertEquals(16, rt.getFontSize(), 0);
            assertTrue(rt.isBold());
            assertTrue(rt.isItalic());
            assertTrue(rt.isUnderlined());
        } else if (text.equals("Plain Text")) {
            assertEquals(18, rt.getFontSize(), 0);
        }
    }
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Aggregations

HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)14 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)9 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)9 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)8 Test (org.junit.Test)8 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)3 Color (java.awt.Color)2 Rectangle2D (java.awt.geom.Rectangle2D)2 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)2 HSLFTable (org.apache.poi.hslf.usermodel.HSLFTable)2 HSLFTableCell (org.apache.poi.hslf.usermodel.HSLFTableCell)2 DrawTableShape (org.apache.poi.sl.draw.DrawTableShape)2 Rectangle (java.awt.Rectangle)1 TextLayout (java.awt.font.TextLayout)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Hyperlink (org.apache.poi.common.usermodel.Hyperlink)1