2014-02-19 34 views
4

我試圖添加圖像到使用docx4j的新單詞文檔。我從網上採取了這種代碼,並修改它爲我的計劃,但是我收到一個奇怪的錯誤,我不知道是什麼原因造成或如何調試它...我試圖添加圖像到docx4j的新單詞文檔

這裏是我的代碼

private static void test() 
{ 
    WordprocessingMLPackage wordMLPackage = null; 
    try { 
     wordMLPackage = WordprocessingMLPackage.createPackage(); 
    } catch (InvalidFormatException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Cannnot create package."); 
    } 
    wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello Word! \n\t" + "Try This!"); 

    byte[] bytes = null; 
    try { 
     bytes = convertImageToByteArray(); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Image file not found."); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Image file exception: " + e1.toString()); 
    } 
    try { 
     addImageToPackage(wordMLPackage, bytes); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Cannot add image to package: " + e.toString()); 
    } 

    try { 
     wordMLPackage.save(new java.io.File("HelloWord7.docx")); 
    } catch (Docx4JException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Cannot save image to file."); 
    } 
} 

private static void addImageToPackage(WordprocessingMLPackage wordMLPackage, 
     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); 

    P paragraph = addInlineImageToParagraph(inline); 
    wordMLPackage.getMainDocumentPart().addObject(paragraph); 
} 

private static P addInlineImageToParagraph(Inline inline) { 
    // Now add the in-line image to a paragraph 
    ObjectFactory factory = new ObjectFactory(); 
    P paragraph = factory.createP(); 
    R run = factory.createR(); 
    paragraph.getContent().add(run); 
    Drawing drawing = (Drawing) factory.createDrawing(); 
    run.getContent().add(drawing); 
    ((org.docx4j.wml.Drawing) drawing).getAnchorOrInline().add(inline); 
    return paragraph; 
} 


private static byte[] convertImageToByteArray() throws IOException { 
    // get DataBufferBytes from Raster 
    WritableRaster raster = logo.getRaster(); 
    DataBufferByte data = (DataBufferByte)raster.getDataBuffer(); 

    return (data.getData()); 
} 

,我發現了以下錯誤的

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

返回錯誤是:Docx4JException:錯誤檢查圖像格式。

這裏的是如何 '標誌' 被載入,

try { 
     BufferedImage logo = ImageIO.read(getClass().getResourceAsStream("/logo.png")); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     javax.swing.JOptionPane.showMessageDialog(panel, "Cannot load logo for word doc"); 
    } 

任何幫助表示讚賞,感謝

新信息


我運行應用程序運行的JAR和我收到上面提到的各種圖像類型的相同錯誤,例如:png,jpg。我曾嘗試通過從main()調用test()在eclipse中運行應用程序,並且應用程序卡住了,爲什麼?我該如何調試它?

我修改了一下代碼,我將文件傳遞給createImagePart而不是byte []數組。

public static P newImage(WordprocessingMLPackage wordMLPackage, File file, 
     String filenameHint, String altText, int id1, int id2) throws Exception { 
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, file); 
    //.createImagePart(wordMLPackage, bytes); 
    javax.swing.JOptionPane.showMessageDialog(panel, "Created image part"); 
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false); 

    ObjectFactory factory = new ObjectFactory(); 

    P p = factory.createP(); 
    R run = factory.createR(); 

    p.getContent().add(run);   
    Drawing drawing = (Drawing) factory.createDrawing();  
    run.getContent().add(drawing);  
    ((org.docx4j.wml.Drawing) drawing).getAnchorOrInline().add(inline); 

    return p; 
} 

private static void test() throws Exception 
{ 
    File file = new File("logo.png"); 
    if (!file.canRead()) 
     javax.swing.JOptionPane.showMessageDialog(panel, "Cannot read file"); 
    if (!file.exists()) 
     javax.swing.JOptionPane.showMessageDialog(panel, "File does not exist"); 
    javax.swing.JOptionPane.showMessageDialog(panel, file.getAbsolutePath()); 

    String filenameHint = null; 
    String altText = null; 

    int id1 = 0; 
    int id2 = 1; 

    P p = newImage(wordMLPackage, file, filenameHint, altText, id1, id2); 

    wordMLPackage.getMainDocumentPart().addObject(p); 
    wordMLPackage.save(new File("Example.docx")); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       wordMLPackage = WordprocessingMLPackage.createPackage(); 
      } catch (InvalidFormatException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
       javax.swing.JOptionPane.showMessageDialog(panel, "Could not create wordMLPackage: " + e2.toString()); 
      } 
      new Calculator().setVisible(true); 
     } 
    }); 
} 

回答

4

下面是一個簡單的例子,你可以把它作爲基礎。

import java.io.*; 

import org.docx4j.dml.wordprocessingDrawing.Inline; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.wml.*; 
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 

public class Example { 
    public static void main(String[] args) throws Exception { 
     WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.createPackage(); 
     File file = new File("C://java-duke-logo.jpg"); 

     InputStream inputStream = new java.io.FileInputStream(file); 
     long fileLength = file.length();  

     byte[] bytes = new byte[(int)fileLength]; 

     int offset = 0; 
     int numRead = 0; 

     while(offset < bytes.length 
       && (numRead = inputStream.read(bytes, offset, bytes.length-offset)) >= 0) { 
      offset += numRead; 
     } 

     inputStream.close(); 

     String filenameHint = null; 
     String altText = null; 

     int id1 = 0; 
     int id2 = 1; 

     P p = newImage(wordprocessingMLPackage, bytes, filenameHint, altText, id1, id2); 

     wordprocessingMLPackage.getMainDocumentPart().addObject(p); 
     wordprocessingMLPackage.save(new File("C://Example.docx")); 
    } 

    public static P newImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, 
      String filenameHint, String altText, int id1, int id2) throws Exception { 
     BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes); 
     Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2); 

     ObjectFactory factory = new ObjectFactory(); 

     P p = factory.createP(); 
     R run = factory.createR(); 

     p.getParagraphContent().add(run);   
     Drawing drawing = factory.createDrawing();  
     run.getRunContent().add(drawing);  
     drawing.getAnchorOrInline().add(inline); 

     return p; 
    } 
} 

Example.docx:

enter image description here

+0

感謝您的答覆...我收到了同樣的錯誤: Docx4JException:錯誤檢查圖像格式 此錯誤從線彈出 BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage,bytes); –

+0

我想你如何序列化圖像的錯誤。如果使用示例中顯示的代碼而不是「convertImageToByteArray()'方法,它將解決問題。祝你好運。 –

+0

我使用您發佈的代碼... –