Example usage for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument.

Prototype

public XWPFDocument(InputStream is) throws IOException 

Source Link

Usage

From source file:com.anphat.customer.controller.ExportContractToDocController.java

private void getDatasDocx() throws Exception {
    try {/* w  w  w .  java  2 s. co  m*/
        document = new XWPFDocument(new FileInputStream(Constants.PATH_TEMPLATE + fileName));
        //Lay tat ca cac bang
        lstTable = document.getTables();
    } catch (Exception e) {
        throw new Exception("Khng ?c c file biu mu");
    }
}

From source file:com.anritsu.mcreleaseportal.docgeneration.ReleaseMailGeneration.java

private String convertDocToHtml(File fileMail) {
    String html = "";
    try {//  w ww . ja v a 2  s .  c o m
        InputStream is = new FileInputStream(fileMail);
        XWPFDocument document = new XWPFDocument(is);
        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("word/media")));
        OutputStream outStream = new ByteArrayOutputStream();

        XHTMLConverter.getInstance().convert(document, outStream, options);
        html = outStream.toString();

        System.out.println("####MAIL####\n" + html + "\n############\n");

    } catch (IOException ex) {
        Logger.getLogger(ReleaseMailGeneration.class.getName()).log(Level.SEVERE, null, ex);
    }
    return html;

}

From source file:com.artech.prototype2.bardakov.utils.impl.MultiParserImpl.java

/**
 *      doc/docx// w  ww .  jav a 2  s  .  c  o m
 * @param FilePath -   
 * @return ?? ?
 */
