如何从十六进制颜色代码获取 CTColor?

huangapple go评论76阅读模式
英文:

How can I get a CTColor from a Hex color code?

问题

我正在使用Apache POI来修改pptx文件。我试图更改XSLFTheme类的主题颜色。

我想要更改所有可以在PowerPoint GUI中更改的颜色,就像在这里所描述的那样(“创建自己的颜色主题”):https://support.office.com/en-us/article/change-a-theme-and-make-it-the-default-in-word-or-excel-c846f997-968e-4daa-b2d4-42bd2afef904

我通过以下代码获取主题的颜色方案:

CTOfficeStyleSheet styleSheet = xslfTheme.getXmlObject();
CTBaseStyles themeElements = styleSheet.getThemeElements();
CTColorScheme colorscheme = themeElements.getClrScheme();

要更改CTColorScheme中的颜色,我需要一个CTColor作为输入。我如何从十六进制颜色代码获取CTColor?

谢谢您的帮助!

英文:

I am using Apache POI to modify a pptx. I am trying to change the theme colors of the XSLFTheme class.

I would like to change all the colors that one can change in the PowerPoint GUI as described here ("Create my own color theme"): https://support.office.com/en-us/article/change-a-theme-and-make-it-the-default-in-word-or-excel-c846f997-968e-4daa-b2d4-42bd2afef904

The color scheme of the theme I get with the following code:

CTOfficeStyleSheet styleSheet = xslfTheme.getXmlObject();
CTBaseStyles themeElements = styleSheet.getThemeElements();
CTColorScheme colorscheme = themeElements.getClrScheme();

For changing the colors in the CTColorScheme, I need a CTColor as input. How can I get a CTColor from a Hex color code?

Thanks for your help!

答案1

得分: 1

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;

import org.apache.commons.codec.binary.Hex;

public class PowerPointChangeThemeColors {

 enum ThemeColor {
  DK1, //Dark1
  LT1, //Light1
  DK2, //Dark2
  LT2, //Light2
  ACCENT1, //Accent1
  ACCENT2, //Accent2
  ACCENT3, //Accent3
  ACCENT4, //Accent4
  ACCENT5, //Accent5
  ACCENT6, //Accent6
  HLINK, //Hyperlink
  FOLHLINK //FollowedHyperlink
 }

 static void setThemeColor(XSLFTheme theme, ThemeColor color, String rgbS) throws Exception {
  CTOfficeStyleSheet styleSheet = theme.getXmlObject();
  CTBaseStyles themeElements = styleSheet.getThemeElements();
  CTColorScheme colorScheme = themeElements.getClrScheme();
  CTColor ctColor = CTColor.Factory.newInstance();
  CTSRgbColor sRgbColor = ctColor.addNewSrgbClr();
  byte[] rgbB = Hex.decodeHex(rgbS);
  sRgbColor.setVal(rgbB);
  if (color == ThemeColor.DK1) {
   colorScheme.setDk1(ctColor);
  } else if (color == ThemeColor.LT1) {
   colorScheme.setLt1(ctColor);
  } else if (color == ThemeColor.DK2) {
   colorScheme.setDk2(ctColor);
  } else if (color == ThemeColor.LT2) {
   colorScheme.setLt2(ctColor);
  } else if (color == ThemeColor.ACCENT1) {
   colorScheme.setAccent1(ctColor);
  } else if (color == ThemeColor.ACCENT2) {
   colorScheme.setAccent2(ctColor);
  } else if (color == ThemeColor.ACCENT3) {
   colorScheme.setAccent3(ctColor);
  } else if (color == ThemeColor.ACCENT4) {
   colorScheme.setAccent4(ctColor);
  } else if (color == ThemeColor.ACCENT5) {
   colorScheme.setAccent5(ctColor);
  } else if (color == ThemeColor.ACCENT6) {
   colorScheme.setAccent6(ctColor);
  } else if (color == ThemeColor.HLINK) {
   colorScheme.setHlink(ctColor);
  } else if (color == ThemeColor.FOLHLINK) {
   colorScheme.setFolHlink(ctColor);
  }
 }

 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();
   setThemeColor(theme, ThemeColor.DK1, "0000FF");
   setThemeColor(theme, ThemeColor.ACCENT1, "FF0000");
  }

  FileOutputStream out = new FileOutputStream("./PPTXNew.pptx");
  slideShow.write(out);
  out.close();
  slideShow.close();
 }
}
英文:

