Example usage for org.apache.poi.xwpf.usermodel BodyElementType PARAGRAPH

List of usage examples for org.apache.poi.xwpf.usermodel BodyElementType PARAGRAPH

Introduction

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

Prototype

BodyElementType PARAGRAPH

To view the source code for org.apache.poi.xwpf.usermodel BodyElementType PARAGRAPH.

Click Source Link

Usage

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
 * returns the type of the BodyElement Paragraph
 * @see org.apache.poi.xwpf.usermodel.IBodyElement#getElementType()
 *///from  w w  w  .  ja v  a  2  s .c  o m
public BodyElementType getElementType() {
    return BodyElementType.PARAGRAPH;
}

From source file:com.example.office.DOCDocumentParse.java

@SuppressWarnings("unused")
private File doc2docxOld(File docFile) {
    String docxFilePath = docFile.getPath() + "x";
    File docxFile = new File(docxFilePath);
    if (!docxFile.exists()) {
        XWPFDocument document = null;//from   w ww  .  ja v  a2  s  . c  o  m
        try (InputStream ins = new FileInputStream(docxFile);
                OutputStream out = new FileOutputStream(docxFile);) {
            Document doc = new Document(docFile.getPath());
            doc.save(docxFile.getPath());
            document = new XWPFDocument(ins);

            // document.removeBodyElement(0)
            List<IBodyElement> elements = document.getBodyElements();
            IBodyElement element = elements.get(elements.size() - 1);
            if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) {
                XWPFParagraph xp = ((XWPFParagraph) element);
                String text = xp.getText();
                if (StringUtils.isNotBlank(text)) {
                    if (text.contains("Evaluation") && text.contains("Aspose")) {
                        document.removeBodyElement(elements.size() - 1);
                    }
                }
            }
            IBodyElement element0 = elements.get(0);
            if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) {
                XWPFParagraph xp = ((XWPFParagraph) element0);
                String text = xp.getText();
                if (StringUtils.isNotBlank(text)) {
                    if (text.contains("Evaluation") && text.contains("Aspose")) {
                        document.removeBodyElement(0);
                    }
                }
            }

            document.write(out);
        } catch (Exception e) {
            LogUtils.writeWarnExceptionLog(log, e);
        } finally {
            try {
                if (document != null)
                    document.close();
            } catch (IOException e) {
                LogUtils.writeDebugExceptionLog(log, e);
            }
        }
    }
    return docxFile;
}

From source file:com.example.office.DOCDocumentParse.java

private void deleteAsposeInfo(File docxFile) {
    XWPFDocument document = null;/* ww  w. j a va2  s . c  om*/
    try (InputStream ins = new FileInputStream(docxFile); OutputStream out = new FileOutputStream(docxFile);) {
        document = new XWPFDocument(ins);
        List<IBodyElement> elements = document.getBodyElements();
        IBodyElement element = elements.get(elements.size() - 1);
        if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element.getElementType().name())) {
            XWPFParagraph xp = ((XWPFParagraph) element);
            String text = xp.getText();
            if (StringUtils.isNotBlank(text)) {
                if (text.contains("Evaluation") && text.contains("Aspose")) {
                    document.removeBodyElement(elements.size() - 1);
                }
            }
        }
        IBodyElement element0 = elements.get(0);
        if (StringUtils.equals(BodyElementType.PARAGRAPH.name(), element0.getElementType().name())) {
            XWPFParagraph xp = ((XWPFParagraph) element0);
            String text = xp.getText();
            if (StringUtils.isNotBlank(text)) {
                if (text.contains("Evaluation") && text.contains("Aspose")) {
                    document.removeBodyElement(0);
                }
            }
        }

        document.write(out);
    } catch (Exception e) {
        LogUtils.writeWarnExceptionLog(log, e);
    } finally {
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
                LogUtils.writeDebugExceptionLog(log, e);
            }
        }

    }
}

From source file:com.pdf.GetPdf.java

public static void docConvert(Document document, String url, String type)
        throws IOException, DocumentException {
    WordExtractor we;//from   ww  w.  ja v  a  2s.  c o m

    if (type.equals("doc")) {
        HWPFDocument wordDoc = new HWPFDocument(new URL(url).openStream());
        we = new WordExtractor(wordDoc);
        String[] paragraphs = we.getParagraphText();
        for (int i = 0; i < paragraphs.length; i++) {
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            document.add(new Paragraph(paragraphs[i]));
        }
    } else {
        XWPFDocument wordDoc = new XWPFDocument(new URL(url).openStream());
        List<IBodyElement> contents = wordDoc.getBodyElements();

        for (IBodyElement content : contents) {
            if (content.getElementType() == BodyElementType.PARAGRAPH) {
                List<XWPFParagraph> paras = content.getBody().getParagraphs();
                for (XWPFParagraph para : paras) {
                    document.add(new Paragraph(para.getParagraphText()));
                }

            } else if (content.getElementType() == BodyElementType.TABLE) {
                List<XWPFTable> tables = content.getBody().getTables();
                for (XWPFTable table : tables) {
                    List<XWPFTableRow> rows = table.getRows();
                    for (XWPFTableRow row : rows) {
                        List<XWPFTableCell> tablecells = row.getTableCells();
                    }
                }
            }

        }
    }

}

