1
votes

Screenshot are getting created and word doc also has some size but when I open it its empty...Can anyone please suggest, code attached

I need to have all my screenshots in word document together.

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.

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 <= 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);
    }

}
1

1 Answers

0
votes

Modify your captureScreenShot method like below and it will work.

public static void captureScreenShot() throws Exception {
    XWPFDocument docx = new XWPFDocument();
    XWPFParagraph paragraph = docx.createParagraph();    
    XWPFRun run = paragraph.createRun();
    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);
    run.addBreak();
    run.addPicture(pic, XWPFDocument.PICTURE_TYPE_JPEG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
    pic.close();
    FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");
    docx.write(out);
    out.flush();
    out.close();

    TimeUnit.SECONDS.sleep(1);
}