Example usage for org.apache.poi.hwpf.usermodel Section getParagraph

List of usage examples for org.apache.poi.hwpf.usermodel Section getParagraph

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.usermodel Section getParagraph.

Prototype


public Paragraph getParagraph(int index) 

Source Link

Document

Gets the paragraph at index.

Usage

From source file:com.duroty.lucene.parser.MSWordParser.java

License:Open Source License

/**
 * DOCUMENT ME!/*from  w ww.  j a v a2 s. c  o  m*/
 *
 * @return DOCUMENT ME!
 *
 * @throws ParserException DOCUMENT ME!
 */
private String getContents() throws ParserException {
    String contents = "";

    try {
        HWPFDocument doc = new HWPFDocument(input);
        Range r = doc.getRange();
        StringBuffer buffer = new StringBuffer();

        for (int x = 0; x < r.numSections(); x++) {
            Section s = r.getSection(x);

            for (int y = 0; y < s.numParagraphs(); y++) {
                Paragraph p = null;

                try {
                    p = s.getParagraph(y);
                } catch (Exception e) {
                    buffer.append("\n");
                }

                if (p != null) {
                    for (int z = 0; z < p.numCharacterRuns(); z++) {
                        try {
                            //character run
                            CharacterRun run = p.getCharacterRun(z);

                            //character run text
                            buffer.append(run.text());
                        } catch (Exception e) {
                            buffer.append(" ");
                        }
                    }
                }

                /*if (sleep > 0) {
                    try {
                        Thread.sleep(sleep);
                    } catch (Exception ex) {
                    }
                }*/
                // use a new line at the paragraph break
                buffer.append("\n");
            }
        }

        contents = buffer.toString();
    } catch (Exception ex) {
        throw new ParserException(ex);
    }

    return contents;
}

From source file:javaapplication1.HWPFTest.java

private static HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText) {
    Range r1 = doc.getRange();/* ww w . j a v a 2  s  .c o m*/

    for (int i = 0; i < r1.numSections(); ++i) {
        Section s = r1.getSection(i);
        for (int x = 0; x < s.numParagraphs(); x++) {
            Paragraph p = s.getParagraph(x);
            /*String text = p.text();
            if(text.contains(findText)) {
            p.replaceText(replaceText, findText);
            }*/

            for (int z = 0; z < p.numCharacterRuns(); z++) {
                CharacterRun run = p.getCharacterRun(z);
                String text = run.text();
                if (text.contains(findText)) {
                    run.replaceText(findText, replaceText);
                }
            }
        }
    }
    return doc;
}

From source file:Modelo.EscribirWord.java

private HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText) {

    Range r1 = doc.getRange();// w  w w .  j  a  va2s.  c o  m

    for (int i = 0; i < r1.numSections(); ++i) {
        Section s = r1.getSection(i);
        for (int x = 0; x < s.numParagraphs(); x++) {
            Paragraph p = s.getParagraph(x);
            for (int z = 0; z < p.numCharacterRuns(); z++) {
                CharacterRun run = p.getCharacterRun(z);
                String text = run.text();

                if (text.contains(findText)) {

                    if (replaceText == null) {
                        System.out.println("null");
                        replaceText = "";
                    }

                    run.replaceText(findText, replaceText);
                }

            }
        }
    }
    return doc;
}

From source file:org.docx4j.convert.in.Doc.java

License:Apache License

/**
 * This method is private, since the fact that conversion is (currently)
 * performed using POI's HWPF should be encapsulated.
 * /*from   www .  ja  v a  2 s .  co  m*/
 * @param doc
 * @param wordMLPackage
 * @return success or failure
 */
private static void convert(HWPFDocument doc, WordprocessingMLPackage wordMLPackage) throws Exception {

    // Convert styles
    org.apache.poi.hwpf.model.StyleSheet stylesheet = doc.getStyleSheet();
    // TODO - higher priority
    // At present, a default set of styles are defined in the output
    // document.

    // Convert lists
    org.apache.poi.hwpf.model.ListTables listTables = doc.getListTables();
    // TODO

    // Convert document properties
    org.apache.poi.hwpf.model.DocumentProperties docProps = doc.getDocProperties();
    // TODO

    // Convert main document part

    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();

    Range r = doc.getRange();

    for (int x = 0; x < r.numSections(); x++) {
        Section s = r.getSection(x);

        // TODO - convert section

        for (int y = 0; y < s.numParagraphs(); y++) {
            Paragraph p = s.getParagraph(y);

            if (p.isInTable()) {
                Table t = s.getTable(p);
                int cl = numCol(t);

                log.info("Found " + t.numRows() + "x" + cl + " table - TODO - convert");

                handleTable(wordMLPackage, doc, t, stylesheet, documentPart, factory);

                // addTODO(factory, wmlP, "[TABLE " + + t.numRows() + "x" +
                // cl
                // + " - can't convert tables yet]");

                y += t.numParagraphs() - 1;
            }

            else {
                org.docx4j.wml.P paraToAdd = handleP(wordMLPackage, doc, p, stylesheet, documentPart, factory);

                documentPart.addObject(paraToAdd);
            }

        }
    }

}