java - Apache Poi: adding an image to paragraph does not work -


i using apache 3.17.

the code below (main method + addimage + addtitle methods).

package office; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream;  import org.apache.poi.openxml4j.exceptions.invalidformatexception; import org.apache.poi.util.units; import org.apache.poi.xwpf.usermodel.xwpfdocument; import org.apache.poi.xwpf.usermodel.xwpfparagraph; import org.apache.poi.xwpf.usermodel.xwpfrun;   public class wordgenerator {     xwpfdocument doc = new xwpfdocument();      public wordgenerator(){         this.doc = new xwpfdocument();     }      public xwpfdocument getdoc(){         return this.doc;     }      public static void main(string[] args) throws ioexception, invalidformatexception{         wordgenerator wg=new wordgenerator();         wg.addtitle("ola");         wg.addimage("path/to/image.jpg");           fileoutputstream out = new fileoutputstream("simple.docx");         wg.getdoc().write(out);         out.close();     }       public void addtitle(string title){         xwpfparagraph p=this.doc.createparagraph();         xwpfrun run=p.createrun();         run.settext(title);      }     public void addimage(string imagepath) throws ioexception, invalidformatexception{         int format=-1;          inputstream stream=new fileinputstream(imagepath);          if (imagepath.endswith(".png")){             format = xwpfdocument.picture_type_png;         }else if (imagepath.endswith(".jpeg") || imagepath.endswith(".jpg")){             format = xwpfdocument.picture_type_jpeg;         }         if (format<0){             system.out.println("problem format");             stream.close();             return;         }          xwpfparagraph p=this.doc.createparagraph();         xwpfrun run=p.createrun();         run.addpicture(stream, format, imagepath,  units.toemu(200), units.toemu(200));          system.out.println("picture added");     }  } 

the result if add both image , title, generates empty document.

if add title, generated document right one.

this known bug in apache poi (one of several bug reports here )

see work arounds discussed in questions:

how add picture .docx document apache poi xwpf in java

insert picture in word document

do need write out docx files?


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -