Java 用文本替换内容控件
Java 用文本替换内容控件,java,docx4j,Java,Docx4j,有人知道我在哪里可以找到这方面的代码示例吗?我自己也在使用docx4j中的内容控件。如果您只想查看docx文件的xml,那么可以将.docx文件扩展名替换为.zip,实际上它就是.zip。然后进入zip文件,您将找到底层xml。我相信有一些应用程序可以让你编辑和保存docx-xml,但我还没有详细研究过 如果要在word模板中插入内容控件,首先需要打开“开发人员”选项卡。看见这将在“开发人员”选项卡下添加“控件”部分。见: 我还没有找到一个只使用XmlUtils.unmarshallFromTe
有人知道我在哪里可以找到这方面的代码示例吗?我自己也在使用docx4j中的内容控件。如果您只想查看docx文件的xml,那么可以将.docx文件扩展名替换为.zip,实际上它就是.zip。然后进入zip文件,您将找到底层xml。我相信有一些应用程序可以让你编辑和保存docx-xml,但我还没有详细研究过 如果要在word模板中插入内容控件,首先需要打开“开发人员”选项卡。看见这将在“开发人员”选项卡下添加“控件”部分。见: 我还没有找到一个只使用XmlUtils.unmarshallFromTemplate(xmlString,mappings)进行实际变量替换的好例子,它似乎有很多夸克。如果你完成了那部分,请发帖
private static void replaceTextValue(WordprocessingMLPackage模板、字符串名称、字符串占位符)
private static void replaceTextValue(WordprocessingMLPackage template, String name, String placeholder)
throws Exception {
List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), SdtBlock.class);
// System.out.println(template.getMainDocumentPart().getXML());
for (Object text : texts) {
SdtBlock textElement = (SdtBlock) text;
List<Object> cList = textElement.getSdtContent().getContent();
ClassFinder finder = new ClassFinder(Text.class);
new TraversalUtil(cList, finder);
for (Object o : finder.results) {
Object o2 = XmlUtils.unwrap(o);
if (o2 instanceof org.docx4j.wml.Text) {
String CTagVal = ((org.docx4j.wml.Text) o2).getValue();
if (CTagVal.equalsIgnoreCase(placeholder)) {
org.docx4j.wml.Text txt = (org.docx4j.wml.Text) o2;
txt.setValue(name);
}
} else {
System.out.println(XmlUtils.marshaltoString(o, true, true));
}
}
}
}
抛出异常{
List text=getAllegementFromObject(template.getMainDocumentPart(),SdtBlock.class);
//System.out.println(template.getMainDocumentPart().getXML());
用于(对象文本:文本){
SdtBlock textElement=(SdtBlock)文本;
List cList=textElement.getSdtContent().getContent();
ClassFinder=新的ClassFinder(Text.class);
新TraversalUtil(cList,finder);
for(对象o:finder.results){
对象o2=XmlUtils.unwrap(o);
if(o2 instanceof org.docx4j.wml.Text){
字符串CTagVal=((org.docx4j.wml.Text)o2.getValue();
if(CTagVal.equalsIgnoreCase(占位符)){
org.docx4j.wml.Text txt=(org.docx4j.wml.Text)o2;
txt.setValue(名称);
}
}否则{
System.out.println(XmlUtils.marshaltoString(o,true,true));
}
}
}
}
此方法替换控件中的文本谢谢…我只想用.docx文件中的给定值替换占位符文本