Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert picture in word document
    primarykey
    data
    text
    <p>This is the first time I am working on Apache POI and the question which I am going to ask has been asked already on this site but no clear answer were given for them so I have no choice but to take all your help.</p> <p>I am trying to write a java program which takes images from one folder and insert that image into a word document. I am using Apache POI for this program. Here I am posting my code.</p> <pre><code>import java.io.*; import java.util.*; import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.*; public class ImagesDoc { public static void main(String[] args) throws IOException { XWPFDocument docx = new XWPFDocument(); XWPFParagraph par = docx.createParagraph(); XWPFRun run = par.createRun(); run.setText("Hello, World. This is my first java generated docx-file. Have fun."); run.setFontSize(13); InputStream pic = new FileInputStream("C:\\Users\\amitabh\\Pictures\\pics\\pool.jpg"); byte [] picbytes = IOUtils.toByteArray(pic); docx.addPicture(picbytes, Document.PICTURE_TYPE_JPEG); FileOutputStream out = new FileOutputStream("C:\\Users\\amitabh\\Pictures\\pics\\simple1.docx"); docx.write(out); out.close(); pic.close(); } } </code></pre> <p>I am able to create the word document file and I am able to insert the text as well but <code>docx.addPicture(picbytes, Document.PICTURE_TYPE_JPEG);</code> line is giving me the error as"add cast to docx". I have added all possible jars for this program. For this error I have searched all over the net and found that many people are having similar problem. "addPicture" for XWPFDocument reference is not working. Please help me to resolve this problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload