vendredi 22 avril 2016

Docx4j - how to get a docx checkbox status

I am trying to read a stack of identically formatted word docx files, and extract the data to a database. I dont have any issues with the text, but I am struggling with the checkboxes. I need to say that I am new to docx4j, but have been struggling with this one for four days now. I would really value some assistance/help/advice.

I have attached a document (test.docx), that I am trying to read. The first checkbox, which I have inserted myself using Word, is detected by my code and appears on the initial pass as a CTSdtCell, but the other checkboxes are not. They seem to be represented in the file differently, by a CTObject, CTSHape, CTIMageData and a CTControl, and I cannot find a way of getting the checkbox from these or one of these.

public static void main(String[] args) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("test.docx"));      
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    Finder finder = new Finder(FldChar.class);
    new TraversalUtil(documentPart.getContent(), finder);
}

public static class Finder extends CallbackImpl {
    protected Class<?> typeToFind;
    protected Finder(Class<?> typeToFind) {
        this.typeToFind = typeToFind;
    }

    public List<Object> results = new ArrayList<Object>(); 

    @Override
    public List<Object> apply(Object o) {
        String txtVal="";
        System.out.println(o.getClass().getName());

        if (o instanceof org.docx4j.wml.CTSdtCell) {
            List<Object> objs = ((org.docx4j.wml.CTSdtCell)o).getSdtPr().getRPrOrAliasOrLock();
            findCheckbox(objs);
        }

        if (o instanceof org.docx4j.wml.SdtRun) {
            List<Object> objs = ((org.docx4j.wml.SdtRun)o).getSdtPr().getRPrOrAliasOrLock();
            findCheckbox(objs);
        }

        if (o instanceof org.docx4j.wml.SdtBlock) {
            List<Object> objs = ((org.docx4j.wml.SdtBlock)o).getSdtPr().getRPrOrAliasOrLock();
            findCheckbox(objs);
        }

        if (o instanceof org.docx4j.wml.Text) {
            System.out.println("      Text Value : "+((org.docx4j.wml.Text)o).getValue());
        }

        // Adapt as required
        if (o.getClass().equals(typeToFind)) {
            results.add(o);
        }
        return null;
    }

    private static void findCheckbox(List<Object> objs) {
        for (Object obj : objs) {
            if (obj instanceof javax.xml.bind.JAXBElement) {
                if (((javax.xml.bind.JAXBElement)obj).getDeclaredType().getName().equals("org.docx4j.w14.CTSdtCheckbox")) {
                    JAXBElement<CTSdtCheckbox> elem = ((javax.xml.bind.JAXBElement)obj);
                    org.docx4j.w14.CTSdtCheckbox cb = elem.getValue();
                    org.docx4j.w14.CTOnOff OnOff=cb.getChecked();
                    System.out.println("      CheckBox found with value="+OnOff.getVal());
                }
            }
        }
    }
}

The results are:

org.docx4j.wml.Tbl
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : WORK INSTRUCTION #
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Inline
org.docx4j.dml.CTBlip
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : A
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : STEP BY STEP
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : - 
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : WORK INSTRUCTION
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Inline
org.docx4j.dml.CTBlip
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : 1234567
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : TASK
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : Chlorine drum change
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : DATE
org.docx4j.wml.CTSdtCell
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : 12/07/2015
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : MACHINE
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : ORIGINATOR
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : D.GROVE
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CLOCK NUMBER
org.docx4j.wml.CTSdtCell
      CheckBox found with value=1
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : ?
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : AREA
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CHLORINE HOUSE
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CHECKED
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : (EXPERT)
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : J Clarke
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CLOCK NUMBER
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : 4985
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : PPE 
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Anchor
org.docx4j.dml.CTBlip
org.docx4j.dml.CTColorChangeEffect
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : EYE
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Anchor
org.docx4j.dml.CTBlip
org.docx4j.dml.CTColorChangeEffect
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : EAR
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Anchor
org.docx4j.dml.CTBlip
org.docx4j.dml.CTColorChangeEffect
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : FOOT
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Anchor
org.docx4j.dml.CTBlip
org.docx4j.dml.CTColorChangeEffect
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : HEAD
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Drawing
org.docx4j.dml.wordprocessingDrawing.Anchor
org.docx4j.dml.CTBlip
org.docx4j.dml.CTColorChangeEffect
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : HAND
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShapetype
org.docx4j.vml.CTStroke
org.docx4j.vml.CTFormulas
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTF
org.docx4j.vml.CTPath
org.docx4j.vml.officedrawing.CTLock
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : COSHH
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : SPECIAL PPE REQUIREMENTS
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : *SITE 
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : R/A NUMBER
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CONSIDERATION
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : PRODUCTS
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : B.A. EQUIPMENT
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : 12668
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.CTObject
org.docx4j.vml.CTShape
org.docx4j.vml.CTImageData
org.docx4j.wml.CTControl
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value : CHLORINE
org.docx4j.wml.R
org.docx4j.wml.Text
      Text Value :  GAS
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tr
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.Tc
org.docx4j.wml.P
org.docx4j.wml.P
org.docx4j.wml.CTBookmark
org.docx4j.wml.CTMarkupRange




Aucun commentaire:

Enregistrer un commentaire