2014-09-05 67 views
0

使用Docx4J,我將如何將一個字段代碼(如「INCLUDEPICTURE」)插入到文檔中?使用docx4java將字段代碼插入文檔

我需要爲INCLUDEPICTURE使用\ d開關。

更新:當我從互聯網插入圖像時,此代碼無法正常工作。 :-(

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); 

} 

當我unqip的.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="[email protected]@[email protected]@[email protected]@[email protected]@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> 
+0

你選擇所有,然後按下F9鍵更新域鏈接? – JasonPlutext 2014-09-06 03:10:00

+0

工作正常!有什麼辦法可以跳過全選&F9步驟?這些文檔最終將自動生成並由最終用戶使用,我們希望儘可能降低複雜性。 – 2014-09-06 15:07:17

+0

根據示例FieldINCLUDEPICTURE.java中的其他註釋,可以通過在字段分隔符(如上面的XML)後面添加w:pict(或w:drawing)來避免這種情況。查看docx4j ImageAdd示例以瞭解如何執行此操作。 – JasonPlutext 2014-09-06 21:36:42

回答

1

查看新示例FieldINCLUDEPICTURE.java,該示例顯示如何將其添加爲簡單字段或複雜字段。

我已經將\ d開關作爲讀者的一項大概的小練習。

請注意,除非您實際添加字段結果,否則在刷新字段之前圖片不會顯示在Word中。以編程方式將圖片添加到字段結果與addImage示例非常相似。

+0

非常感謝您的快速更新...... !!!!我現在正在檢查這個!我真的很感激你在不到1個小時的時間裏更新你的用例源代碼? :-) – 2014-09-06 01:11:58

+0

你好賈森, 這似乎並沒有與位於互聯網上的文件。我可能會做錯什麼?在下面粘貼我的代碼。 – 2014-09-06 01:27:04

+0

在問題中插入代碼 – 2014-09-06 01:30:20

0

我也試圖以編程方式插入圖像遠程鏈接,我沒能達到預期的效果,而無需手動更新於2010年字

public class FieldINCLUDEPICTURE { 

    public static void main(String[] args) throws Exception { 
    boolean save = true; 

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); 
    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:"); 

    String url = "http://www.springsoftware.co.uk/spring/images/sampledata/itcg.png"; 

     P p = new P(); 
//  p.getContent().add(
//    createSimpleField(" INCLUDEPICTURE \"file:///C:/Users/jharrop/git/plutext/docx4j/src/test/resources/images/greentick.png\" \\* MERGEFORMAT ") 
    //   ); 
//  mdp.getContent().add(p); 
    mdp.createParagraphOfText("complex field:"); 

    //addComplexField(p, "INCLUDEPICTURE \"" + url + "\"" + " \\* MERGEFORMAT \\d"); 
    addComplexField(p, " INCLUDEPICTURE \"" + url + "\" \\* MERGEFORMAT \\d"); 

    mdp.getContent().add(p); 

    //addComplexField(p, " INCLUDEPICTURE \"" + attachmentRequestURL + "\"" + " \\* MERGEFORMAT \\d"); 

    // Pretty print the main document part 
    System.out.println(
      XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true)); 

    String filenameHint = null; 
    String altText = null; 
    int id1 = 0; 
    int id2 = 1; 

    // Image 1: no width specified 
    P p1 = newImage(wordMLPackage, url, filenameHint, altText, id1, id2); 

    addComplexField(p1,url); 

    // Optionally save it 
    if (save) { 
     String filename = System.getProperty("user.dir") + "/OUT_FieldINCLUDEPICTURE.docx"; 
     wordMLPackage.save(new java.io.File(filename)); 
     System.out.println("Saved " + filename); 
    } 

} 

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); 

} 


/** 
* Create image, without specifying width 
*/ 
public static P newImage(WordprocessingMLPackage wordMLPackage, String url, String filenameHint, String altText, int id1, int id2) throws Exception { 

    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createLinkedImagePart(wordMLPackage, new URL(url)); 

    Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, true); 

    // Now add the inline in w:p/w:r/w:drawing 
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); 
    P p = factory.createP(); 
    R run = factory.createR(); 
    p.getContent().add(run); 
    org.docx4j.wml.Drawing drawing = factory.createDrawing(); 
    run.getContent().add(drawing); 
    drawing.getAnchorOrInline().add(inline); 

    return p; 

} 

}