Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph createRun

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph createRun

Introduction

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

Prototype

public XWPFRun createRun() 

Source Link

Document

Appends a new run to this paragraph

Usage

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

public static void addBookmark(XWPFParagraph paragraph, String bookmarkAnchor, String bookmarkText) {
    CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
    String bookmarkName = generateValidBookmarkName(bookmarkAnchor);
    bookmark.setName(bookmarkName);//from w  w  w.j a v a 2  s. c  o m
    final BigInteger bookmarkId = generateRandomId();
    bookmark.setId(bookmarkId);
    addFormattedText(paragraph.createRun(), bookmarkText, FONT_SIZE + 2, true);
    paragraph.getCTP().addNewBookmarkEnd().setId(bookmarkId);
}

From source file:org.hackthelegacy.hack400tool.ibmiscannercore.IBMiConnector.java

License:Open Source License

public void exportToDOCX(String fileName, String inputText) {

    System.out.println("Save as file: " + fileName);
    try {/*from w  w  w.j  a  va2 s .  co  m*/
        prepareFile(fileName);
        FileOutputStream fileOutStream = new FileOutputStream(fileName);
        XWPFDocument wordDocument = new XWPFDocument();
        String[] textArray = inputText.split("\n");
        for (String textLine : textArray) {
            if (textLine.length() == 0 || textLine == null)
                continue;
            XWPFParagraph tmpParagraph = wordDocument.createParagraph();
            XWPFRun tmpRun = tmpParagraph.createRun();
            tmpRun.setText(textLine);
            tmpRun.setFontSize(8);
            tmpRun.setFontFamily("Courier New");
        }
        wordDocument.write(fileOutStream);
        fileOutStream.close();
    } catch (Exception ex) {
        Logger.getLogger(IBMiConnector.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }
}

From source file:org.obeonetwork.m2doc.generator.BookmarkManager.java

License:Open Source License

/**
 * Starts a bookmark in the given {@link CTBody} with the given name.
 * /*from w  w w .j  av  a 2s . c  o m*/
 * @param paragraph
 *            the current {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 */
public void startBookmark(XWPFParagraph paragraph, String name) {
    if (bookmarks.containsKey(name)) {
        M2DocUtils.appendMessageRun(paragraph, ValidationMessageLevel.ERROR,
                "Can't start duplicated bookmark " + name);
    } else {
        final CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
        // we create a new run for future error messages.
        messagePositions.put(bookmark, paragraph.createRun());
        bookmark.setName(name);
        final BigInteger id = getRandomID();
        bookmark.setId(id);
        bookmarks.put(name, bookmark);
        startedBookmarks.put(name, bookmark);
        Set<CTText> pendingRefs = pendingReferences.remove(name);
        if (pendingRefs != null) {
            for (CTText pendingRef : pendingRefs) {
                // we remove the created for error messages.
                final XWPFRun run = messagePositions.get(pendingRef);
                final IRunBody parent = run.getParent();
                if (parent instanceof XWPFParagraph) {
                    ((XWPFParagraph) parent).removeRun(((XWPFParagraph) parent).getRuns().indexOf(run));
                } else {
                    throw new IllegalStateException("this should not happend");
                }
                pendingRef.setStringValue(String.format(REF_TAG, bookmark.getName()));
            }
        }
    }
}

From source file:org.obeonetwork.m2doc.generator.BookmarkManager.java

License:Open Source License

/**
 * Inserts a pending reference to the given name in the given {@link XWPFParagraph}.
 * // ww w  .ja  va 2s.  c o  m
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 * @param text
 *            the text
 */
public void insertReference(XWPFParagraph paragraph, String name, String text) {
    final CTBookmark bookmark = bookmarks.get(name);
    if (bookmark != null) {
        insertReference(paragraph, bookmark, text);
    } else {
        final XWPFRun messageRun = paragraph.createRun();
        final CTText ref = insertPendingReference(paragraph, name, text);
        messagePositions.put(ref, messageRun);
        Set<CTText> pendingRefs = pendingReferences.get(name);
        if (pendingRefs == null) {
            pendingRefs = new LinkedHashSet<CTText>();
            pendingReferences.put(name, pendingRefs);
        }
        pendingRefs.add(ref);
    }
}

From source file:org.obeonetwork.m2doc.generator.BookmarkManager.java

License:Open Source License

/**
 * Inserts a reference to the given {@link CTBookmark} with the given text in the given {@link XWPFParagraph}.
 * /* w w  w  . j a  va 2s  .  c o m*/
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 * @param text
 *            the text
 * @return the {@link CTText} corresponding to the reference.
 */
private CTText insertPendingReference(XWPFParagraph paragraph, String name, String text) {
    final byte[] id = getReferenceID(name);
    final XWPFRun beginRun = paragraph.createRun();
    beginRun.getCTR().setRsidR(id);
    beginRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);

    final XWPFRun preservedRun = paragraph.createRun();
    preservedRun.getCTR().setRsidR(id);
    final CTText pgcttext = preservedRun.getCTR().addNewInstrText();
    pgcttext.setSpace(Space.PRESERVE);

    final XWPFRun separateRun = paragraph.createRun();
    separateRun.getCTR().setRsidR(id);
    separateRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);

    final XWPFRun textRun = paragraph.createRun();
    textRun.getCTR().setRsidR(id);
    textRun.getCTR().addNewRPr().addNewNoProof();
    textRun.setText(text);
    textRun.setBold(true);

    final XWPFRun endRun = paragraph.createRun();
    endRun.getCTR().setRsidR(id);
    endRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.END);

    return pgcttext;
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Create a new run in the cell's paragraph and set this run's text, and apply the given style to the cell and its paragraph.
 * /*  w  w w.j av  a 2  s .  c  om*/
 * @param cell
 *            Cell to fill in
 * @param mCell
 *            the cell to read data from
 */
