2014-12-05 71 views
1

正在使用Docx4j在swing应用程序中生成word文档。我想将图片添加到标题。文档已成功创建,但图片未显示。以下是应用程序的代码片段。我正在使用docx4j-nightly-20141016.jar文件。在Docx4j生成的Word文档的标题中显示图像

import org.docx4j.wml.ObjectFactory; 

    public class WordDoc { 

     private WordprocessingMLPackage wordMLPackage; 
     private ObjectFactory factory; 
     private Hdr header; 

     public WordDoc() { 
     } 

     public void createWordDoc() throws Docx4JException, IOException, Exception { 

      wordMLPackage = WordprocessingMLPackage.createPackage(); 
      factory = Context.getWmlObjectFactory(); 

      Relationship relationship = createHeaderPart(); 
      createHeaderReference(relationship); 

      wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!"); 

      File file = new File("src/resources/images/logo.jpg"); 
      byte[] bytes = convertImageToByteArray(file); 
      addImageInline(bytes); 

      wordMLPackage.save(new java.io.File("src/files/HelloWord14.docx")); 

     } 

     private Relationship createHeaderPart() throws InvalidFormatException { 
      HeaderPart headerPart = new HeaderPart(); 
      headerPart.setPackage(wordMLPackage); 

      headerPart.setJaxbElement(createHeader("Text")); 

      return wordMLPackage.getMainDocumentPart().addTargetPart(headerPart); 
     } 

     private Hdr createHeader(String content) { 
      header = factory.createHdr(); 
      P paragraph = factory.createP(); 
      R run = factory.createR(); 
      Text text = new Text(); 
      text.setValue(content); 
      run.getContent().add(text); 
      paragraph.getContent().add(run); 
      header.getContent().add(paragraph); 
      return header; 
     } 

     private void createHeaderReference(Relationship relationship) { 
      List<SectionWrapper> sections 
      = wordMLPackage.getDocumentModel().getSections(); 

      SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr(); 
      // There is always a section wrapper, but it might not contain a sectPr 
      if (sectionProperties == null) { 
       sectionProperties = factory.createSectPr(); 
       wordMLPackage.getMainDocumentPart().addObject(sectionProperties); 
       sections.get(0).setSectPr(sectionProperties); 
      } 

      HeaderReference headerReference = factory.createHeaderReference(); 
      headerReference.setId(relationship.getId()); 
      headerReference.setType(HdrFtrRef.DEFAULT); 
      sectionProperties.getEGHdrFtrReferences().add(headerReference); 
     } 

     private byte[] convertImageToByteArray(File file) 
       throws FileNotFoundException, IOException { 
      InputStream is = new FileInputStream(file); 
     long length = file.length(); 
     // You cannot create an array using a long, it needs to be an int. 
      if (length > Integer.MAX_VALUE) { 
       System.out.println("File too large!!"); 
      } 
     byte[] bytes = new byte[(int) length]; 
      int offset = 0; 
      int numRead = 0; 
      while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - 
        offset)) >= 0) { 
       offset += numRead; 
      } 
      // Ensure all the bytes have been read 
      if (offset < bytes.length) { 
       System.out.println("Could not completely read file " 
         + file.getName()); 
      } 
      is.close(); 
      return bytes; 
     } 

     private void addImageInline(byte[] bytes) throws Exception { 

      BinaryPartAbstractImage imagePart 
        = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); 

      int docPrId = 1; 
      int cNvPrId = 2; 
      Inline inLine = imagePart.createImageInline("Filename hint", 
        "Alternative text", docPrId, cNvPrId, false); 

      if (header != null) { 

       addInlineImageToHeader(inLine); 
      } 
     } 

     private void addInlineImageToHeader(Inline inline) { 
     // Now add the in-line image to a paragraph 
      ObjectFactory factory2 = new ObjectFactory(); 
      P paragraph2 = factory2.createP(); 
      R run = factory.createR(); 
      paragraph2.getContent().add(run); 
      Drawing drawing = factory.createDrawing(); 
      run.getContent().add(drawing); 
      drawing.getAnchorOrInline().add(inline); 
      header.getContent().add(paragraph2); 
     } 

    } 

生成的Word文档的屏幕截图显示以下

enter image description here

也很乐意采取的任何建议。

回答

6

相反的:

 BinaryPartAbstractImage imagePart 
       = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); 

你需要的图像部分添加为头部分的相对,所以使用:

public static BinaryPartAbstractImage createImagePart(
     OpcPackage opcPackage, 
     Part sourcePart, byte[] bytes) throws Exception 

传递headerPart

+0

这个例子可能会有所帮助以及:[docx4j - HeaderFooterCreate](http://www.docx4java.org/svn/docx4j/trunk/docx4j/src/main/java/org/docx4j/samples/HeaderFooterCreate.java)。 – 2014-12-05 20:04:01

+0

检查了这个链接,非常感谢@Freek de Bruijn – CodeAngel 2014-12-06 01:54:42