From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void replaceAllBodyElements(List<IBodyElement> bodyElements, String placeHolder,
        String replaceText) {/*from   w  ww.j  a va  2s  .  c om*/
    for (IBodyElement bodyElement : bodyElements) {
        if (bodyElement.getElementType().compareTo(BodyElementType.PARAGRAPH) == 0)
            replaceParagraph((XWPFParagraph) bodyElement, placeHolder, replaceText);
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.A.java

License:Open Source License

@Test
public void testParagraphStyles() throws Exception {
    // 1) Load docx with Apache POI
    XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("DocxStructures.docx"));

    // Create styles engine
    XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document);

    // Loop for each paragraph
    List<IBodyElement> elements = document.getBodyElements();
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            testParagraph((XWPFParagraph) element, stylesDocument);
        }/*from w  w w.  j  a  v  a  2  s  . com*/
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java

License:Open Source License

@Test
public void testParagraphStyles() throws Exception {
    // 1) Load docx with Apache POI
    XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TestFontStylesBasedOn.docx"));

    // Create styles engine
    XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document);

    // Loop for each paragraph
    List<IBodyElement> elements = document.getBodyElements();
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            testParagraph((XWPFParagraph) element, stylesDocument);
        }/*from w  w w  .  j  a  v a  2s  . c o  m*/
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.run.FontSizeDocDefaultsTestCase.java

License:Open Source License

private void internalTest(Float size, String docName) throws Exception {

    XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream(docName));
    XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document);
    List<IBodyElement> elements = document.getBodyElements();
    boolean ran = false;
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            for (XWPFRun docxRun : ((XWPFParagraph) element).getRuns()) {
                Object sizeFromStyle = stylesDocument.getFontSize(docxRun);
                ran = true;/*from   www  . j  a  va2 s.  co m*/
                assertEquals(sizeFromStyle, size);
            }
        }
    }
    assertTrue(ran);
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

/**
 * Returns true if the given paragraph which is empty (none <w:r> run) must generate new line and false otherwise.
 * /*from  w  ww.ja  v  a 2  s .  c o  m*/
 * @param paragraph
 * @param index
 * @return
 */
private boolean isAddNewLine(XWPFParagraph paragraph, int index) {
    // a new line must be generated if :
    // - there is next paragraph/table
    // - if the body is a cell (with none vMerge) and contains just this paragraph
    IBody body = paragraph.getBody();
    List<IBodyElement> bodyElements = body.getBodyElements();
    if (body.getPartType() == BodyType.TABLECELL && bodyElements.size() == 1) {
        XWPFTableCell cell = (XWPFTableCell) body;
        STMerge.Enum vMerge = stylesDocument.getTableCellVMerge(cell);
        if (vMerge != null && vMerge.equals(STMerge.CONTINUE)) {
            // here a new line must not be generated because the body is a cell (with none vMerge) and contains just
            // this paragraph
            return false;
        }
        // Loop for each cell of the row : if all cells are empty, new line must be generated otherwise none empty
        // line must be generated.
        XWPFTableRow row = cell.getTableRow();
        List<XWPFTableCell> cells = row.getTableCells();
        for (XWPFTableCell c : cells) {
            if (c.getBodyElements().size() != 1) {
                return false;
            }
            IBodyElement element = c.getBodyElements().get(0);
            if (element.getElementType() != BodyElementType.PARAGRAPH) {
                return false;
            }
            return ((XWPFParagraph) element).getRuns().size() == 0;
        }
        return true;

    }
    // here a new line must be generated if there is next paragraph/table
    return bodyElements.size() > index + 1;
}

From source file:org.obeonetwork.m2doc.parser.TokenIterator.java

License:Open Source License

/**
 * Put the iterator in a state where it is finished or there's a token to
 * consumme in the currentIterator.//from  w  w w .  j  av  a  2  s . co  m
 */
private void moveToNextToken() {
    if (tokenIterator == null || !tokenIterator.hasNext()) {
        while (elementIterator.hasNext() && (tokenIterator == null || !tokenIterator.hasNext())) {
            final IBodyElement element = elementIterator.next();
            if (element.getElementType().equals(BodyElementType.PARAGRAPH)) {
                // create an empty run if there's no run in the paragraph.
                // this eases the processing of documents. The processing is based on runs and a paragraph that has no run in it won't
                // be seen by the generator and, as a consequence, won't be inserted as a static part in the result.
                XWPFParagraph paragraph = (XWPFParagraph) element;
                if (paragraph.getRuns().size() == 0) {
                    paragraph.createRun().setText("");
                }
                tokenIterator = new RunIterator(((XWPFParagraph) element).getRuns());
            } else if (element.getElementType().equals(BodyElementType.TABLE)) {
                tokenIterator = new TableIterator((XWPFTable) element);
            } else {
                throw new UnsupportedOperationException(
                        "Unsupported type of body element : " + element.getElementType());
            }
        }
    }
}