Search in sources :

Example 21 with CTTextParagraphProperties

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties in project poi by apache.

the class XSLFTextShape method appendText.

@Override
public XSLFTextRun appendText(String text, boolean newParagraph) {
    if (text == null)
        return null;
    // copy properties from last paragraph / textrun or paragraph end marker
    CTTextParagraphProperties otherPPr = null;
    CTTextCharacterProperties otherRPr = null;
    boolean firstPara;
    XSLFTextParagraph para;
    if (_paragraphs.isEmpty()) {
        firstPara = false;
        para = null;
    } else {
        firstPara = !newParagraph;
        para = _paragraphs.get(_paragraphs.size() - 1);
        CTTextParagraph ctp = para.getXmlObject();
        otherPPr = ctp.getPPr();
        List<XSLFTextRun> runs = para.getTextRuns();
        if (!runs.isEmpty()) {
            XSLFTextRun r0 = runs.get(runs.size() - 1);
            otherRPr = r0.getRPr(false);
            if (otherRPr == null) {
                otherRPr = ctp.getEndParaRPr();
            }
        }
    // don't copy endParaRPr to the run in case there aren't any other runs
    // this is the case when setText() was called initially
    // otherwise the master style will be overridden/ignored
    }
    XSLFTextRun run = null;
    for (String lineTxt : text.split("\\r\\n?|\\n")) {
        if (!firstPara) {
            if (para != null) {
                CTTextParagraph ctp = para.getXmlObject();
                CTTextCharacterProperties unexpectedRPr = ctp.getEndParaRPr();
                if (unexpectedRPr != null && unexpectedRPr != otherRPr) {
                    ctp.unsetEndParaRPr();
                }
            }
            para = addNewTextParagraph();
            if (otherPPr != null) {
                para.getXmlObject().setPPr(otherPPr);
            }
        }
        boolean firstRun = true;
        for (String runText : lineTxt.split("[]")) {
            if (!firstRun) {
                para.addLineBreak();
            }
            run = para.addNewTextRun();
            run.setText(runText);
            if (otherRPr != null) {
                run.getRPr(true).set(otherRPr);
            }
            firstRun = false;
        }
        firstPara = false;
    }
    assert (run != null);
    return run;
}
Also used : CTTextParagraph(org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph) CTTextCharacterProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties) CTTextParagraphProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties)

Example 22 with CTTextParagraphProperties

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties in project poi by apache.

the class TestXSLFTextShape method testTitleStyles.

