Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write into a word file using JAVA

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.

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<XWPFParagraph> 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");
    }
}

I got an answer to copy the same format of text but I am unable to copy numbers. I executed this 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<XWPFParagraph> 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"); 
  }
  }
like image 606
Mehul Jain Avatar asked Oct 18 '22 01:10

Mehul Jain


1 Answers

Copying whole paragraphs from one Word docx to another will be simpler than taking all the content from one Word docx to a single run in a single paragraph of another. And since you stated that there are numberings in the source docx, the whole paragraphs are needed since only paragraphs can be numbered.

But to copy the numberings, the /word/numbering.xml must also be copied. So if the Test1.docx looks like this: enter image description here

After processing the following program:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;

import java.util.List;
import java.lang.reflect.Field;

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

  XWPFDocument docx1 = new XWPFDocument(new  FileInputStream("Test1.docx"));
  XWPFNumbering numberingDocx1 = docx1.getNumbering();
  // get paragraphListDocx1 as a List of all paragraphs from docx1
  List<XWPFParagraph> paragraphListDocx1 =  docx1.getParagraphs();

  // get the numbering.xml from docx1 to docx2
  // this is needed if some of the paragraphs from docx1 are numbered
  XWPFDocument docx2= new XWPFDocument(); 
  if (numberingDocx1 != null) {
   XWPFNumbering numberingDocx2 = docx2.createNumbering();
   try {
    Field f = numberingDocx1.getClass().getDeclaredField("ctNumbering");
    f.setAccessible(true);
    numberingDocx2.setNumbering((CTNumbering)f.get(numberingDocx1));
   } catch (NoSuchFieldException nsfex) {
   } catch (IllegalAccessException iaex) {
   }
  }

  // create a paragraph in docx2
  XWPFParagraph paragraphDocx2 = docx2.createParagraph();
  XWPFRun run = paragraphDocx2.createRun();
  run.setText("This is from Test1.docx:");

  // this will copy all paragraphs from paragraphListDocx1 to docx2
  for (XWPFParagraph paragraphDocx1 : paragraphListDocx1) { 
   paragraphDocx2 = docx2.createParagraph();
   docx2.setParagraph(paragraphDocx1, docx2.getPosOfParagraph(paragraphDocx2));            
  }

  paragraphDocx2 = docx2.createParagraph();
  run = paragraphDocx2.createRun();
  run.setText("^-- this was from Test1.docx.");


  FileOutputStream out = new FileOutputStream(new File("Test2.docx"));
  docx2.write(out); 
  docx2.close();   

  System.out.println("Test2.docx written successfully");
 }
}

The Test2.docx will look like this:

enter image description here

Note: Tables will not be copied since we only copy paragraphs. Pictures will be broken since the /word/media/*.* is not copied. Special stylings like "Heading 1" will not be copied since we not copy /word/styles.xml.

like image 83
Axel Richter Avatar answered Oct 21 '22 04:10

Axel Richter