private void setCellContent(XWPFTableCell cell, MCell mCell) {
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    XWPFRun cellRun = cellParagraph.createRun();
    if (mCell != null) {
        String label = mCell.getLabel();
        if (label != null) {
            cellRun.setText(label);
        }
        MStyle style = mCell.getStyle();
        if (style != null) {
            if (style.getBackgroundColor() != null) {
                cell.setColor(hexColor(style.getBackgroundColor()));
            }
            applyMStyle(cellRun, style);
        }
    }
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a document./*from  ww w. jav a  2 s  .com*/
 * 
 * @param tableRun
 *            the run after which the table must be created
 * @param first
 *            Whether it's the first table to insert or not
 * @param mtable
 *            The table description
 * @return The newly created table.
 */
private XWPFTable createTableInDocument(XWPFRun tableRun, boolean first, MTable mtable) {
    XWPFTable table;
    if (!first) {
        ((XWPFDocument) body).createParagraph();
    }
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = ((XWPFDocument) body).createParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    table = ((XWPFDocument) body).createTable();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a table cell./*from   w w  w. j  a v a 2s. c om*/
 * 
 * @param tableRun
 *            The table run
 * @param first
 *            whether it's the 1st table created in this cell
 * @param mtable
 *            The table description
 * @param tcell
 *            The cell in which to create a new table
 * @return The newly creatted table, located in the given cell.
 */
private XWPFTable createTableInCell(XWPFRun tableRun, boolean first, MTable mtable, XWPFTableCell tcell) {
    XWPFTable table;
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = tcell.addParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    CTTbl ctTbl = tcell.getCTTc().addNewTbl();
    table = new XWPFTable(ctTbl, tcell);
    int tableRank = tcell.getTables().size();
    tcell.insertTable(tableRank, table);
    // A paragraph is mandatory at the end of a cell, so let's always add one.
    tcell.addParagraph();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a new run in the cell's paragraph and set this run's text, and apply the given style to the cell and its paragraph.
 * /*from  w  ww . j a  v  a2  s.  c o  m*/
 * @param cell
 *            Cell to fill in
 * @param text
 *            Text to set in the cell
 * @param style
 *            Style to use, can be <code>null</code>
 */
private void setCellContent(XWPFTableCell cell, String text, MStyle style) {
    XWPFParagraph cellParagraph = cell.getParagraphs().get(0);
    XWPFRun cellRun = cellParagraph.createRun();
    cellRun.setText(text);
    if (style != null) {
        cell.setColor(hexColor(style.getBackgroundColor()));
        applyTableClientStyle(cellRun, style);
    }
}

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 2s . 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());
            }
        }
    }
}