Page 1 of 1

"Print" Fields. Is it possible to create them?

PostPosted: Tue Aug 21, 2012 11:04 am
by jallen
Hi Jason,

I'm hoping maybe you can point me in the right direction. Our client uses an offsite printing service that reads printer codes out of old style "Print" fields. I have to create new print fields as I am building the batched word doc using MergeDocx. I can't see how to create a new print field in docx4j. My main issue is that I can't find any class for instrText. I believe if I had that I could make it work.

Here is an example of the xml:
Code: Select all
<w:p>
  <w:r>
    <w:fldChar w:fldCharType="begin"/>
  </w:r>

  <w:r>
    <w:instrText xml:space="preserve"> print 027"&amp;p14X@@@FORM=BAN000 </w:instrText>
  </w:r>

  <w:r>
    <w:fldChar w:fldCharType="end"/>
  </w:r>
</w:p>


printcodefield.png
printcodefield.png (19.61 KiB) Viewed 1653 times

Re: "Print" Fields. Is it possible to create them?

PostPosted: Tue Aug 21, 2012 1:08 pm
by jason
In org.docx4j.wml.ObjectFactory, the method you want to use is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    @XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "instrText", scope = R.class)
    public JAXBElement<Text> createRInstrText(Text value) {
        return new JAXBElement<Text>(_RInstrText_QNAME, Text.class, R.class, value);
    }
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


or you should be able to unmarshall a string of XML representing the entire paragraph using XmlUtils.unmarshalString (don't forget to add a declaration for xmlns:w to it).

Re: "Print" Fields. Is it possible to create them?

PostPosted: Thu Aug 30, 2012 7:24 am
by jallen
For anyone intersted.

Code: Select all
private void addPrintField(org.docx4j.wml.P par, String printCode) {
      
      try {
      
         org.docx4j.wml.R run;
         FldChar fld;

         // Create Field Begin
         run = objectFactory.createR();
         fld = objectFactory.createFldChar();
         fld.setFldCharType(STFldCharType.BEGIN);
         run.getContent().add(fld);
         par.getContent().add(0,run);   

         // Create Instr Text
         run = objectFactory.createR();
         Text text = objectFactory.createText();
         text.setValue(printCode);
         JAXBElement<Text> instr = objectFactory.createRInstrText(text);
         run.getContent().add(instr);
         par.getContent().add(1,run); 

         // Create Field End
         run = objectFactory.createR();
         fld = objectFactory.createFldChar();
         fld.setFldCharType(STFldCharType.END);
         run.getContent().add(fld);
         par.getContent().add(2,run); 

      } catch (Exception e) {
         log.error("addPrintField", true, e);
      }

   }