2

I'm using docx4j to create Word documents using a Word template. The template contents control which in the document are filled with text by my Java code. The problem is that the formatting I have added to some of the controls has no effect. I have tried formatting with both text content controls and rich text content controls. In fact, the entire document appears grey (including the image in the document header) so I'm not sure that the problem is specific to docx4j. Here is my code:

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

                }

        }           

    }
}
}

Here is the XML of a Content Control

<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>
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • I've made a start on an answer, but it is hard to be more specific given that your code does not show how you format the content control contents. Better yet, add an example of the resulting XML to your question. – JasonPlutext Feb 16 '15 at 09:38
  • Thanks, I have added the XML of one of the Content Controls –  Feb 16 '15 at 10:40
  • That does meet the constraints described at http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/text.html but is a little different to what Word emits (see answer below). Also, check your style Platzhaltertext is actually defined in your styles part? – JasonPlutext Feb 16 '15 at 12:02
  • The XML I added is actually from a Content Control which I added directly to the Word document. I am not trying to add a Content Control programatically. What I am trying to do, is modify an existing content control. Can you tell me how I can add an rPr to an existing Content Control? I would also like to know how to delete an existing Content Control? Thanks –  Feb 16 '15 at 12:45
  • Now I'm not sure what you are really asking. Does the Platzhaltertext style work or not? Regarding deleting the content control (which belongs in a separate question), do you want to keep the content or not? – JasonPlutext Feb 16 '15 at 19:16
  • Yes, the style does work. I want to be able to remove a Content Control AND any content it contains. –  Feb 17 '15 at 09:05
  • 1
    Since you want to delete its contents, you delete the content control the same way to delete a P element, as at http://stackoverflow.com/questions/28026290/docx4j-delete-wml-p-element – JasonPlutext Feb 17 '15 at 11:04
  • Thanks, that works great. I've solved the formatting issue by changing settings in Word itself. Thanks again for the great help. –  Feb 17 '15 at 12:04
  • Could you please give your getAllSdtElementFromObject() as well please, I'm having a really hard time here :( thx in advance – Igor Beaufils Aug 03 '15 at 15:39

1 Answers1

1

A plain text content control can be formatted using run (rPr) formatting; see http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rPr_5.html

For example, Word emits:

    <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>

A rich text control's contents can also include pPr formatting (on any P's it is allowed to contain given its context).

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84