2014-09-05 2 views
0

Docx4J를 사용하면 "INCLUDEPICTURE"와 같은 필드 코드를 문서에 어떻게 삽입 할 수 있습니까?docx4java를 사용하여 문서에 필드 코드 삽입

INCLUDEPICTURE에 \ d 스위치를 사용해야합니다.

업데이트 : 인터넷에서 이미지를 삽입 할 때이 코드가 작동하지 않습니다.

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

그러나,이 문서에 이미지로 표시되지 않습니다

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을 unqip하고 document.xml을 열어

, 여기에 내가 찾을 것입니다 :-(내가 인터넷 URL을 사용하는 경우.

내가 MS 워드를 사용하여 필드 코드를 삽입했는데, 이것은 내가 가진 코드입니다.이 제대로 이미지로 표시됩니다.

<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를 누르 선택 했 워드 2010에 대한 링크를 업데이트하지 않고 원하는 결과를 달성하는 데 실패입니까? – JasonPlutext

+0

그게 효과가! Select All & F9 Step을 건너 뛸 방법이 있습니까? 문서는 궁극적으로 자동 생성되어 최종 사용자가 사용하게되며 가능한 한 복잡성을 줄이고 자합니다. –

+0

샘플 FieldINCLUDEPICTURE.java의 다른 주석에 따라 필드 구분 기호 뒤에 w : pict (또는 w : 도면)를 추가하여 피할 수 있습니다 (위에서 설명한 XML과 유사). 이를 수행하는 방법은 docx4j ImageAdd 샘플을보십시오. – JasonPlutext

답변

1

새 샘플 FieldINCLUDEPICTURE.java을 참조하십시오. 간단한 또는 복잡한 필드로 추가하는 방법을 보여줍니다.

저는 독자에게 아마도 \ d 스위치를 약간의 연습으로 남겨 두었습니다.

실제로 필드 결과를 추가하지 않으면 필드를 새로 고칠 때까지 그림이 Word에 나타나지 않습니다. 필드 결과에 프로그램 방식으로 그림을 추가하는 것은 addImage 예제와 동일합니다.

+0

빠른 업데이트에 감사드립니다 ... !!!! 나는 이것을 지금 점검하고있다!! 1 시간 이내에 생각하면이 사용 사례에 대한 소스를 업데이트 해 주셔서 감사합니다. :-) –

+0

Hello Jason, 이것은 인터넷에있는 파일에서는 작동하지 않습니다. 나는 무엇을 잘못하고있을 것인가? 내 코드를 아래에 붙여 넣으십시오. –

+0

질문에 코드를 삽입 함 –

0

는 또한 원격 링크를 프로그래밍 방식으로 이미지를 삽입하려고 내가 수동으로

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; 

} 

} 
관련 문제