Search code examples
xmlshapesdocx4j

How to read an MS word xml file in Docx4j


I have questions.

I want to create MS word file with Java.

I can make simple word file.

but I don't know how to do the coding to create a word with complex shapes.

Using the xml code, in my opinion it seems to be able to make.

How to use raw xml code with docx4j?

I have used below code style. but it's too hard

static String tblXML = "<w:tbl xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
         "xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
         "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
         "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
         +"<w:tblPr>"
         +"<w:tblStyle w:val=\"a5\" />"
         +"<w:tblW w:w=\"0\" w:type=\"auto\" />"
         +"<w:tblLook w:val=\"04A0\" w:firstRow=\"1\" w:lastRow=\"0\" w:firstColumn=\"1\" w:lastColumn=\"0\" w:noHBand=\"0\" w:noVBand=\"1\" />"
      +"</w:tblPr>"
      +"<w:tblGrid>"
         +"<w:gridCol w:w=\"9224\" />"
      +"</w:tblGrid>"
      +"<w:tr w:rsidR=\"00F327EA\" w:rsidTr=\"0047463F\">"
         +"<w:trPr>"
            +"<w:trHeight w:val=\"1912\" />"
         +"</w:trPr>"
         +"<w:tc>"
            +"<w:tcPr>"
               +"<w:tcW w:w=\"9224\" w:type=\"dxa\" />"
            +"</w:tcPr>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Plan No. </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Inspection Type Special , Regular , Specially assigned inspector , , , </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t>Planning Period 2015</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>06</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>17 ~ 2015</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>06</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t xml:space=\"preserve\">18 </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Individual Target Aerodrome (AD), Major International Airport, </w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>GimHae</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\"> </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Inspector Primary: </w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>superadmin</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\"> </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:pPr>"
                  +"<w:ind w:firstLineChars=\"850\" w:firstLine=\"1700\" />"
               +"</w:pPr>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>Secondary:superadmin</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
            +"</w:p>"
         +"</w:tc>"
      +"</w:tr>"
   +"</w:tbl>";


Solution

  • You can unmarshal a string like that easily enough.

    The easiest way to create more complex code is to use the docx4j code generator. You feed it a sample docx, and it generates suitable code (take your pick between String form, and using the JAXB API).

    The code generator is available online, or as a Word Add-In.