在 Apache POI 中更改 pptx 幻灯片母版中的字体
Change font in pptx slide master in Apache POI
我正在使用 Apache POI 修改 pptx。我想一次更改整个pptx的字体。
我知道在 Powerpoint 中这可以通过更改主题字体来实现(如果幻灯片都使用这些字体),但我无法通过 Apache POI 使其工作。
到目前为止我发现我可以使用例如设置单个 XSLFTextRun 的字体系列。 run.setFontFamily(HSSFFont.FONT_ARIAL)
。
编辑:我发现 XSLFSlideMaster 的 XSLFTheme Class 确实有一个 getMajorFont() 和一个 getMinorFont() 方法。我认为这些可能是我需要更改的字体,但这些字段没有设置器。
感谢您的帮助!
如果您的尝试与Change the default font in PowerPoint相同,那么这会更改所用主题中字体方案的主要字体和次要字体。
您可以从幻灯片放映的每张幻灯片中获取 XSLFTheme,母版也一样。但是正如您发现的那样,已经有主要字体和次要字体的吸气剂但没有二传手。所以我们需要使用低级别ooxml-schemas
类。
下面的代码示例提供了setMajorFont
和setMinorFont
方法来设置主题中主要字体和次要字体的字体,无论是拉丁文字、东亚文字还是复杂文字。
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();
}
}
我正在使用 Apache POI 修改 pptx。我想一次更改整个pptx的字体。
我知道在 Powerpoint 中这可以通过更改主题字体来实现(如果幻灯片都使用这些字体),但我无法通过 Apache POI 使其工作。
到目前为止我发现我可以使用例如设置单个 XSLFTextRun 的字体系列。 run.setFontFamily(HSSFFont.FONT_ARIAL)
。
编辑:我发现 XSLFSlideMaster 的 XSLFTheme Class 确实有一个 getMajorFont() 和一个 getMinorFont() 方法。我认为这些可能是我需要更改的字体,但这些字段没有设置器。
感谢您的帮助!
如果您的尝试与Change the default font in PowerPoint相同,那么这会更改所用主题中字体方案的主要字体和次要字体。
您可以从幻灯片放映的每张幻灯片中获取 XSLFTheme,母版也一样。但是正如您发现的那样,已经有主要字体和次要字体的吸气剂但没有二传手。所以我们需要使用低级别ooxml-schemas
类。
下面的代码示例提供了setMajorFont
和setMinorFont
方法来设置主题中主要字体和次要字体的字体,无论是拉丁文字、东亚文字还是复杂文字。
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();
}
}