Here is my Java code for reading docx document, maybe it helps somebody:

/**
         * Read content of *.doc file
         * @param fileName - full name of target file
         * @return - {@code String} representation of file content
         */
        public String readDocxFileToString(String fileName) {
                //String content = "";
                StringBuilder content = new StringBuilder();
                File file = new File(fileName);

                String filePath = FileUtil.getAbsolutePath(fileName);
                log.debug(this.toString() + "; Opening file: " + filePath);

                XWPFDocument doc;
                try {

                        doc = new XWPFDocument(new FileInputStream(file));

                        List<XWPFParagraph> paragraphs = doc.getParagraphs();

                        for (XWPFParagraph par : paragraphs) {

                                List<XWPFRun> runs = par.getRuns();

                                for (XWPFRun run : runs) {

                                        //content += run.getText(0);
                                        content = 
content.append(run.getText(0));

                                }
                        }

                } catch (FileNotFoundException ex) {
                        System.out.println("File was not found :: " + filePath);
                } catch (IOException e) {
                        System.out.println("Failed to close '" + filePath + "' 
file output
stream");
                }

                return content.toString();
                
        }

 

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/Help-in-XWPF-POI-code-tp3287261p5281585.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to