Example usage for org.apache.poi.xwpf.usermodel XWPFRun setText

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun setText

Introduction

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

Prototype

public void setText(String value) 

Source Link

Document

Sets the text of this text run

Usage

From source file:oop.nhom5.de3.model.IOFileWord.java

public static boolean writeWord(String text, String fileName) {
    //Blank Document
    XWPFDocument document = new XWPFDocument();
    //Write the Document in file system
    try {//from  w w  w  . ja  v  a  2s.c  o m
        FileOutputStream out = new FileOutputStream(new File(fileName));
        String[] lines = text.split("\n");
        //create Paragraph
        for (String line : lines) {
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText(line);
        }

        document.write(out);
        out.close();
        return true;
    } catch (IOException | HeadlessException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

private void applyChangeToXWPF(DocumentEvent.ElementChange change, Element root) {
    Document doc = root.getDocument();

    Element[] removedElements = change.getChildrenRemoved();
    Element[] addedElements = change.getChildrenAdded();
    System.out.println(root + " removed=" + removedElements.length + " added=" + addedElements.length);

    for (int i = removedElements.length - 1; i >= 0; i--) {
        int start = removedElements[i].getStartOffset();
        int length = removedElements[i].getEndOffset() - removedElements[i].getStartOffset();
        remove(start, length);//www. j a va  2s. c  o  m
    }
    for (Element addedElem : addedElements) {
        try {
            currentOffset = 0;
            DocumentPosition pos = searchPart(document.getBodyElements(), addedElem.getStartOffset());
            if (pos != null) {
                String text = doc.getText(addedElem.getStartOffset(),
                        addedElem.getEndOffset() - addedElem.getStartOffset());
                XWPFRun run = pos.run.getParagraph().createRun();
                run.setText(text);
                applyAttributes(run, addedElem.getAttributes());
            }
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

}

From source file:org.articleEditor.insertContent.DocumentUpdater1.java

License:Apache License

public void changedUpdateOriginal(DocumentEvent de) {
    DefaultStyledDocument doc = (DefaultStyledDocument) de.getDocument();
    ElementIterator iter = new ElementIterator(de.getDocument());
    for (Element elem = iter.first(); elem != null; elem = iter.next()) {
        DocumentEvent.ElementChange change = de.getChange(elem);
        if (change != null) {
            Element[] removedElems = change.getChildrenRemoved();
            for (int i = removedElems.length - 1; i >= 0; i--) {
                remove(removedElems[i].getStartOffset(), removedElems[i].getEndOffset());
            }//ww  w . j av  a  2  s .c  o m
            for (Element addedElem : change.getChildrenAdded()) {
                try {
                    currentOffset = 0;
                    DocumentPosition pos = searchPart(document.getBodyElements(), addedElem.getStartOffset());
                    if (pos != null) {
                        String text = doc.getText(addedElem.getStartOffset(),
                                addedElem.getEndOffset() - addedElem.getStartOffset());
                        XWPFRun run = pos.run.getParagraph().createRun();
                        run.setText(text);
                        applyAttributes(run, addedElem.getAttributes());
                    }
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
}

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

License:Open Source License

public static void setText(XWPFRun run, String text) {
    String[] split = text.split("\n");
    run.setText(split[0]);
    for (int i = 1; i < split.length; i++) {
        run.addBreak();//from   w  w  w .  ja v  a  2  s  . co  m
        run.setText(split[i]);
    }
}

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 {/*  w  ww.j ava2s . com*/
        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

/**
 * Inserts a reference to the given {@link CTBookmark} with the given text in the given {@link XWPFParagraph}.
 * /*from   w w  w  .  j ava 2 s  .co  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

/**
 * Insert a new run containing the given text.
 * /*from   w ww. j a  v  a  2 s .c o  m*/
 * @param srcRun
 *            the run to copy the style from.
 * @param fragment
 *            the text fragment to insert
 * @return the generated run.
 */
private XWPFRun insertFragment(XWPFRun srcRun, String fragment) {
    XWPFRun generatedRun = currentGeneratedParagraph.createRun();
    generatedRun.getCTR().set(srcRun.getCTR().copy());
    generatedRun.getCTR().getInstrTextList().clear();
    generatedRun.setText(fragment);
    return generatedRun;
}

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

License:Open Source License

/**
 * Inserts the given {@link MTable}.//from   ww w.  j a  v a2s . c o  m
 * 
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param table
 *            the {@link MTable} to insert
 */
private void insertMTable(XWPFRun run, MTable table) {
    final XWPFTable docTable;
    if (generatedDocument instanceof XWPFDocument) {
        if (table.getLabel() != null) {
            XWPFRun captionRun;
            captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        docTable = ((XWPFDocument) generatedDocument).createTable();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        docTable = new XWPFTable(cttbl, headerFooter);
        headerFooter.insertTable(index, docTable);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tcell = (XWPFTableCell) generatedDocument;
        if (table.getLabel() != null) {
            final XWPFRun captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        CTTbl ctTbl = tcell.getCTTc().addNewTbl();
        docTable = new XWPFTable(ctTbl, tcell);
        int tableRank = tcell.getTables().size();
        tcell.insertTable(tableRank, docTable);
        // A paragraph is mandatory at the end of a cell, so let's always add one.
        tcell.addParagraph();
    } else {
        docTable = null;
        M2DocUtils.appendMessageRun((XWPFParagraph) run.getParent(), ValidationMessageLevel.ERROR,
                "m:table can't be inserted here.");
    }

    if (docTable != null) {
        fillTable(docTable, table);
    }
}

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.
 * /*from  ww w. ja v  a 2s  .  co m*/
 * @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.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseImage(Image image) {
    XWPFRun imageRun = insertRun(image.getStyleRun());
    imageRun.setText("");
    imageRun.getCTR().getInstrTextList().clear();
    if (image.getFileName() == null) {
        insertQuerySyntaxMessages(image, "");
    } else {/*from   w ww.  ja v a  2  s  .com*/
        URI imageURI = URI.createFileURI(image.getFileName());
        imageURI = imageURI.resolve(image.eResource().getURI());
        try {
            int heigth = Units.toEMU(image.getHeight());
            int width = Units.toEMU(image.getWidth());

            try (InputStream imageStream = uriConverter.createInputStream(imageURI)) {
                imageRun.addPicture(imageStream, PictureType.toType(imageURI).getPoiType(), image.getFileName(),
                        width, heigth);
            }
        } catch (InvalidFormatException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    String.format(PICTURE_INVALID_FORMAT, imageURI.toString(), e.getMessage()));
        } catch (IOException e) {
            insertMessage(currentGeneratedParagraph, ValidationMessageLevel.ERROR,
                    String.format(AN_I_O_PROBLEM_OCCURED_WHILE_READING, imageURI.toString(), e.getMessage()));
        }
    }

    return image;
}