Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Word、docx4j和内容控制文本格式_Java_Ms Word_Docx4j_Word Contentcontrol - Fatal编程技术网

Java Word、docx4j和内容控制文本格式

Java Word、docx4j和内容控制文本格式,java,ms-word,docx4j,word-contentcontrol,Java,Ms Word,Docx4j,Word Contentcontrol,我正在使用docx4j使用Word模板创建Word文档。模板内容控制文档中由Java代码填充的文本。问题是,我添加到某些控件的格式设置无效。我尝试过使用文本内容控件和富文本内容控件进行格式化。事实上,整个文档显示为灰色(包括文档标题中的图像),因此我不确定问题是否特定于docx4j。这是我的密码: private void replaceTextValue(WordprocessingMLPackage template, String name, String placeholder ) th

我正在使用docx4j使用Word模板创建Word文档。模板内容控制文档中由Java代码填充的文本。问题是,我添加到某些控件的格式设置无效。我尝试过使用文本内容控件和富文本内容控件进行格式化。事实上,整个文档显示为灰色(包括文档标题中的图像),因此我不确定问题是否特定于docx4j。这是我的密码:

private void replaceTextValue(WordprocessingMLPackage template, String name, String placeholder ) throws Exception{     


    List<Object> texts = getAllSdtElementFromObject(template.getMainDocumentPart());

    for (Object text : texts) {         

        SdtElement textElement = (SdtElement) text; // SdtElement is an Interface, not a Class

        List<Object> cList = textElement.getSdtContent().getContent();

        SdtPr pr = textElement.getSdtPr();
        List<Object> al = pr.getRPrOrAliasOrLock();

        for (Object alias : al) {   // go through all SdtPr objects

            if ( alias.getClass().toString().contains("org.docx4j.wml.Tag")) {

                String CTagVal = ((org.docx4j.wml.Tag) alias).getVal();

                if (CTagVal.equalsIgnoreCase(placeholder))  {

                ClassFinder finder = new ClassFinder(Text.class); 
                new TraversalUtil(cList, finder);

                // taken from the TraveseFind example
                // https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/TraverseFind.java
                for (Object o : finder.results) {
                    Object o2 = XmlUtils.unwrap(o);
                    if (o2 instanceof org.docx4j.wml.Text) {
                    org.docx4j.wml.Text txt = (org.docx4j.wml.Text)o2;
                    txt.setValue(name);
                    } else {
                    System.out.println( XmlUtils.marshaltoString(o, true, true));
                    }
                    }

                }

        }           

    }
}
}
private void replaceTextValue(WordprocessingMLPackage模板、字符串名称、字符串占位符)引发异常{
列表文本=getAllSdtElementFromObject(template.getMainDocumentPart());
对于(对象文本:文本){
SdtElement textElement=(SdtElement)text;//SdtElement是接口,不是类
List cList=textElement.getSdtContent().getContent();
SdtPr pr=textElement.getSdtPr();
List al=pr.getRPrOrAliasOrLock();
对于(对象别名:al){//遍历所有SdtPr对象
if(alias.getClass().toString().contains(“org.docx4j.wml.Tag”)){
字符串CTagVal=((org.docx4j.wml.Tag)别名).getVal();
if(CTagVal.equalsIgnoreCase(占位符)){
ClassFinder=新的ClassFinder(Text.class);
新TraversalUtil(cList,finder);
//取自TraveseFind示例
// https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/TraverseFind.java
for(对象o:finder.results){
对象o2=XmlUtils.unwrap(o);
if(o2 instanceof org.docx4j.wml.Text){
org.docx4j.wml.Text txt=(org.docx4j.wml.Text)o2;
txt.setValue(名称);
}否则{
System.out.println(XmlUtils.marshaltoString(o,true,true));
}
}
}
}           
}
}
}
下面是一个内容控件的XML

<w:sdt>
<w:sdtPr>
<w:alias w:val="Aufgabengebiet"/>
<w:id w:val="-996718060"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1082065158"/>
</w:placeholder>
<w:showingPlcHdr/>
<w:text/>
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidRDefault="00A858B9" w:rsidR="00066661" w:rsidP="00A858B9">
<w:r w:rsidRPr="00FD7E66">
<w:rPr>
<w:rStyle w:val="Platzhaltertext"/>
</w:rPr>
<w:t>Klicken Sie hier, um Text einzugeben.</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>

克莱肯,请你,嗯,请给我发短信。

可以使用run(rPr)格式设置纯文本内容控件的格式;看

例如,Word发出:

    <w:sdt>
  <w:sdtPr>
    <w:rPr>
      <w:rStyle w:val="IntenseEmphasis"/>
    </w:rPr>
    <w:id w:val="-2141179504"/>
    <w:placeholder>
      <w:docPart w:val="DefaultPlaceholder_1082065158"/>
    </w:placeholder>
    <w:text/>
  </w:sdtPr>
  <w:sdtContent>
    <w:p >
      <w:pPr>
        <w:rPr>
          <w:rStyle w:val="IntenseEmphasis"/>
        </w:rPr>
      </w:pPr>
      <w:r w:rsidRPr="00B61E2E">
        <w:rPr>
          <w:rStyle w:val="IntenseEmphasis"/>
        </w:rPr>
        <w:t>Klicken Sie hier, um Text einzugeben.</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:sdt>

克莱肯,请你,嗯,请给我发短信。

富文本控件的内容还可以包括pPr格式(在给定上下文的任何p上,它都可以包含)

我已经开始回答这个问题了,但是由于您的代码没有显示如何格式化内容控件内容,所以很难说得更具体。更好的是,在您的问题中添加一个结果XML的示例。谢谢,我添加了一个内容控件的XML,它确实满足中描述的约束条件,但与Word发出的内容略有不同(请参见下面的答案)。另外,请检查您的样式平台是否在样式部分中定义了文本?我添加的XML实际上来自一个直接添加到Word文档的内容控件。我并没有试图以编程方式添加内容控件。我试图做的是修改现有的内容控件。您能告诉我如何将rPr添加到现有的内容控件中吗?我也想知道如何删除现有的内容控件?感谢您要删除其内容后,可以使用与删除P元素相同的方法删除内容控件,如中所示