Pregunta

I'm building a small java program to test all the "document related" features i need to cover with docx4j to fulfill my project requirements. In abstract: being able to create templates with "content controls" that will be offered as "raw template" to the users. Let the user enhance them, to finally populate them with data. I'm dealing with "docx4j" because of the "repeating and conditional" features.

This has been my progress, programmatically:

1) Create a blank document (DONE)

2) Add a custom xml part with the xml data (the part3.xml in the invoice example). (DONE)

3) Add a custom xml part with the xml xpaths reference (the part4.xml in the invoice example). (DONE)

4) Add the content control (PENDING)

It has to be something similar to this extract from the invoice example:

<w:sdt>
  <w:sdtPr>
    <w:alias w:val="Customer name" />
    <w:tag w:val="od:xpath=x1&customer name=customer name" />
    <w:id w:val="1418037941" />
    <w:placeholder>
      <w:docPart w:val="DefaultPlaceholder_22675703" />
    </w:placeholder>
    <w:dataBinding w:xpath="/invoice[1]/customer[1]/name[1]" w:storeItemID="{8B049945-9DFE-4726-9DE9-CF5691E53858}" />
    <w:text />
  </w:sdtPr>
  <w:sdtContent>
    <w:p>
      <w:r>
        <w:t>Joe Bloggs</w:t>
     </w:r>
    </w:p>
  </w:sdtContent>
</w:sdt>

And here is my question: How to generate this xml ? Can be done by Java code or only from Word? What are and how are generated the "1418037941" and the "DefaultPlaceholder_22675703" values? I guess i cannot "invent" them, and are referenced somewhere else. I was looking for some method in MainDocumentPart class to do so but was unable to find it. Am i misunderstandig something?

The other steps are almost ok for me:

5) Perform "preprocess" (DONE)

6) Perform "process" (DONE)

7) Perform "strip" (DONE, except removing repat tags)

Thank you very much for your help! David

¿Fue útil?

Solución

You can certainly create a content control programmatically using docx4j. That said, the usual approach for creating a "template" is to use an authoring tool, such as this one.

To answer your specific questions:-

  • You can leave out the w:placeholder.

  • The spec says w:id is type xsd:int, being an integer between -2147483648 and 2147483647. You can generate it randomly, or you may be able to leave it out as well.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top