lost Shape layout when set text

723 views Asked by At

I am using Apache POI 3.12 for interacting with Powerpoint. I open an existing Powerpoint instance:

File file = new File("PATH_TO_FILE...");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));

In this Powerpoint application, I have defined some named shapes: At runtime I want to replace certain values:

XSLFSlide[] slide = ppt.getSlides();

for (int i = 0; i < slide.length; i++) {

    XSLFShape[] sh = slide[i].getShapes();
    for (int j = 0; j < sh.length; j++) {
        if ("SHAPE_NAME".equals(sh[j].getShapeName())) {
            if (sh[j] instanceof XSLFAutoShape) {
                XSLFAutoShape shape = (XSLFAutoShape) sh[j];
                shape.setText("BlaBla");                
            }
        }
    }
    ...
}

This are working very well, but when I change the text of the Shape, then automatically the defined layout for this Shape it lost. Is there a way to prevent this?

1

There are 1 answers

0
Higune On BEST ANSWER

I have solved my issue: You must explicit save the layout settings. Later when you edit the text, you must set these values.

Color fontColor = null;
String fontFamily = null;
double fontSize = 0.0;
boolean italic = false;
boolean bold = false;
boolean underline = false;
for (XSLFTextParagraph paragraph : autoShape.getTextParagraphs()) {
    for (XSLFTextRun text : paragraph.getTextRuns()) {
        fontColor = text.getFontColor();
        fontFamily = text.getFontFamily();
        fontSize = text.getFontSize();
        italic = text.isItalic();
        bold = text.isBold();
        underline = text.isUnderline();
    }
}
autoShape.clearText();
XSLFTextParagraph addNewTextParagraph = autoShape.addNewTextParagraph();

XSLFTextRun addNewTextRun = addNewTextParagraph.addNewTextRun();
addNewTextRun.setText(values.get(0)[1]);
addNewTextRun.setFontColor(fontColor);
addNewTextRun.setFontFamily(fontFamily);
addNewTextRun.setFontSize(fontSize);
addNewTextRun.setItalic(italic);
addNewTextRun.setBold(bold);
addNewTextRun.setUnderline(underline);