使用docx4java将字段代码插入到文档中

时间:2014-09-05 23:54:11

标签: java ms-word docx docx4j

使用Docx4J,如何在文档中插入“INCLUDEPICTURE”等字段代码?

我需要使用\ d开关进行INCLUDEPICTURE。

更新:当我从互联网上插入图像时,此代码无效。 : - (

    boolean save = true; 

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();

    // Example 1: add text in Title style
    mdp.addStyledParagraphOfText("Title", "Select all, then hit F9 in Word to see your pictures, or programmatically add them first");

    mdp.createParagraphOfText("simple field:");

    P p = new P(); 
    p.getContent().add( 
            createSimpleField( " INCLUDEPICTURE  \"http://placehold.it/312x322\"  \\* MERGEFORMAT ")
            );
    mdp.getContent().add(p);

    mdp.createParagraphOfText("complex field:");

    p = new P(); 
    addComplexField(p, " INCLUDEPICTURE  \"http://placehold.it/312x322\"  \\* MERGEFORMAT ");
    mdp.getContent().add(p);

    wordMLPackage.save(file);


private static CTSimpleField createSimpleField(String val) {

    CTSimpleField field = new CTSimpleField();
    field.setInstr(val);
    return field;
}

private static void addComplexField(P p, String instrText) {

    org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();

    // Create object for r
    R r = wmlObjectFactory.createR(); 
    p.getContent().add( r); 
        // Create object for fldChar (wrapped in JAXBElement) 
        FldChar fldchar = wmlObjectFactory.createFldChar(); 
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
        // Create object for instrText (wrapped in JAXBElement) 
        Text text = wmlObjectFactory.createText(); 
        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text); 
        r.getContent().add( textWrapped); 
            text.setValue( instrText); 
            text.setSpace( "preserve");     

        // Create object for fldChar (wrapped in JAXBElement) 
        fldchar = wmlObjectFactory.createFldChar(); 
        fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);

}

当我取消.docx并打开document.xml时,我发现这是:

<w:p>
  <w:pPr>
    <w:pStyle w:val="Title"/>
  </w:pPr>
  <w:r>
    <w:t>Select all, then hit F9 in Word to see your pictures, or programmatically add them first</w:t>
  </w:r>
</w:p>
<w:p>
  <w:fldSimple w:instr=" INCLUDEPICTURE  &quot;http://placehold.it/312x322&quot;  \* MERGEFORMAT "/>
</w:p>
<w:p>
  <w:r>
    <w:fldChar w:fldCharType="begin"/>
    <w:instrText xml:space="preserve">INCLUDEPICTURE  &quot;http://placehold.it/312x322&quot;  \* MERGEFORMAT </w:instrText>
    <w:fldChar w:fldCharType="end"/>
  </w:r>
</w:p>

但是,如果我使用互联网网址,则不会在文档中显示图像。

我尝试使用MS Word插入域代码,这是我得到的代码。这恰当地显示为图像。

<w:p w14:paraId="43d51bd" w14:textId="43d51bd" w:rsidR="006549FC" w:rsidRDefault="00581431">
  <w:pPr>
    <w15:collapsed w:val="false"/>
  </w:pPr>
  <w:r>
    <w:fldChar w:fldCharType="begin"/>
  </w:r>
  <w:r>
    <w:instrText xml:space="preserve">INCLUDEPICTURE &quot;http://placehold.it/100x100&quot; \* MERGEFORMAT </w:instrText>
  </w:r>
  <w:r>
    <w:fldChar w:fldCharType="separate"/>
  </w:r>
  <w:r>
    <w:pict>
      <v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75.0" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
        <v:stroke joinstyle="miter"/>
        <v:formulas>
          <v:f eqn="if lineDrawn pixelLineWidth 0"/>
          <v:f eqn="sum @0 1 0"/>
          <v:f eqn="sum 0 0 @1"/>
          <v:f eqn="prod @2 1 2"/>
          <v:f eqn="prod @3 21600 pixelWidth"/>
          <v:f eqn="prod @3 21600 pixelHeight"/>
          <v:f eqn="sum @0 0 1"/>
          <v:f eqn="prod @6 1 2"/>
          <v:f eqn="prod @7 21600 pixelWidth"/>
          <v:f eqn="sum @8 21600 0"/>
          <v:f eqn="prod @7 21600 pixelHeight"/>
          <v:f eqn="sum @10 21600 0"/>
        </v:formulas>
        <v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f"/>
        <o:lock aspectratio="t" v:ext="edit"/>
      </v:shapetype>
      <v:shape id="_x0000_i1025" style="width:100pt;height:100pt" type="#_x0000_t75">
        <v:imagedata r:href="rId6" r:id="rId5"/>
      </v:shape>
    </w:pict>
  </w:r>
  <w:r>
    <w:fldChar w:fldCharType="end"/>
  </w:r>
  <w:bookmarkEnd w:id="0"/>
</w:p>

1 个答案:

答案 0 :(得分:1)

请参阅新示例FieldINCLUDEPICTURE.java,其中介绍了如何将其添加为简单字段或复杂字段。

我把\ d开关作为一个可能是读者的微不足道的练习。

请注意,除非您实际添加字段结果,否则在刷新字段之前,图片不会显示在Word中。以编程方式将图片添加到字段结果与addImage示例非常相似。