0
votes

I am using Apache POI to modify a pptx. I would like to change the font of the whole pptx at once.

I know in Powerpoint this is possible by changing the fonts of the theme (if the slides all use these fonts), but I cannot make it work through Apache POI.

What I found so far is that I can set the font family of a single XSLFTextRun by using e.g. run.setFontFamily(HSSFFont.FONT_ARIAL).

Edit: I found that the XSLFTheme Class of the XSLFSlideMaster does have a getMajorFont() and a getMinorFont() method. I think these might be the fonts I need to change, but there are no setters for these fields.

Thanks for your help!

1
The best practice is to key all font instances to the font theme. Then you just have to update the theme to change formatting.John Korchok
@JohnKorchok Thanks for your answer. In the presentation I am trying to modify, I am using the fonts from the slide master I created. So, in PowerPoint I am able to modify the font of the whole presentation at once by changing the fonts in the slide master. Now I am trying to do the same with Apache POI.Amygdala
Not clear what exactly is the need. You can get the slide masters from the slideshow using XMLSlideShow.getSlideMasters. But each master defines multiple layouts. So is the need to change all font settings of all placeholders in all layouts? What exactly are you doing in PowrPoints GUI to achieve ẁhat you want?Axel Richter
@AxelRichter I edited the question to (hopefully) make it more clear.Amygdala

1 Answers

0
votes

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();
 }
}