@Test
public void testTitleStyles() throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();
    XSLFSlideMaster master = ppt.getSlideMasters().get(0);
    XSLFTheme theme = master.getTheme();
    XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE);
    XSLFSlide slide = ppt.createSlide(layout);
    assertSame(layout, slide.getSlideLayout());
    assertSame(master, slide.getSlideMaster());
    XSLFTextShape titleShape = slide.getPlaceholder(0);
    titleShape.setText("Apache POI");
    XSLFTextParagraph paragraph = titleShape.getTextParagraphs().get(0);
    XSLFTextRun textRun = paragraph.getTextRuns().get(0);
    // level 1 : default title style on the master slide
    // /p:sldMaster/p:txStyles/p:titleStyle/a:lvl1pPr
    CTTextParagraphProperties lv1PPr = master.getXmlObject().getTxStyles().getTitleStyle().getLvl1PPr();
    CTTextCharacterProperties lv1CPr = lv1PPr.getDefRPr();
    assertEquals(4400, lv1CPr.getSz());
    assertEquals(44.0, textRun.getFontSize(), 0);
    assertEquals("+mj-lt", lv1CPr.getLatin().getTypeface());
    assertEquals("Calibri", theme.getMajorFont());
    assertEquals("Calibri", textRun.getFontFamily());
    lv1CPr.setSz(3200);
    assertEquals(32.0, textRun.getFontSize(), 0);
    lv1CPr.getLatin().setTypeface("Arial");
    assertEquals("Arial", textRun.getFontFamily());
    assertEquals(STTextAlignType.CTR, lv1PPr.getAlgn());
    assertEquals(TextAlign.CENTER, paragraph.getTextAlign());
    lv1PPr.setAlgn(STTextAlignType.L);
    assertEquals(TextAlign.LEFT, paragraph.getTextAlign());
    // level 2: title placeholder on the master slide
    // /p:sldMaster/p:cSld/p:spTree/p:sp/p:nvPr/p:ph[@type="title"]
    XSLFTextShape tx2 = master.getPlaceholder(0);
    CTTextParagraphProperties lv2PPr = tx2.getTextBody(true).getLstStyle().addNewLvl1PPr();
    CTTextCharacterProperties lv2CPr = lv2PPr.addNewDefRPr();
    lv2CPr.setSz(3300);
    assertEquals(33.0, textRun.getFontSize(), 0);
    lv2CPr.addNewLatin().setTypeface("Times");
    assertEquals("Times", textRun.getFontFamily());
    lv2PPr.setAlgn(STTextAlignType.R);
    assertEquals(TextAlign.RIGHT, paragraph.getTextAlign());
    // level 3: title placeholder on the slide layout
    // /p:sldLayout /p:cSld/p:spTree/p:sp/p:nvPr/p:ph[@type="ctrTitle"]
    XSLFTextShape tx3 = layout.getPlaceholder(0);
    CTTextParagraphProperties lv3PPr = tx3.getTextBody(true).getLstStyle().addNewLvl1PPr();
    CTTextCharacterProperties lv3CPr = lv3PPr.addNewDefRPr();
    lv3CPr.setSz(3400);
    assertEquals(34.0, textRun.getFontSize(), 0);
    lv3CPr.addNewLatin().setTypeface("Courier New");
    assertEquals("Courier New", textRun.getFontFamily());
    lv3PPr.setAlgn(STTextAlignType.CTR);
    assertEquals(TextAlign.CENTER, paragraph.getTextAlign());
    // level 4: default text properties in the shape itself
    // ./p:sp/p:txBody/a:lstStyle/a:lvl1pPr
    CTTextParagraphProperties lv4PPr = titleShape.getTextBody(true).getLstStyle().addNewLvl1PPr();
    CTTextCharacterProperties lv4CPr = lv4PPr.addNewDefRPr();
    lv4CPr.setSz(3500);
    assertEquals(35.0, textRun.getFontSize(), 0);
    lv4CPr.addNewLatin().setTypeface("Arial");
    assertEquals("Arial", textRun.getFontFamily());
    lv4PPr.setAlgn(STTextAlignType.L);
    assertEquals(TextAlign.LEFT, paragraph.getTextAlign());
    // level 5: text properties are defined in the text run
    CTTextParagraphProperties lv5PPr = paragraph.getXmlObject().addNewPPr();
    CTTextCharacterProperties lv5CPr = textRun.getRPr(false);
    lv5CPr.setSz(3600);
    assertEquals(36.0, textRun.getFontSize(), 0);
    lv5CPr.addNewLatin().setTypeface("Calibri");
    assertEquals("Calibri", textRun.getFontFamily());
    lv5PPr.setAlgn(STTextAlignType.CTR);
    assertEquals(TextAlign.CENTER, paragraph.getTextAlign());
    ppt.close();
}
Also used : CTTextCharacterProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties) CTTextParagraphProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties) Test(org.junit.Test)

Aggregations

CTTextParagraphProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties)22 ParagraphPropertyFetcher (org.apache.poi.xslf.model.ParagraphPropertyFetcher)6 CTTextSpacing (org.openxmlformats.schemas.drawingml.x2006.main.CTTextSpacing)5 CTTextCharacterProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties)4 CTPlaceholder (org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder)3 Color (java.awt.Color)2 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)2 Test (org.junit.Test)2 CTColor (org.openxmlformats.schemas.drawingml.x2006.main.CTColor)2 CTSRgbColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor)2 CTTextBulletSizePoint (org.openxmlformats.schemas.drawingml.x2006.main.CTTextBulletSizePoint)2 CTTextTabStopList (org.openxmlformats.schemas.drawingml.x2006.main.CTTextTabStopList)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 AutoNumberingScheme (org.apache.poi.sl.usermodel.AutoNumberingScheme)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlObject (org.apache.xmlbeans.XmlObject)1 CTTextAutonumberBullet (org.openxmlformats.schemas.drawingml.x2006.main.CTTextAutonumberBullet)1 CTTextBulletSizePercent (org.openxmlformats.schemas.drawingml.x2006.main.CTTextBulletSizePercent)1 CTTextCharBullet (org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharBullet)1 CTTextFont (org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont)1