Write into a word file using JAVA
<p>I read a word document and want to write into another word file using Java. I want the style (font, bold, italic, heading, etc.) of the content in the read document to be written as it is the new document created.
I am able to copy the content but not the format style.</p>

<pre><code>import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.util.List;

public class ReadFile 
{
    public static void main(String[] args)throws Exception 
    {

        XWPFDocument docx = new XWPFDocument(new  FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));
        List&lt;XWPFParagraph&gt; paragraphList =  docx.getParagraphs();

        XWPFDocument document= new XWPFDocument(); 
        FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
        XWPFParagraph n = document.createParagraph();
        XWPFRun run=n.createRun();

        for (XWPFParagraph paragraph: paragraphList)
        { 
            run.setText(paragraph.getText());              
            run.addCarriageReturn();
        }
        document.write(out); 
        document.close();   
        out.close();
        System.out.println("Test2.docx written successfully");
    }
}
</code></pre>

<p>I got an answer to copy the same format of text but I am unable to copy numbers.
I executed this code: </p>

<pre><code> import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import java.util.List;

public class ReadFile 
{
public static void main(String[] args)throws Exception 
{

  XWPFDocument docx = new XWPFDocument(new    FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));

  List&lt;XWPFParagraph&gt; paragraphList =  docx.getParagraphs();

   XWPFDocument document= new XWPFDocument(); 
   FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
   XWPFParagraph n = document.createParagraph();



  for (XWPFParagraph paragraph : paragraphList)
  {

       for(XWPFRun run1 : paragraph.getRuns())
       {
         XWPFRun run=n.createRun();
         run.setText(run1.getText(0));
        run.setFontFamily( run1.getFontFamily() );
        run.setBold( run1.isBold() );
        run.setItalic( run1.isItalic() );
        run.setStrike( run1.isStrike() );
        run.setColor( run1.getColor() );
        }
       XWPFRun run=n.createRun();
       run.addCarriageReturn();
    }
   document.write(out); 
   document.close();   
   out.close();
   System.out.println("Test2.docx written successfully"); 
  }
  }
</code></pre>