如何用Java替换Powerpoint文件中的文本
我需要在运行时替换Powerpoint文件中的一些文本。Powerpoint文件被用作带有一些占位符/托克的模板,例如{{USER_NAME} 我尝试过使用POI,但没有成功。 我参考了论坛上的其他链接,从“docx4j”开始,但我无法超越一点,而且文档不是很清楚,至少对我来说是这样。 以下是我迄今为止所做的工作: 已将PPTX加载到“PresentationLPackage” 使用MainPresentationPart.getSliden获取“MainPresentationPart”和幻灯片 但我不确定接下来的步骤,或者这是否是正确的方法 如有任何建议,将不胜感激 非常感谢, -维尼 JaxbXmlPart包含:如何用Java替换Powerpoint文件中的文本,java,powerpoint,docx4j,Java,Powerpoint,Docx4j,我需要在运行时替换Powerpoint文件中的一些文本。Powerpoint文件被用作带有一些占位符/托克的模板,例如{{USER_NAME} 我尝试过使用POI,但没有成功。 我参考了论坛上的其他链接,从“docx4j”开始,但我无法超越一点,而且文档不是很清楚,至少对我来说是这样。 以下是我迄今为止所做的工作: 已将PPTX加载到“PresentationLPackage” 使用MainPresentationPart.getSliden获取“MainPresentationPart”和幻灯
/**
* unmarshallFromTemplate. Where jaxbElement has not been
* unmarshalled yet, this is more efficient (3 times
* faster, in some testing) than calling
* XmlUtils.marshaltoString directly, since it avoids
* some JAXB processing.
*
* @param mappings
* @throws JAXBException
* @throws Docx4JException
*
* @since 3.0.0
*/
public void variableReplace(java.util.HashMap<String, String> mappings) throws JAXBException, Docx4JException {
// Get the contents as a string
String wmlTemplateString = null;
if (jaxbElement==null) {
PartStore partStore = this.getPackage().getSourcePartStore();
String name = this.getPartName().getName();
InputStream is = partStore.loadPart(
name.substring(1));
if (is==null) {
log.warn(name + " missing from part store");
throw new Docx4JException(name + " missing from part store");
} else {
log.info("Lazily unmarshalling " + name);
// This seems to be about 5% faster than the Scanner approach
try {
wmlTemplateString = IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
throw new Docx4JException(e.getMessage(), e);
}
}
} else {
wmlTemplateString = XmlUtils.marshaltoString(jaxbElement, true, false, jc);
}
// Do the replacement
jaxbElement = (E)XmlUtils.unwrap(
XmlUtils.unmarshallFromTemplate(wmlTemplateString, mappings, jc));
}
因此,一旦您有了幻灯片部件,就可以在其上调用variableReplace。您需要变量采用XmlUtils.unmarshallFromTemplate所期望的格式
/**
* unmarshallFromTemplate. Where jaxbElement has not been
* unmarshalled yet, this is more efficient (3 times
* faster, in some testing) than calling
* XmlUtils.marshaltoString directly, since it avoids
* some JAXB processing.
*
* @param mappings
* @throws JAXBException
* @throws Docx4JException
*
* @since 3.0.0
*/
public void variableReplace(java.util.HashMap<String, String> mappings) throws JAXBException, Docx4JException {
// Get the contents as a string
String wmlTemplateString = null;
if (jaxbElement==null) {
PartStore partStore = this.getPackage().getSourcePartStore();
String name = this.getPartName().getName();
InputStream is = partStore.loadPart(
name.substring(1));
if (is==null) {
log.warn(name + " missing from part store");
throw new Docx4JException(name + " missing from part store");
} else {
log.info("Lazily unmarshalling " + name);
// This seems to be about 5% faster than the Scanner approach
try {
wmlTemplateString = IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
throw new Docx4JException(e.getMessage(), e);
}
}
} else {
wmlTemplateString = XmlUtils.marshaltoString(jaxbElement, true, false, jc);
}
// Do the replacement
jaxbElement = (E)XmlUtils.unwrap(
XmlUtils.unmarshallFromTemplate(wmlTemplateString, mappings, jc));
}