简体   繁体   English

Docx4j - 如何获取docx复选框状态

[英]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. 我试图读取一堆相同格式的word docx文件,并将数据提取到数据库。 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. 我需要说我是docx4j的新手,但现在已经四天苦苦挣扎。 I would really value some assistance/help/advice. 我非常重视一些帮助/帮助/建议。

I have attached a document ( test.docx ), that I am trying to read. 我附上了一份文件( test.docx ),我正在尝试阅读。 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. 我使用Word自己插入的第一个复选框由我的代码检测到,并作为CTSdtCell出现在初始传递中,但其他复选框则不是。 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. 它们似乎在文件中以不同的方式表示,CTObject,CTSHape,CTIMageData和CTControl,我找不到从这些或其中一个获取复选框的方法。

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

I have now added the output from a MainDocumentPart.getXML() for the cell containing one of the elusive checkboxes. 我现在已经为包含一个难以捉摸的复选框的单元格添加了MainDocumentPart.getXML()的输出。 I can see nothing there to tell me the value. 我什么都看不到告诉我的价值。 Can anyone tell me what I am missing please? 有人能告诉我我想念的是什么吗?

<w:tc>
        <w:tcPr>
            <w:tcW w:w="1015" w:type="dxa"/>
            <w:tcBorders>
                <w:left w:val="single" w:color="auto" w:sz="24" w:space="0"/>
                <w:bottom w:val="single" w:color="auto" w:sz="24" w:space="0"/>
                <w:right w:val="single" w:color="auto" w:sz="24" w:space="0"/>
            </w:tcBorders>
            <w:vAlign w:val="center"/>
        </w:tcPr>
        <w:p w:rsidRPr="00A7008C" w:rsidR="00F909A4" w:rsidP="00017AE9" w:rsidRDefault="000F5760">
            <w:pPr>
                <w:jc w:val="center"/>
                <w:rPr>
                    <w:b/>
                    <w:color w:val="FFFFFF" w:themeColor="background1"/>
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:b/>
                    <w:color w:val="FFFFFF" w:themeColor="background1"/>
                    <w:sz w:val="36"/>
                </w:rPr>
                <w:object w:dxaOrig="225" w:dyaOrig="225">
                    <v:shape type="#_x0000_t75" style="width:12pt;height:29.25pt" id="_x0000_i1063" o:ole="">
                        <v:imagedata o:title="" r:id="rId17"/>
                    </v:shape>
                    <w:control w:name="CheckBox11" w:shapeid="_x0000_i1063" r:id="rId18"/>
                </w:object>
            </w:r>
            <w:bookmarkEnd w:id="0"/>
        </w:p>
    </w:tc>

I have cracked it!! 我破解了!! The CTImageData's point to images which can be accessed via the document's relationships. CTImageData指向可以通过文档关系访问的图像。 These images contain the ticked or unticked boxes. 这些图像包含勾选或未勾选的框。 By checking the size of the images I can tell which it is. 通过检查图像的大小,我可以知道它是什么。

I do not understand Word more than for superficial use, and do not know how these 'checkboxes' were created, but it seems they were not created the same way as my test ones. 我不理解Word而不是表面使用,并且不知道这些“复选框”是如何创建的,但似乎它们的创建方式与我的测试方式不同。 I therefore do not know whether these images may change if/when the organisation upgrades its MS Office software, edits and saves the docs files again. 因此,如果/当组织升级其MS Office软件,再次编辑和保存文档文件时,我不知道这些图像是否会发生变化。 However the need for my software will change quickly after initial load and so the implication of this risk is small for me. 但是,我的软件需要在初始加载后迅速改变,因此这种风险对我来说意义不大。

The existing checkboxes are legacy ActiveX controls: 现有的复选框是旧版ActiveX控件:

          <w:object w:dxaOrig="225" w:dyaOrig="225">
            <v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
              <v:stroke joinstyle="miter"/>
              <v:formulas>
                :
              </v:formulas>
              <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
              <o:lock v:ext="edit" aspectratio="t"/>
            </v:shapetype>
            <v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:12pt;height:29.25pt" o:ole="">
              <v:imagedata r:id="rId15" o:title=""/>
            </v:shape>
            <w:control r:id="rId16" w:name="CheckBox" w:shapeid="_x0000_i1025"/>
          </w:object>

The ones you are creating are modern XML-friendly checkbox content controls. 您正在创建的是现代XML友好的复选框内容控件。

There are also checkbox characters, and checkbox form fields... 还有复选框字符和复选框表单字段......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM