Hi Team,
I am beginner in Java.
I am trying to prepare report using Java,the code will generate a Microsoft Word Document and add some text and images in it.
I am using Apache POI for this.I am able to create and add text in the doc file and the doc is opening fine,but when i am trying to add image in the doc file,the code is not giving any error,but when I am trying to open the Word Dcument it is giving a erro message like

"The Office Open XML file reportcan not be opened because there are problems with the contents"

PFA the file

and I am unable to open the document
06c4dea449aab88c5b2eab071ee1b70d

My code is:

package com.example.ImageAttachmentInDocument;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.*;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;


public class ImageAttachmentInDocument {

    /**
     * @param args
     */
        public static void main(String[] args) {
        // TODO Auto-generated method stub
            DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
            Calendar cal = Calendar.getInstance();
            String date=dateFormat.format(cal.getTime());
            XWPFDocument document = new XWPFDocument();
            XWPFParagraph paragraphOne = document.createParagraph();
            paragraphOne.setAlignment(ParagraphAlignment.CENTER);

            XWPFRun paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setFontSize(20);
            paragraphOneRunOne.setFontFamily("Verdana");
            paragraphOneRunOne.setColor("000070");
            paragraphOneRunOne.setText("Daily Status Report");
            //XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
            //paragraphOneRunTwo.setText(" More text in paragraph one...");

            XWPFParagraph paragraphTwo = document.createParagraph();
            paragraphTwo.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
            //paragraphTwoRunOne.setBold(true);
            paragraphTwoRunOne.setFontSize(12);
            paragraphTwoRunOne.setFontFamily("Verdana");
            paragraphTwoRunOne.setColor("000070");
            paragraphTwoRunOne.setText(date);
            paragraphTwoRunOne.addBreak();

            XWPFParagraph paragraphThree = document.createParagraph();
            paragraphThree.setAlignment(ParagraphAlignment.LEFT);
            XWPFRun paragraphThreeRunOne = paragraphThree.createRun();
            //paragraphThreeRunOne.setBold(true);
            paragraphThreeRunOne.setFontSize(14);
            paragraphThreeRunOne.setFontFamily("Verdana");
            paragraphThreeRunOne.setColor("000070");
            paragraphThreeRunOne.setText("5.30 AM PST");
            paragraphThreeRunOne.addBreak();

            XWPFParagraph paragraphFour = document.createParagraph();
            paragraphFour.setAlignment(ParagraphAlignment.LEFT);
            XWPFRun paragraphFourRunOne = paragraphFour.createRun();
            paragraphFourRunOne.setBold(true);
            paragraphFourRunOne.setUnderline(UnderlinePatterns.SINGLE);
            paragraphFourRunOne.setFontSize(10);
            paragraphFourRunOne.setFontFamily("Verdana");
            paragraphFourRunOne.setColor("000070");
            paragraphFourRunOne.setText("ABCD");
            /*up to this the code is working fine,and it is preparing the doc as expected,but after adding the rest of the part, that is adding the image,the doc got corrupted*/

            InputStream pic=null;
            try {
                pic = new FileInputStream("C:\\1.JPG");
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            XWPFParagraph paragraphFive = document.createParagraph();
            paragraphFive.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun paragraphFiveRunOne = paragraphFive.createRun();
            try {
                paragraphFiveRunOne.addPicture(pic, XWPFDocument.PICTURE_TYPE_JPEG, "C:\\1.JPG",Units.toEMU(200), Units.toEMU(200));
            } catch (InvalidFormatException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 


            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream("c:\\screenshot.docx");
            } catch (FileNotFoundException e) {
                System.out.println("First Catch");
                e.printStackTrace();
            }
            try {
                document.write(outStream);
                outStream.close();
            } catch (FileNotFoundException e) {
                System.out.println("Second Catch");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Third Catch");
                e.printStackTrace();
            }

        }
}

Please help...i need it urgently

i have same problem

please help me

this code inserts pic in word .. used poi 3.10 ..worked for me

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class ImageAttachmentInDocument {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        // InputStream is = new
        // FileInputStream("D:\\c2\\Generated Sample Letter.doc");
        // POIFSFileSystem fs = new POIFSFileSystem(is);
        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        Calendar cal = Calendar.getInstance();
        String date = dateFormat.format(cal.getTime());
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setFontSize(20);
        paragraphOneRunOne.setFontFamily("Verdana");
        paragraphOneRunOne.setColor("000070");
        paragraphOneRunOne.setText("Daily Status Report");
        // XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
        // paragraphOneRunTwo.setText(" More text in paragraph one...");
        XWPFParagraph paragraphTwo = document.createParagraph();
        paragraphTwo.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
        // paragraphTwoRunOne.setBold(true);
        paragraphTwoRunOne.setFontSize(12);
        paragraphTwoRunOne.setFontFamily("Verdana");
        paragraphTwoRunOne.setColor("000070");
        paragraphTwoRunOne.setText(date);
        paragraphTwoRunOne.addBreak();
        XWPFParagraph paragraphThree = document.createParagraph();
        paragraphThree.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun paragraphThreeRunOne = paragraphThree.createRun();
        // paragraphThreeRunOne.setBold(true);
        paragraphThreeRunOne.setFontSize(14);
        paragraphThreeRunOne.setFontFamily("Verdana");
        paragraphThreeRunOne.setColor("000070");
        paragraphThreeRunOne.setText("5.30 AM PST");
        paragraphThreeRunOne.addBreak();
        XWPFParagraph paragraphFour = document.createParagraph();
        paragraphFour.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun paragraphFourRunOne = paragraphFour.createRun();
        paragraphFourRunOne.setBold(true);
        paragraphFourRunOne.setUnderline(UnderlinePatterns.SINGLE);
        paragraphFourRunOne.setFontSize(10);
        paragraphFourRunOne.setFontFamily("Verdana");
        paragraphFourRunOne.setColor("000070");
        paragraphFourRunOne.setText("ABCD");
        /*
         * up to this the code is working fine,and it is preparing the doc as
         * expected,but after adding the rest of the part, that is adding the
         * image,the doc got corrupted
         */
        InputStream pic = null;
        try {
            pic = new FileInputStream("D:\\c2\\cts.jpg");
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        XWPFParagraph paragraphFive = document.createParagraph();
        paragraphFive.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun paragraphFiveRunOne = paragraphFive.createRun();
        try {

            // Adding Picture
            // paragraphFiveRunOne.addPicture(pic,
            // XWPFDocument.PICTURE_TYPE_GIF, "D:\\c2\\WLB.gif",Units.toEMU(10),
            // Units.toEMU(10));
            /*
             * paragraphFive.getDocument().addPictureData(arg0, arg1);
             * paragraphFive.createRun().addPicture(arg0, arg1, arg2, arg3,
             * arg4);
             */
            paragraphFiveRunOne.addPicture(pic, Document.PICTURE_TYPE_GIF,
                    null, 0, 100);

            paragraphFiveRunOne.addCarriageReturn();
            pic.close();
        } catch (InvalidFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream("D:\\c2\\test1.doc");
        } catch (FileNotFoundException e) {
            System.out.println("First Catch");
            e.printStackTrace();
        }
        try {
            document.write(outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            System.out.println("Second Catch");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Third Catch");
            e.printStackTrace();
        }
    }
}


/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    // InputStream is = new
    // FileInputStream("D:\\c2\\Generated Sample Letter.doc");
    // POIFSFileSystem fs = new POIFSFileSystem(is);
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    Calendar cal = Calendar.getInstance();
    String date = dateFormat.format(cal.getTime());
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph paragraphOne = document.createParagraph();
    paragraphOne.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun paragraphOneRunOne = paragraphOne.createRun();
    paragraphOneRunOne.setBold(true);
    paragraphOneRunOne.setFontSize(20);
    paragraphOneRunOne.setFontFamily("Verdana");
    paragraphOneRunOne.setColor("000070");
    paragraphOneRunOne.setText("Daily Status Report");
    // XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
    // paragraphOneRunTwo.setText(" More text in paragraph one...");
    XWPFParagraph paragraphTwo = document.createParagraph();
    paragraphTwo.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
    // paragraphTwoRunOne.setBold(true);
    paragraphTwoRunOne.setFontSize(12);
    paragraphTwoRunOne.setFontFamily("Verdana");
    paragraphTwoRunOne.setColor("000070");
    paragraphTwoRunOne.setText(date);
    paragraphTwoRunOne.addBreak();
    XWPFParagraph paragraphThree = document.createParagraph();
    paragraphThree.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun paragraphThreeRunOne = paragraphThree.createRun();
    // paragraphThreeRunOne.setBold(true);
    paragraphThreeRunOne.setFontSize(14);
    paragraphThreeRunOne.setFontFamily("Verdana");
    paragraphThreeRunOne.setColor("000070");
    paragraphThreeRunOne.setText("5.30 AM PST");
    paragraphThreeRunOne.addBreak();
    XWPFParagraph paragraphFour = document.createParagraph();
    paragraphFour.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun paragraphFourRunOne = paragraphFour.createRun();
    paragraphFourRunOne.setBold(true);
    paragraphFourRunOne.setUnderline(UnderlinePatterns.SINGLE);
    paragraphFourRunOne.setFontSize(10);
    paragraphFourRunOne.setFontFamily("Verdana");
    paragraphFourRunOne.setColor("000070");
    paragraphFourRunOne.setText("ABCD");
    /*
     * up to this the code is working fine,and it is preparing the doc as
     * expected,but after adding the rest of the part, that is adding the
     * image,the doc got corrupted
     */
    InputStream pic = null;
    try {
        pic = new FileInputStream("D:\\c2\\cts.jpg");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    XWPFParagraph paragraphFive = document.createParagraph();
    paragraphFive.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun paragraphFiveRunOne = paragraphFive.createRun();
    try {

        // Adding Picture
        // paragraphFiveRunOne.addPicture(pic,
        // XWPFDocument.PICTURE_TYPE_GIF, "D:\\c2\\WLB.gif",Units.toEMU(10),
        // Units.toEMU(10));
        /*
         * paragraphFive.getDocument().addPictureData(arg0, arg1);
         * paragraphFive.createRun().addPicture(arg0, arg1, arg2, arg3,
         * arg4);
         */
        paragraphFiveRunOne.addPicture(pic, Document.PICTURE_TYPE_GIF,
                null, 0, 100);

        paragraphFiveRunOne.addCarriageReturn();
        pic.close();
    } catch (InvalidFormatException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream("D:\\c2\\test1.doc");
    } catch (FileNotFoundException e) {
        System.out.println("First Catch");
        e.printStackTrace();
    }
    try {
        document.write(outStream);
        outStream.close();
    } catch (FileNotFoundException e) {
        System.out.println("Second Catch");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Third Catch");
        e.printStackTrace();
    }
}

}

` Previous code got some issue this works fine.. I have used poi 3.10 to generate word doc and to insert pic.it worked for me..try it.. 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class ImageAttachmentInDocument {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        Calendar cal = Calendar.getInstance();
        String date = dateFormat.format(cal.getTime());

        // Create a document file
        CustomXWPFDocument document = new CustomXWPFDocument();

        // Adding a file
        try {

            // Working addPicture Code below...
            XWPFParagraph paragraphX = document.createParagraph();
            paragraphX.setAlignment(ParagraphAlignment.CENTER);

            String blipId = paragraphX.getDocument().addPictureData(
                    new FileInputStream(new File("D://c2//WLB.jpg")),
                    Document.PICTURE_TYPE_JPEG);
            System.out.println("4" + blipId);
            System.out.println(document
                    .getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));
            document.createPicture(blipId,
                    document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG),
                    200, 75);
            System.out.println("5");

        } catch (InvalidFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // insert doc details
        // Createa a para -1
        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setFontSize(20);
        paragraphOneRunOne.setFontFamily("Verdana");
        paragraphOneRunOne.setColor("000070");
        paragraphOneRunOne.setText("Daily Status Report");

        // Createa a para -2
        XWPFParagraph paragraphTwo = document.createParagraph();
        paragraphTwo.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
        paragraphTwoRunOne.setFontSize(12);
        paragraphTwoRunOne.setFontFamily("Verdana");
        paragraphTwoRunOne.setColor("000070");
        paragraphTwoRunOne.setText(date);
        paragraphTwoRunOne.addBreak();

        // Createa a para -3
        XWPFParagraph paragraphThree = document.createParagraph();
        paragraphThree.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun paragraphThreeRunOne = paragraphThree.createRun();
        paragraphThreeRunOne.setFontSize(14);
        paragraphThreeRunOne.setFontFamily("Verdana");
        paragraphThreeRunOne.setColor("000070");
        paragraphThreeRunOne.setText("5.30 AM PST");
        paragraphThreeRunOne.addBreak();

        // Createa a para -4
        XWPFParagraph paragraphFour = document.createParagraph();
        paragraphFour.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun paragraphFourRunOne = paragraphFour.createRun();
        paragraphFourRunOne.setBold(true);
        paragraphFourRunOne.setUnderline(UnderlinePatterns.SINGLE);
        paragraphFourRunOne.setFontSize(10);
        paragraphFourRunOne.setFontFamily("Verdana");
        paragraphFourRunOne.setColor("000070");
        paragraphFourRunOne.setText("ABCD");

        // insert doc details end

        XWPFParagraph paragraphFive = document.createParagraph();
        paragraphFive.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun paragraphFiveRunOne = paragraphFive.createRun();
        paragraphFiveRunOne.addBreak();
        paragraphFourRunOne.setBold(true);
        paragraphFourRunOne.setUnderline(UnderlinePatterns.SINGLE);
        paragraphFourRunOne.setFontSize(10);
        paragraphFourRunOne.setFontFamily("Verdana");
        paragraphFourRunOne.setColor("000070");
        paragraphFourRunOne.setText("ABCD00000000000");

        FileOutputStream outStream = null;
        try {
            double x = Math.random();
            String fileName = "D:\\c2\\" + String.valueOf(x) + ".docx";
            outStream = new FileOutputStream(fileName);
        } catch (FileNotFoundException e) {
            System.out.println("First Catch");
            e.printStackTrace();
        }
        try {
            document.write(outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            System.out.println("Second Catch");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Third Catch");
            e.printStackTrace();
        }
    }
}`

Thanks arindam for the reply.Anyways I have solved the issue using java2word.jar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.