The CTColorScheme defines different CTColors: dk1, lt1, dk2, lt2, accent1 to accent6, hlink and folHlink. So at first you need to decide what color needs to be set.

I suspect you have a PowerPoint presentation using a theme already and wants changing some of the theme colors. If that shall be RGB colors then, you need overwriting them using CTColors containing CTSRgbColors. And CTSRgbColor has a method setVal​(byte[] val) where byte[] val is a byte array containing three bytes for RGB.

If the need is using a hex-string like "FF00FF" to define the color instead of using the byte array directly, then org.apache.commons.codec.binary.Hex could be used. That provides a method Hex.decodeHex which takes a string and decodes it into a byte array. The commons-codec library is a apache poi prerequisite anyway.

Example:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.apache.commons.codec.binary.Hex;
public class PowerPointChangeThemeColors {
enum ThemeColor {
DK1, //Dark1
LT1, //Light1
DK2, //Dark2
LT2, //Light2
ACCENT1, //Accent1
ACCENT2, //Accent2
ACCENT3, //Accent3
ACCENT4, //Accent4
ACCENT5, //Accent5
ACCENT6, //Accent6
HLINK, //Hyperlink
FOLHLINK //FollowedHyperlink
}
static void setThemeColor(XSLFTheme theme, ThemeColor color, String rgbS) throws Exception {
CTOfficeStyleSheet styleSheet = theme.getXmlObject();
CTBaseStyles themeElements = styleSheet.getThemeElements();
CTColorScheme colorScheme = themeElements.getClrScheme();
CTColor ctColor = CTColor.Factory.newInstance();
CTSRgbColor sRgbColor = ctColor.addNewSrgbClr();
byte[] rgbB = Hex.decodeHex(rgbS);
sRgbColor.setVal(rgbB);
if (color == ThemeColor.DK1) {
colorScheme.setDk1(ctColor);
} else if (color == ThemeColor.LT1) {
colorScheme.setLt1(ctColor);
} else if (color == ThemeColor.DK2) {
colorScheme.setDk2(ctColor);
} else if (color == ThemeColor.LT2) {
colorScheme.setLt2(ctColor);
} else if (color == ThemeColor.ACCENT1) {
colorScheme.setAccent1(ctColor);
} else if (color == ThemeColor.ACCENT2) {
colorScheme.setAccent2(ctColor);
} else if (color == ThemeColor.ACCENT3) {
colorScheme.setAccent3(ctColor);
} else if (color == ThemeColor.ACCENT4) {
colorScheme.setAccent4(ctColor);
} else if (color == ThemeColor.ACCENT5) {
colorScheme.setAccent5(ctColor);
} else if (color == ThemeColor.ACCENT6) {
colorScheme.setAccent6(ctColor);
} else if (color == ThemeColor.HLINK) {
colorScheme.setHlink(ctColor);
} else if (color == ThemeColor.FOLHLINK) {
colorScheme.setFolHlink(ctColor);
}
}
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();
setThemeColor(theme, ThemeColor.DK1, "0000FF");
setThemeColor(theme, ThemeColor.ACCENT1, "FF0000");
}
FileOutputStream out = new FileOutputStream("./PPTXNew.pptx");
slideShow.write(out);
out.close();
slideShow.close();
}
}

This gets a PPTX.pptx, gets it's theme and changes the theme colors dk1 (dark1), which is the default dark color used as font color for example, and accent1, which is the first accent color, into user defined RGB colors.

huangapple
  • 本文由 发表于 2020年5月4日 17:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61589371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定