Writing to a text file using JAVA

I read a text document and want to write to another file using Java. I want the style (font, bold, italics, heading, etc.) of the content in the read document to be written, as this is a new created document. I can copy the contents, 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 a response to copy the same text format, but I can not 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"); 
  }
  }
+4
source share
3 answers

This can help:

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class FontStyle 
{
   public static void main(String[] args)throws Exception 
   {
   //Blank Document
   XWPFDocument document= new XWPFDocument(); 

   //Write the Document in file system
   FileOutputStream out = new FileOutputStream(
   new File("fontstyle.docx"));

   //create paragraph
   XWPFParagraph paragraph = document.createParagraph();

   //Set Bold an Italic
   XWPFRun paragraphOneRunOne = paragraph.createRun();
   paragraphOneRunOne.setBold(true);
   paragraphOneRunOne.setItalic(true);
   paragraphOneRunOne.setText("Font Style");
   paragraphOneRunOne.addBreak();

   //Set text Position
   XWPFRun paragraphOneRunTwo = paragraph.createRun();
   paragraphOneRunTwo.setText("Font Style two");
   paragraphOneRunTwo.setTextPosition(100);

   //Set Strike through and Font Size and Subscript
   XWPFRun paragraphOneRunThree = paragraph.createRun();
   paragraphOneRunThree.setStrike(true);
   paragraphOneRunThree.setFontSize(20);
   paragraphOneRunThree.setSubscript(
   VerticalAlign.SUBSCRIPT);
   paragraphOneRunThree.setText(" Different Font Styles");

   document.write(out);
   out.close();
   System.out.println("fontstyle.docx written successully");
   }
}

Suitable for: http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_font_style.htm

+1
source

Word docx , Word docx . , docx , , .

, /word/numbering.xml. , Test1.docx : enter image description here

:

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

Test2.docx :

enter image description here

: , . , /word/media/*.* . , " 1", , /word/styles.xml.

+1

java. :

Files.copy(new File("source path location"), new File("destination path location"));

, .

0

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


All Articles