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:de.iisys.schub.processMining.similarity.parsing.DocxParser.java

License:Apache License

private XWPFDocument readFile(InputStream is) {
    if (is != null) {
        try {/*w ww . java 2s  .co m*/
            XWPFDocument doc = new XWPFDocument(OPCPackage.open(is));
            is.close();
            return doc;
        } catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:de.knowwe.include.export.ExportModel.java

License:Open Source License

public ExportModel(ExportManager manager, InputStream templateStream, ProgressListener progressListener)
        throws IOException {
    // create new document based on template
    this.manager = manager;
    this.exporters = manager.createExporters();
    this.document = new XWPFDocument(templateStream);
    this.progressListener = progressListener;

    // delete all undesired example content
    int index = 0;
    for (IBodyElement element : document.getBodyElements()) {
        if (element instanceof XWPFParagraph) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            if (Strings.equalsIgnoreCase(paragraph.getStyle(), "StartDelete")) {
                break;
            }/*from   www  .ja v a  2 s  . co  m*/
        }
        index++;
    }
    while (document.getBodyElements().size() > index) {
        document.removeBodyElement(index);
    }
}

From source file:de.micromata.tpsb.doc.renderer.MicrosoftWordRenderer.java

License:Apache License

@Override
public void renderResult(ParserContext ctx, ParserConfig cfg) {
    ParserResult parserResult = ctx.getCurrentParserResult();
    try {/*from   www  . j a  v  a2  s.c om*/
        document = new XWPFDocument(new FileInputStream(template));
    } catch (FileNotFoundException e1) {
        log.error("Microsoft Word docx Vorlage nicht gefunden.", e1);
    } catch (IOException e1) {
        log.error("Fehler beim Verarbeiten der Word Vorlage.", e1);
    }

    // Neue Seite
    addPageBreak();

    // Zusammenfassung
    addSummaryPage(parserResult);

    // Inhalt...
    addTestCases(parserResult);

    // Report speichern
    saveReport(cfg);
}

From source file:de.powerstaff.business.service.impl.reader.msword.DOCXWordDocumentReader.java

License:Open Source License

public ReadResult getContent(File inputFile) throws Exception {

    XWPFDocument theDocument = new XWPFDocument(new FileInputStream(inputFile));
    XWPFWordExtractor theExtractor = new XWPFWordExtractor(theDocument);
    String theText = theExtractor.getText();
    theText = theText.replace('|', ' ');
    return new ReadResult(toFlatString(theText));
}

From source file:demo.poi.image.SimpleImages.java

License:Apache License

public static List<byte[]> extractImagesFromWord(InputStream file) {
    try {//from w  w  w .  j ava  2  s. c  o m
        List<byte[]> result = new ArrayList<byte[]>();
        XWPFDocument doc = new XWPFDocument(file);
        log.debug("{}", doc);

        for (XWPFPictureData picture : doc.getAllPictures()) {
            result.add(picture.getData());
            log.debug("{}", picture.getFileName());
        }

        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:DocxProcess.DocxTemplateReplacer.java

public DocxTemplateReplacer(InputStream is) {
    try {//from ww w .  j  av a 2 s .c om
        this.docx = new XWPFDocument(is);
    } catch (IOException e) {
        System.err.println("File isn't docx.");
    }
}

From source file:DocxProcess.ReadWordDocx.java

public void showDocx(String path) throws IOException {
    InputStream is = new FileInputStream(path);
    XWPFDocument doc = new XWPFDocument(is);
    this.ReadByDocx(doc);
    this.close(is);
}

From source file:easyoffice.word.WordMaker.java

private static XWPFDocument openDoc() {

    XWPFDocument doc = null;/*from   w w w.jav a 2  s  . c  o m*/
    String path = DEFAULT_FILE_DIR + INPUT_PPT_NAME;

    try {
        File file = new File(path);
        FileInputStream inputstream = new FileInputStream(file);
        doc = new XWPFDocument(inputstream);
    } catch (IOException ex) {
        System.err.println("el documento por defecto no existe (./" + path + ")");
        System.exit(-1);
    }
    return doc;
}

From source file:edu.gatech.pmase.capstone.awesome.impl.output.DisasterResponseTradeStudyOutputer.java

License:Open Source License

@Override
public Path createOutputFile(final List<DRTSArchitectureResult> results,
        final List<DisasterEffect> selectedDisasterEffects, final List<TerrainEffect> selectedTerrainEffects)
        throws IOException, InvalidFormatException {
    // set time//w ww .  ja v  a  2 s.  c  o m
    now = ZonedDateTime.now();

    LOGGER.info("Creating results architecture file.");
    final String filename = "DRTS-Results-" + fileNameFormatter.format(now) + ".docx";

    // create paths
    final Path templatePath = Paths.get(
            DisasterResponseTradeStudyPropertiesSingleton.getInstance().getResultsDirectory(),
            DisasterResponseTradeStudyPropertiesSingleton.getInstance().getResultsTemplate());

    final Path resultsDir = Paths
            .get(DisasterResponseTradeStudyPropertiesSingleton.getInstance().getResultsDirectory());

    Path resultPath = Paths
            .get(DisasterResponseTradeStudyPropertiesSingleton.getInstance().getResultsDirectory(), filename);

    // copy template file
    final File workbookFile = templatePath.toFile();

    if (workbookFile.exists() && !workbookFile.isDirectory() && workbookFile.canRead()) {
        LOGGER.debug("Using results template file: " + workbookFile.getAbsolutePath());
        final XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(workbookFile));

        // get table
        final List<XWPFTable> tables = xdoc.getTables();

        // set values
        DRTSArchitectureResult result = null;

        if (null != results && !results.isEmpty()) {
            result = results.get(0);

            // create platform weightings table
            this.createOptionWeightingTable(result.getPlatform(), tables.get(PLATFORM_WEIGHTING_TABLE_INDEX));

            // create comm weighting table
            this.createOptionWeightingTable(result.getComms(), tables.get(COMM_WEIGHTING_TABLE_INDEX));

            // create sensor weighting table
            this.createOptionWeightingTable(result.getSensor(), tables.get(SENSOR_WEIGHTING_TABLE_INDEX));
        } else {
            LOGGER.warn("No architecture result found, cannot place into output result.");
        }

        // create arch table
        this.createArchTable(result, tables.get(ARCH_RESULT_TABLE_INDEX));

        // create disaster effect table
        this.createSelectedDisasterTable(selectedDisasterEffects, tables.get(SELECTED_DISASTER_TABLE_INDEX));

        // create selected terrain table
        this.createSelectedTerrainTable(selectedTerrainEffects, tables.get(SELECTED_TERRAIN_TABLE_INDEX));

        // create details
        this.createReportDetails(xdoc);

        // get result file
        final File resultFile = resultPath.toFile();
        LOGGER.debug("Trying to use result file: " + resultFile.getAbsolutePath());
        LOGGER.debug("Result file is in result directory: " + resultsDir.toAbsolutePath());

        if (!resultFile.isDirectory() && Files.isDirectory(resultsDir) && Files.isWritable(resultsDir)) {
            LOGGER.debug("Creating result file: " + resultFile.getAbsolutePath());
            // write out result
            try (final FileOutputStream outStream = new FileOutputStream(resultFile)) {
                xdoc.write(outStream);
            }
        } else {
            LOGGER.error("Cannot create output result file at path: " + resultFile.getAbsolutePath());
            resultPath = null;
        }
    } else {
        LOGGER.error("Cannot read input workbook file at path: " + templatePath.toString());
        resultPath = null;
    }

    return resultPath;
}

From source file:edu.ur.ir.index.DefaultWordXmlTextExtractor.java

License:Apache License

/**
 * Extract text from a word 97-2003 document.
 * @throws Exception //www  .  j ava2  s . c om
 * 
 * @see edu.ur.ir.index.FileTextExtractor#getText(java.io.File)
 */
public String getText(File f) throws Exception {
    log.debug("Getting text for file " + f.getAbsolutePath());
    String text = null;
    if (isFileTooLarge(f) || f.length() <= 0l) {
        return text;
    }

    OPCPackage p = null;
    try {
        p = XWPFDocument.openPackage(f.getAbsolutePath());
        XWPFDocument wordDocument = new XWPFDocument(p);
        XWPFWordExtractor wordExtractor = new XWPFWordExtractor(wordDocument);

        String myText = wordExtractor.getText();
        if (myText != null && !myText.trim().equals("")) {
            text = myText;
        }

    } catch (OutOfMemoryError oome) {
        text = null;
        log.error("could not extract text", oome);
        throw (oome);
    } catch (Exception e) {
        text = null;
        log.error("could not get text for word document " + f.getAbsolutePath(), e);
        throw (e);
    }

    finally {
        if (p != null) {
            try {
                p.close();
                p = null;
            } catch (IOException e) {
                log.debug(e);
                p = null;
            }
        }
    }
    return text;
}