2

I'm writing java code to achieve the followings.

1.Read given Microsoft-office document(.doc) file.

2.Search for given string in the file.

3.Delete the given String located in any place.

4.Insert or replace any given string at specified position.

5.Write and save the updated file content into new .doc file.

I have written a code to read, search, insert or replace, delete and save the file and it's working good, but i couldn't able to preserve the text format(such as font color, font size, justification, left and right indent, styles etc) applied in the input file.

please anyone helps me to solve the issue.

Thank you

nagesh
  • 307
  • 2
  • 10
  • 22

3 Answers3

2

I'll added new solution for styling Ms-Word Document..

public class CreateDocumentFromScratch {

    public static void main(String[] args) {
        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        paragraphOne.setBorderBottom(Borders.SINGLE);
        paragraphOne.setBorderTop(Borders.SINGLE);
        paragraphOne.setBorderRight(Borders.SINGLE);
        paragraphOne.setBorderLeft(Borders.SINGLE);
        paragraphOne.setBorderBetween(Borders.SINGLE);

        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setItalic(true);
        paragraphOneRunOne.setText("Hello world! This is paragraph one!");
        paragraphOneRunOne.addBreak();

        XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
        paragraphOneRunTwo.setText("Run two!");
        paragraphOneRunTwo.setTextPosition(100);

        XWPFRun paragraphOneRunThree = paragraphOne.createRun();
        paragraphOneRunThree.setStrike(true);
        paragraphOneRunThree.setFontSize(20);
        paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
        paragraphOneRunThree.setText(" More text in paragraph one...");

        XWPFParagraph paragraphTwo = document.createParagraph();
        paragraphTwo.setAlignment(ParagraphAlignment.DISTRIBUTE);
        paragraphTwo.setIndentationRight(200);
        XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
        paragraphTwoRunOne.setText("And this is paragraph two.");

        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(args[0]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            document.write(outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Bhushankumar Lilapara
  • 780
  • 1
  • 13
  • 26
2

You can use the following code:

public class EditingWord{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String filename = "path to input file/file_input_name";
        List<String> paraList = new ArrayList<String>();
        try {

            XWPFDocument doc = new XWPFDocument(OPCPackage.open(new FileInputStream(filename)));
            List<XWPFParagraph> paragraphList = doc.getParagraphs();
            for (XWPFParagraph para : paragraphList) {
                if ((para.getStyle() != null) && (para.getNumFmt() != null)) {
                    for (XWPFRun run : para.getRuns()) {
                        String text = run.text();
                        text = text.replaceAll(text, "replacement" + text);
                        run.setText(text, 0);
                    }
                }
            }
            doc.write(new FileOutputStream("path to your file/output_File_name"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

In case if you want to save your content to same file, you can change doc.write(new FileOutputStream("path to your inpufile/input_File_name"));

fredtantini
  • 15,966
  • 8
  • 49
  • 55
0

I'll suggest you to use of Apache POI Documentation. I'll do some experiment using documentation and getting text formatting easily for Ms-Excel Sheet..

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html

I was browsing through the API when I saw the DataFormat class and its hierarchy, the BuiltinFormats class, and the setDataFormat method of the CellStyle class. So did some experimentation, and the code below seems to work!

XSSFCellStyle textFormatStyle = book.createCellStyle(); 
textFormatStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text")); 
XSSFCell cell = row.createCell(columnIndex++); 
cell.setCellStyle(textFormatStyle); 

Now, once spreadsheet is created, you can edit a cell, and when you tab out, the format remains "text".

I having showing you another way with full example.. In which I'll show one effect further you can add as per your requirement...

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");

HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);


font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);

row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);

try {
    FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
    workbook.write(out);
    out.close();
    System.out.println("Excel written successfully..");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

This code will generate bellow output :

enter image description here

Bhushankumar Lilapara
  • 780
  • 1
  • 13
  • 26
  • That looks to be for Excel though, not Word! – Gagravarr May 30 '13 at 20:38
  • Thank you for the reply. Though you given answer for defining styles in excel sheet but the requirement is need to fetch and apply the same styles used in the old ms-office .doc file. I'm looking for a method which fetch the styles applied to each and every text in the doc file. Once i get the style it would be easy to apply the style while creating a new copy(with modified content) of a doc file. – nagesh May 31 '13 at 05:47
  • I am sorry for misguide you. I have added new answer for formatting Ms-Word Document, Please check... – Bhushankumar Lilapara Jun 05 '13 at 08:53