title: Java - Image into Word Doc
body: <p>Screenshot are getting created and word doc also has some size but when I open it its empty...Can anyone please suggest, code attached</p>
  
  <p>I need to have all my screenshots in word document together.</p>
  
  <p>was getting some "Enable Content" error while opening the word document, I have trusted the publisher for it...now that error (well not an error) is not coming but the WORD doc is empty with reasonable size.</p>
  
  <pre><code>import java.awt.AWTException;
  import java.awt.HeadlessException;
  import java.awt.Rectangle;
  import java.awt.Robot;
  import java.awt.Toolkit;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.concurrent.TimeUnit;
  
  import javax.imageio.ImageIO;
  
  import org.apache.commons.io.IOUtils;
  import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  import org.apache.poi.xwpf.usermodel.Document;
  import org.apache.poi.xwpf.usermodel.XWPFDocument;
  import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  import org.apache.poi.xwpf.usermodel.XWPFRun;
  
  public class TakeScreenshots {
  
      public static void main(String[] args) {
          try {
  
              // for (int counter = 0; counter &lt;= 5; counter++) {
              captureScreenShot();
              // captureScreenShot(out, docx);
              // }
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
      public static void captureScreenShot() throws Exception {
  
          XWPFDocument docx = new XWPFDocument();
          String screenshot_name = System.currentTimeMillis() + ".png";
  
          BufferedImage image = new Robot()
                  .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
          ImageIO.write(image, "png", new File("d:/xyz/" + screenshot_name));
  
          InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
          docx.addPictureData(pic, Document.PICTURE_TYPE_PNG);
          FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");
          docx.write(out);
          pic.close();
          out.flush();
          out.close();
  
          TimeUnit.SECONDS.sleep(1);
      }
  
  }
  </code></pre>