locked
Cannot modify word document (.doc, .docx, .dot) using java algorithm RRS feed

  • Question

  • Hi

    I'm trying to modify text in a word document. Those documents contains pictures and tables. I'm using Eclipse plateform with POI library to open the document.
    Code :
                String filesname = "C:/test.doc";
                POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filesname));
                HWPFDocument doc = new HWPFDocument(fs);
                Range range = doc.getRange();
                for (int i = 0; i < range.numSections(); i++)
                {
                    Section section  = range.getSection(i);
                    for (int x = 0; x < section.numParagraphs(); x++)
                    {
                        Paragraph p = section.getParagraph(x);
                        for (int z = 0; z < p.numCharacterRuns(); z++)
                        {
                            //character run  
                            CharacterRun run = p.getCharacterRun(z);  
                            //character run text  
                            String text = run.text();  
                            if( text.contains("word_text"))
                            {
                                run.insertAfter("word_replace_text");
                            }
                        }
                    }
                }
                OutputStream out = new FileOutputStream(new File("C:/res.doc"));
                doc.write(out);
                out.flush();
                out.close();


    Problems : Pictures aren't visible in the result document (a simply square with a huge red cross appears) then when a target text is founded in a table the shape is altered.

    Is possibly to unzip word document to XML files and modify only the text ?

    Thanks..
    Wednesday, September 16, 2009 1:31 PM

Answers