private ArrayList<String> getListOfWordsFromDoc(String FilePath) {
    FileInputStream fis;
    List<String> result = new ArrayList<String>();
    if (FilePath.substring(FilePath.length() - 1).equals("x")) { //is a docx
        try {
            fis = new FileInputStream(new File(FilePath));
            XWPFDocument doc = new XWPFDocument(fis);
            XWPFWordExtractor extract = new XWPFWordExtractor(doc);
            // System.out.println(extract.getText());
            StringBuilder builder = new StringBuilder();
            builder.append(extract.getText());
            String[] words = builder.toString().split(" ");
            for (String s : words) {
                result.add(s);
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
    } else { //is not a docx
        try {
            fis = new FileInputStream(new File(FilePath));
            HWPFDocument doc = new HWPFDocument(fis);
            WordExtractor extractor = new WordExtractor(doc);
            StringBuilder builder = new StringBuilder();
            builder.append(extractor.getText());
            String[] words = builder.toString().split(" ");
            for (String s : words) {
                result.add(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return (ArrayList<String>) result;
}

From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.DocxExtractor.java

License:Open Source License

/**
 * Gets the text from file content /*from  w  w w  . ja va  2 s.c  o m*/
 * @param file
 * @param fileExtension
 * @return
 */
@Override
public String getText(File file, String fileExtension) {
    FileInputStream fis = null;
    XWPFWordExtractor ex = null;
    try {
        fis = new FileInputStream(file);
        XWPFDocument doc = new XWPFDocument(fis);
        if (doc != null) {
            ex = new XWPFWordExtractor(doc);
            return ex.getText();
        }
    } catch (FileNotFoundException e) {
        LOGGER.info("File " + file.getName() + " not found. " + e.getMessage());
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } catch (Exception e) {
        LOGGER.debug("Extracting text from the .doc file " + file.getName() + " failed with " + e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            LOGGER.debug("Closing the FileInputStream for file " + file.getName() + " failed with "
                    + e.getMessage());
            LOGGER.error(ExceptionUtils.getStackTrace(e));
        }
        if (ex != null) {
            try {
                ex.close();
            } catch (IOException e) {
                LOGGER.debug("Closing the text extractor from the .docx file " + file.getName()
                        + " failed with " + e.getMessage());
                LOGGER.error(ExceptionUtils.getStackTrace(e));
            }
        }
    }
    return null;
}

From source file:com.bluetech.reader.WordReader.java

License:Apache License

public static String readWordDoc(String filePath) throws FileNotFoundException, IOException {

    File file = new File(filePath);

    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    XWPFDocument document = new XWPFDocument(fis);
    XWPFWordExtractor extractor = new XWPFWordExtractor(document);
    // String[] fileData = extractor.getText().split("##\\d{4}[_]\\d{4}[a-z]*");

    return extractor.getText();
}

From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java

License:Open Source License

@Override
public void generate(String versionOid, List<QuestionSnapshot> questions) {
    File docxFile = new File(getRootPath(versionOid), "T-Java.docx");
    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(docxFile));
            InputStream is = TestGenerator.class.getResourceAsStream("/template/questions.docx")) {

        XWPFDocument doc = new XWPFDocument(is);
        replaceWordHolder(doc);/* ww  w .  j ava 2  s  .c  o  m*/

        XWPFTable table = doc.getTables().get(0);
        for (QuestionSnapshot question : questions) {
            XWPFTableRow row = table.createRow();
            writeQuestionNo(question.getQuestionNo(), row);
            writeQuestion(question, row);
        }

        //  template row
        table.removeRow(0);
        // 
        setBorders(table);
        // 
        doc.write(bos);

        File[] unzipFiles = { docxFile, new File(getRootPath(versionOid), "A-Java.txt") };

        File destination = new File(getRootPath(versionOid), "T-Java-docx.zip");
        ZipUtils.zip(destination, true, null, unzipFiles);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new TestGenException(e.getMessage(), ModuleInfo.TestGenMgr);
    }
}

From source file:com.bxf.hradmin.testgen.service.impl.PdfTestGenerator.java

License:Open Source License

@Override
public void generate(String versionOid, List<QuestionSnapshot> questions) {
    try (InputStream source = new FileInputStream(new File(getRootPath(versionOid), "T-Java.docx"));
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(getRootPath(versionOid), "T-Java.pdf")))) {

        IXWPFConverter<PdfOptions> converter = PdfConverter.getInstance();
        XWPFDocument document = new XWPFDocument(source);
        PdfOptions options = PdfOptions.getDefault();
        converter.convert(document, bos, options);

        File[] unzipFiles = { new File(getRootPath(versionOid), "T-Java.pdf"),
                new File(getRootPath(versionOid), "A-Java.txt") };

        File destination = new File(getRootPath(versionOid), "T-Java-pdf.zip");
        ZipUtils.zip(destination, true, null, unzipFiles);
    } catch (IOException e) {
        log.error(e.getMessage(), e);/*from  w w w.j av a2  s  . c  o  m*/
        throw new TestGenException(e.getMessage(), ModuleInfo.TestGenMgr);
    }
}

From source file:com.camel.util.WordDocReader.java

public File readWordDoc(String orginalFilePath, File newFile, String content) {
    FileInputStream fis = null;//  ww  w. j  a v  a 2 s  .  c om
    FileOutputStream out = null;
    try {
        JTextPane textPane = new JTextPane();
        textPane.setText(content);
        StyledDocument sdoc = textPane.getStyledDocument();

        fis = new FileInputStream(orginalFilePath);
        XWPFDocument doc = new XWPFDocument(fis);
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        System.out.println("sonuc..:" + sdoc.getText(0, sdoc.getEndPosition().getOffset()));
        run.setText(sdoc.getText(0, sdoc.getEndPosition().getOffset()));

        out = new FileOutputStream(newFile); // yeni dosya oluturuluyor. 
        doc.write(out);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {

                out.close();

                fis.close();
                out = null;
                fis = null;
            } catch (IOException ioEx) {
                ioEx.printStackTrace();
            }
        }
    }
    return newFile;
}

From source file:com.ccsna.safetynet.DocReadWrite.java

public List<XWPFParagraph> readDocFile(String fileName) throws URISyntaxException, IOException {
    fileName = "/home/estelle/NetBeansProjects/CCSNA/build/web/public/aboutus.doc";
    //fileName = this.getClass().getClassLoader().getResource("").getPath() + fileName;
    //String glassfishInstanceRootPropertyName = "com.sun.aas.";
    //fileName = System.getProperty(glassfishInstanceRootPropertyName) + fileName;
    //ServletContext context = (ServletContext) getContext();
    //fileName = context.getRealPath("/WEB-INF/" + fileName);
    System.out.println("filename is : " + fileName);
    try {/* ww  w .  j a v  a 2 s  .  c o m*/
        File file = new File(fileName);

        // to read a file from webcontent
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());

        XWPFDocument document = new XWPFDocument(fis);

        List<XWPFParagraph> paragraphs = document.getParagraphs();
        //return paragraphs;

        //fis.close();
        return paragraphs;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.crimelab.service.GalleryServiceImpl.java

@Override
public XWPFDocument create(GalleryResults galleryResults, HttpSession session) {
    XWPFDocument document = null;//from w  w  w . ja  va2  s . co m

    //Insert into database
    galleryDAO.insertResults(galleryResults);

    try {
        //Retrieving Template
        InputStream inpDocx = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/CompositeSketch.docx");
        document = new XWPFDocument(inpDocx);

        //Adding the picture
        XWPFParagraph pictureHolder = document.createParagraph();
        XWPFRun pictureRun = pictureHolder.createRun();

        FileInputStream getPhoto = new FileInputStream(galleryResults.getPhotoLocation());
        FileInputStream getImage = new FileInputStream(galleryResults.getPhotoLocation());
        ImageInputStream imageInput = ImageIO.createImageInputStream(getPhoto);
        BufferedImage bi = ImageIO.read(imageInput);
        int width = bi.getWidth() - 100;
        int height = bi.getHeight() - 100;

        pictureRun.addBreak();
        pictureRun.addPicture(getImage, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(width),
                Units.toEMU(height));

        pictureHolder.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderTop(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderRight(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setAlignment(ParagraphAlignment.CENTER);
        //            pictureRowHolder.getCell(0).setText("IMAGE PLACER");

        //Case number and Date            
        XWPFParagraph info = document.createParagraph();
        XWPFRun caseNoAndDate = info.createRun();
        caseNoAndDate.setText("Case Number: " + galleryResults.getCaseNo());
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.setText(galleryResults.getDate());
        caseNoAndDate.setFontSize(16);

        //Sketch Details
        XWPFParagraph caseDetails = document.createParagraph();
        XWPFRun detailsParagraph = caseDetails.createRun();
        detailsParagraph.setText("Offense/Incident: " + galleryResults.getOffenseIncident());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Name/AKA: " + galleryResults.getNameAKA());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Sex: " + galleryResults.getSex());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Age: " + galleryResults.getAge());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Height: " + galleryResults.getHeight());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Weight: " + galleryResults.getWeight());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Built: " + galleryResults.getBuilt());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Complexion: " + galleryResults.getComplexion());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Other Information: " + galleryResults.getOtherInfo());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Described by: " + galleryResults.getDescribedBy());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Requesting party: " + galleryResults.getRequestingParty());

        //Details Borders
        caseDetails.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderTop(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderRight(Borders.BASIC_BLACK_DASHES);
        caseDetails.setAlignment(ParagraphAlignment.LEFT);

        //Reference Paragraph
        XWPFParagraph outsideDetails = document.createParagraph();
        XWPFRun outsideDetailsRun = outsideDetails.createRun();
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("Note: For reference");
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("The witness indicates that this image is: " + galleryResults.getRating());
        getPhoto.close();
        getImage.close();
        imageInput.close();
        document.getXWPFDocument();
    } catch (IOException | InvalidFormatException e) {
        e.printStackTrace();
    }
    return document;
}