konstantin_v
konstantin_v

Reputation: 11

How to edit docx using Java

I need replace cerain words or phrases in docx-file and save it with another name. I know that my problem is not unik and I tried find solution in the web. But I still can't get a result that I need.

I found two ways to solwe my task but came to the deadlock in each case. 1. Unfold docx like a zip-file, change xml with main content and pack into archive again. But after that manipulations I can't open new changed docx in MS Word. It is odd because I can do the similar steps by hands (without Java, using WinRar) and get a correct result file. So can you explain me how to archive docx content to get a correct file using Java?

  1. Using external API. I get an advice to use docx4j Java library. But all tat I can with it is just replace a label (like ${label}) in template with any words (I used VariableReplace sample). But I want change words that I want without using a template with labels.

I hope for a help.

Upvotes: 1

Views: 12760

Answers (2)

JHDev
JHDev

Reputation: 995

I had this code. I hope that it helps you to resolve your problem. With it, you can read from a .docx find the word that you would change. Change this word and save the new paragraphs in new document.

 //WriteDocx.java
   import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
   import org.apache.poi.xwpf.usermodel.*;
   import java.io.*;
   import java.util.ArrayList;
   import java.util.List;
   import java.util.StringTokenizer;

   public class WriteDocx
   {
    public static void main(String[] args) throws Exception {
        int count = 0;
        XWPFDocument document = new XWPFDocument();
        XWPFDocument docx = new XWPFDocument(new FileInputStream("Bonjour1.docx"));
        XWPFWordExtractor we = new XWPFWordExtractor(docx);
        String text = we.getText() ;
        if(text.contains("SMS")){
            text = text.replace("SMS", "sms");
            System.out.println(text);
        }
        char[] c = text.toCharArray();
        for(int i= 0; i < c.length;i++){

            if(c[i] == '\n'){
                count ++;
            }
        }
        System.out.println(c[0]);
        StringTokenizer st = new StringTokenizer(text,"\n");

        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = para.createRun();
        run.setBold(true);
        run.setFontSize(36);
        run.setText("Apache POI works well!");

        List<XWPFParagraph>paragraphs = new ArrayList<XWPFParagraph>();
        List<XWPFRun>runs = new ArrayList<XWPFRun>();
        int k = 0;
        for(k=0;k<count+1;k++){
            paragraphs.add(document.createParagraph());
        }
        k=0;
        while(st.hasMoreElements()){
            paragraphs.get(k).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(k).setSpacingAfter(0);
            paragraphs.get(k).setSpacingBefore(0);
            run = paragraphs.get(k).createRun();
            run.setText(st.nextElement().toString());
            k++;
        }

        document.write(new FileOutputStream("test2.docx"));
    }          
   }

PS: XWPFDocument docx = new XWPFDocument(new FileInputStream("Bonjour1.docx"))

You must change "Bonjour1.docx" with the name of file from where you would replace certain words or phrases. I use APACHE POI library And I take some code from this site HANDLING MS WORD DOCUMENTS USING APACHE POI

UPDATE enter image description here

Upvotes: 4

JasonPlutext
JasonPlutext

Reputation: 15878

If you want to change arbitrary words, you can do that easily enough with docx4j.

But first you need to find them.

You can find your words using an XPath query, or by traversing the document tree in Java.

Upvotes: 0

Related Questions