【问题标题】:Change font in pptx slide master in Apache POI在 Apache POI 中更改 pptx 幻灯片母版中的字体
【发布时间】:2020-04-30 15:46:58
【问题描述】:

我正在使用 Apache POI 来修改 pptx。我想一次更改整个 pptx 的字体。

我知道在 Powerpoint 中这可以通过更改主题的字体来实现(如果幻灯片都使用这些字体),但我无法通过 Apache POI 使其工作。

到目前为止,我发现我可以使用例如设置单个 XSLFTextRun 的字体系列。 run.setFontFamily(HSSFFont.FONT_ARIAL)

编辑:我发现 XSLFSlideMaster 的 XSLFTheme 类确实有一个 getMajorFont() 和一个 getMinorFont() 方法。我认为这些可能是我需要更改的字体,但这些字段没有设置器。

感谢您的帮助!

【问题讨论】:

  • 最佳实践是将所有字体实例键入字体主题。然后你只需要更新主题来改变格式。
  • @JohnKorchok 感谢您的回答。在我尝试修改的演示文稿中,我使用的是我创建的幻灯片母版中的字体。因此,在 PowerPoint 中,我可以通过更改幻灯片母版中的字体来一次修改整个演示文稿的字体。现在我正在尝试对 Apache POI 做同样的事情。
  • 不清楚到底是什么需求。您可以使用XMLSlideShow.getSlideMasters 从幻灯片中获取幻灯片母版。但是每个master定义了多个布局。那么是否需要更改所有布局中所有占位符的所有字体设置?你到底在 PowrPoints GUI 做什么来实现你想要的帽子?
  • @AxelRichter 我编辑了这个问题(希望)让它更清楚。

标签: java apache-poi powerpoint


【解决方案1】:

如果您的尝试与Change the default font in PowerPoint 相同,则这会更改所用主题中字体方案的主要字体和次要字体。

您可以从幻灯片的每张幻灯片中获取XSLFTheme,也可以从母版中获取。但是正如您发现的那样,主要字体和次要字体已经有 getter 但没有 setter。所以我们需要使用低级别的ooxml-schemas 类。

以下代码示例提供了setMajorFontsetMinorFont 方法,用于为拉丁文、东亚文或复杂文体设置主题中主要字体和次要字体的字体。

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

【讨论】:

  • 这就是我想要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多