Put the image in a text document

I am trying to create a document with apachepoi text that will contain an image jpeg. I found the code to make it out of here qaru.site/questions/1447666 / ... . However, when I run the code, it is created docx, it seems to be its size, which contains the jpg image, but I could not open it. My code is as follows:

    import org.apache.poi.util.Units;
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;
    import org.apache.poi.xwpf.usermodel.BreakType; 
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;


    public class SimpleImages {

    public static void main(String[] args) throws Exception {
        XWPFDocument doc = new XWPFDocument();
        XWPFParagraph p = doc.createParagraph();

        XWPFRun r = p.createRun();

        //for(String imgFile : args) {
        String imgFile = "mosaic.jpg";
            int format =XWPFDocument.PICTURE_TYPE_JPEG;

            r.setText(imgFile);
            r.addBreak();
            r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
            r.addBreak(BreakType.PAGE);
        //}

        FileOutputStream out = new FileOutputStream("images.docx");
        doc.write(out);
        out.close();
    }


}

When I tried to open my docx, I get:

file.docx cannot be opened because there are problems with the content

.

+4
source share
1 answer

, . poi 3.10, . 3.12, .

0

Source: https://habr.com/ru/post/1536590/


All Articles