If your attempt is to do the same as Change the default font in PowerPoint, then this changes the major font and the minor font of the font scheme in used theme.
You can get XSLFTheme from each slide of the slide show, so also from the master. But as you found already there are getters but no setters for major font and minor font. So we need using low level ooxml-schemas
classes.
Following code example provides setMajorFont
and setMinorFont
method which sets type face of major font and minor font in the theme for either Latin script, east Asia script or complex script.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
public class PowerPointChangeThemeFonts {
enum Script {
LATIN, //Latin script
EA, //east Asia script
CS //complex script
}
static void setMajorFont(XSLFTheme theme, Script script, String typeFace) {
CTOfficeStyleSheet styleSheet = theme.getXmlObject();
CTBaseStyles themeElements = styleSheet.getThemeElements();
CTFontScheme fontScheme = themeElements.getFontScheme();
CTFontCollection fontCollection = fontScheme.getMajorFont();
CTTextFont textFont = null;
if (script == Script.LATIN) {
textFont = fontCollection.getLatin();
textFont.setTypeface(typeFace);
} else if (script == Script.EA) {
textFont = fontCollection.getEa();
textFont.setTypeface(typeFace);
} else if (script == Script.CS) {
textFont = fontCollection.getCs();
textFont.setTypeface(typeFace);
}
}
static void setMinorFont(XSLFTheme theme, Script script, String typeFace) {
CTOfficeStyleSheet styleSheet = theme.getXmlObject();
CTBaseStyles themeElements = styleSheet.getThemeElements();
CTFontScheme fontScheme = themeElements.getFontScheme();
CTFontCollection fontCollection = fontScheme.getMinorFont();
CTTextFont textFont = null;
if (script == Script.LATIN) {
textFont = fontCollection.getLatin();
textFont.setTypeface(typeFace);
} else if (script == Script.EA) {
textFont = fontCollection.getEa();
textFont.setTypeface(typeFace);
} else if (script == Script.CS) {
textFont = fontCollection.getCs();
textFont.setTypeface(typeFace);
}
}
public static void main(String args[]) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream("./PPTX.pptx"));
if (slideShow.getSlideMasters().size() > 0) {
XSLFSlideMaster master = slideShow.getSlideMasters().get(0);
XSLFTheme theme = master.getTheme();
setMajorFont(theme, Script.LATIN, "Courier New");
setMinorFont(theme, Script.LATIN, "Courier New");
}
FileOutputStream out = new FileOutputStream("./PPTXNew.pptx");
slideShow.write(out);
out.close();
slideShow.close();
}
}
PowrPoint
sGUI
to achieve ẁhat you want? – Axel Richter