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.obeonetwork.m2doc.test.M2DocTestUtils.java

License:Open Source License

/**
 * Links the given {@link AbstractConstruct} with new {@link XWPFRun} created in the given {@link XWPFParagraph}.
 * //from   w  w  w  .j a  va 2  s  . c  om
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param construct
 *            the {@link AbstractConstruct}
 */
private static void linkRuns(XWPFParagraph paragraph, AbstractConstruct construct) {
    construct.setStyleRun(paragraph.createRun());

    construct.getRuns().add(paragraph.createRun());
    construct.getRuns().add(paragraph.createRun());
    construct.getRuns().add(paragraph.createRun());

    for (EObject child : construct.eContents()) {
        if (child instanceof AbstractConstruct) {
            linkRuns(paragraph, (AbstractConstruct) child);
        }
    }

    construct.getClosingRuns().add(paragraph.createRun());
    construct.getClosingRuns().add(paragraph.createRun());
    construct.getClosingRuns().add(paragraph.createRun());
}

From source file:org.obeonetwork.m2doc.tests.M2DocTestUtils.java

License:Open Source License

/**
 * Links the given {@link IConstruct} with new {@link XWPFRun} created in the given {@link XWPFParagraph}.
 * /*from  w  ww  . j  a v a  2s. c  o  m*/
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param construct
 *            the {@link IConstruct}
 */
private static void linkRuns(XWPFParagraph paragraph, IConstruct construct) {
    construct.setStyleRun(paragraph.createRun());

    construct.getRuns().add(paragraph.createRun());
    construct.getRuns().add(paragraph.createRun());
    construct.getRuns().add(paragraph.createRun());

    for (EObject child : construct.eContents()) {
        if (child instanceof IConstruct) {
            linkRuns(paragraph, (IConstruct) child);
        }
    }

    construct.getClosingRuns().add(paragraph.createRun());
    construct.getClosingRuns().add(paragraph.createRun());
    construct.getClosingRuns().add(paragraph.createRun());
}

From source file:org.obeonetwork.m2doc.util.M2DocUtils.java

License:Open Source License

/**
 * Appends the given error message at the end of the given {@link XWPFParagraph}.
 * //w  w w.  ja v a  2s . co m
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param level
 *            the {@link ValidationMessageLevel}
 * @param message
 *            the message
 * @return the created {@link XWPFRun}
 */
public static XWPFRun appendMessageRun(XWPFParagraph paragraph, ValidationMessageLevel level, String message) {
    final XWPFRun res = paragraph.createRun();

    setRunMessage(res, level, message);

    return res;
}

From source file:org.ohdsi.rabbitInAHat.ETLDocumentGenerator.java

License:Apache License

private static void addSourceTablesAppendix(CustomXWPFDocument document, ETL etl) {
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();
    run.addBreak(BreakType.PAGE);/*from   w  ww  .j a  v  a2s . c  o  m*/
    run.setText("Appendix: source tables");
    run.setFontSize(18);

    for (Table sourceTable : etl.getSourceDatabase().getTables()) {
        paragraph = document.createParagraph();
        run = paragraph.createRun();
        run.setText("Table: " + sourceTable.getName());
        run.setFontSize(14);

        createDocumentParagraph(document, sourceTable.getComment());

        XWPFTable table = document.createTable(sourceTable.getFields().size() + 1, 4);
        // table.setWidth(2000);
        XWPFTableRow header = table.getRow(0);
        setTextAndHeaderShading(header.getCell(0), "Field");
        setTextAndHeaderShading(header.getCell(1), "Type");
        setTextAndHeaderShading(header.getCell(2), "Most freq. value");
        setTextAndHeaderShading(header.getCell(3), "Comment");
        int rowNr = 1;
        for (Field sourceField : sourceTable.getFields()) {
            XWPFTableRow row = table.getRow(rowNr++);
            row.getCell(0).setText(sourceField.getName());
            row.getCell(1).setText(sourceField.getType().toString());
            if (sourceField.getValueCounts() != null && sourceField.getValueCounts().length != 0)
                row.getCell(2).setText(sourceField.getValueCounts()[0][0]);
            createCellParagraph(row.getCell(3), sourceField.getComment().trim());
        }

    }

    run.setFontSize(18);
}

From source file:org.ohdsi.rabbitInAHat.ETLDocumentGenerator.java

License:Apache License

private static void addTargetTableSection(CustomXWPFDocument document, ETL etl, Table targetTable)
        throws InvalidFormatException, FileNotFoundException {
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();
    run.addBreak(BreakType.PAGE);/*from  w w w. j  a va2 s  .co  m*/

    run.setText("Table name: " + targetTable.getName());
    run.setFontSize(18);

    createDocumentParagraph(document, targetTable.getComment());

    for (ItemToItemMap tableToTableMap : etl.getTableToTableMapping().getSourceToTargetMaps())
        if (tableToTableMap.getTargetItem() == targetTable) {
            Table sourceTable = (Table) tableToTableMap.getSourceItem();
            Mapping<Field> fieldtoFieldMapping = etl.getFieldToFieldMapping(sourceTable, targetTable);

            paragraph = document.createParagraph();
            run = paragraph.createRun();
            run.setText("Reading from " + tableToTableMap.getSourceItem());
            run.setFontSize(14);

            createDocumentParagraph(document, tableToTableMap.getLogic());

            createDocumentParagraph(document, tableToTableMap.getComment());

            // Add picture of field to field mapping
            MappingPanel mappingPanel = new MappingPanel(fieldtoFieldMapping);
            mappingPanel.setShowOnlyConnectedItems(true);
            int height = mappingPanel.getMinimumSize().height;
            mappingPanel.setSize(800, height);

            BufferedImage im = new BufferedImage(800, height, BufferedImage.TYPE_INT_ARGB);
            im.getGraphics().setColor(Color.WHITE);
            im.getGraphics().fillRect(0, 0, im.getWidth(), im.getHeight());
            mappingPanel.paint(im.getGraphics());
            document.addPicture(im, 600, height * 6 / 8);

            // Add table of field to field mapping
            XWPFTable table = document.createTable(fieldtoFieldMapping.getTargetItems().size() + 1, 4);
            // table.setWidth(2000);
            XWPFTableRow header = table.getRow(0);
            setTextAndHeaderShading(header.getCell(0), "Destination Field");
            setTextAndHeaderShading(header.getCell(1), "Source Field");
            setTextAndHeaderShading(header.getCell(2), "Logic");
            setTextAndHeaderShading(header.getCell(3), "Comment");
            int rowNr = 1;
            for (MappableItem targetField : fieldtoFieldMapping.getTargetItems()) {
                XWPFTableRow row = table.getRow(rowNr++);
                row.getCell(0).setText(targetField.getName());

                StringBuilder source = new StringBuilder();
                StringBuilder logic = new StringBuilder();
                StringBuilder comment = new StringBuilder();
                for (ItemToItemMap fieldToFieldMap : fieldtoFieldMapping.getSourceToTargetMaps()) {
                    if (fieldToFieldMap.getTargetItem() == targetField) {
                        if (source.length() != 0)
                            source.append("\n");
                        source.append(fieldToFieldMap.getSourceItem().getName().trim());

                        if (logic.length() != 0)
                            logic.append("\n");
                        logic.append(fieldToFieldMap.getLogic().trim());

                        if (comment.length() != 0)
                            comment.append("\n");
                        comment.append(fieldToFieldMap.getComment().trim());
                    }
                }

                for (Field field : targetTable.getFields()) {
                    if (field.getName().equals(targetField.getName())) {
                        if (comment.length() != 0)
                            comment.append("\n");
                        comment.append(field.getComment().trim());
                    }
                }

                createCellParagraph(row.getCell(1), source.toString());
                createCellParagraph(row.getCell(2), logic.toString());
                createCellParagraph(row.getCell(3), comment.toString());
            }
        }

}

From source file:org.ohdsi.rabbitInAHat.ETLDocumentGenerator.java

License:Apache License

private static void addTableLevelSection(CustomXWPFDocument document, ETL etl)
        throws InvalidFormatException, FileNotFoundException {
    XWPFParagraph tmpParagraph = document.createParagraph();
    XWPFRun tmpRun = tmpParagraph.createRun();

    MappingPanel mappingPanel = new MappingPanel(etl.getTableToTableMapping());
    mappingPanel.setShowOnlyConnectedItems(true);
    int height = mappingPanel.getMinimumSize().height;
    mappingPanel.setSize(800, height);/*from  w  w  w  .j  ava 2  s  .c o m*/

    tmpRun.setText(
            mappingPanel.getSourceDbName() + " Data Mapping Approach to " + mappingPanel.getTargetDbName());
    tmpRun.setFontSize(18);

    BufferedImage im = new BufferedImage(800, height, BufferedImage.TYPE_INT_ARGB);
    im.getGraphics().setColor(Color.WHITE);
    im.getGraphics().fillRect(0, 0, im.getWidth(), im.getHeight());
    mappingPanel.paint(im.getGraphics());
    document.addPicture(im, 600, height * 6 / 8);
}

From source file:org.ohdsi.rabbitInAHat.ETLDocumentGenerator.java

License:Apache License

private static void addToParagraph(XWPFParagraph paragraph, String text) {
    XWPFRun run = paragraph.createRun();
    run.setText(text);
}

From source file:org.robert.study.service.merge.doc.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);/*from w w w.j a  v  a  2 s.c o  m*/
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    // BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    // p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
    r4.setItalic(true);
    // This would imply that this break shall be treated as a simple line
    // break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("z://simple.docx");
    doc.write(out);
    out.close();

}

From source file:org.roiderh.machinetoolconfsheet.CreateMachineToolConfSheetAction.java

License:Open Source License

@Override
public void actionPerformed(ActionEvent e) {
    BaseDocument doc = null;//from   ww  w  .j a v a  2s  .com
    JTextComponent ed = org.netbeans.api.editor.EditorRegistry.lastFocusedComponent();
    if (ed == null) {
        JOptionPane.showMessageDialog(null, "Error: no open editor"); //NOI18N
        return;
    }

    FileObject fo = NbEditorUtilities.getFileObject(ed.getDocument());
    String path = fo.getPath();

    InputStream is = new ByteArrayInputStream(ed.getText().getBytes());

    BufferedReader br;

    br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); //NOI18N

    ArrayList<String> lines = new ArrayList<>();

    try {
        String line;
        while ((line = br.readLine()) != null) {
            lines.add(line);
            System.out.println(line);
        }
    } catch (IOException x) {
        JOptionPane.showMessageDialog(null, "Error: " + x.getLocalizedMessage()); //NOI18N
    }

    TreeMap<Integer, Tool> tools = new TreeMap<>();
    ArrayList<String> programs = new ArrayList<>();
    int activ_tool = -1;
    // Read all Tools with comments:
    for (int i = lines.size() - 1; i >= 0; i--) {
        String line = lines.get(i).trim();
        Matcher tool_change_command = Pattern.compile("(T)([0-9])+").matcher(line); //NOI18N

        if (line.startsWith("(") || line.startsWith(";")) { //NOI18N
            if (activ_tool >= 0) {

                Tool t = tools.get(activ_tool);
                if (line.startsWith("(")) { //NOI18N
                    line = line.substring(1, line.length() - 1);
                } else {
                    line = line.substring(1, line.length());
                }

                t.text.add(line);
                tools.put(activ_tool, t);
            }
        } else if (line.trim().startsWith("%")) { //NOI18N
            activ_tool = -1;
        } else if (tool_change_command.find()) {
            String ts = line.substring(tool_change_command.start() + 1, tool_change_command.end());
            activ_tool = Integer.parseInt(ts);
            if (!tools.containsKey(activ_tool)) {
                tools.put(activ_tool, new Tool());
            }
        } else if (line.contains("M30") || line.contains("M17") || line.contains("M2") || line.contains("M02")
                || line.contains("RET")) { //NOI18N
            activ_tool = -1;

        } else {
            activ_tool = -1;
        }
        //System.out.println("line=" + line);
    }
    boolean is_header = false;
    ArrayList<String> header = new ArrayList<>();
    // Read the Comments at the beginning of the file:
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i).trim();

        if (line.trim().startsWith("%")) { //NOI18N
            is_header = true;
            //programs.add(line.replaceAll(" ", "")); //NOI18N
            programs.add(this.parse_progname(line));

            //header.add(line.replaceAll(" ", "")); //NOI18N
        } else if (line.trim().startsWith("(") || line.trim().startsWith(";")) { //NOI18N
            if (is_header) {
                if (line.trim().startsWith("(")) { //NOI18N
                    line = line.trim().substring(1, line.length() - 1);
                } else {
                    line = line.trim().substring(1, line.length());
                }
                if (line.startsWith("$PATH=/_N_") || line.length() < 1) { //NOI18N

                } else {
                    header.add(line.trim());
                }

            }
        } else {
            is_header = false;
        }

    }

    try {
        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("dd.MM.yyyy"); //NOI18N

        System.out.println("Current Date: " + ft.format(dNow)); //NOI18N
        InputStream in = CreateMachineToolConfSheetAction.class
                .getResourceAsStream("/org/roiderh/machinetoolconfsheet/resources/base_document.docx"); //NOI18N

        XWPFDocument document = new XWPFDocument(in);
        //Write the Document in file system
        File tempFile = File.createTempFile("NcToolSettings_", ".docx"); //NOI18N
        try (FileOutputStream out = new FileOutputStream(tempFile)) {
            XWPFTable table = document.getTableArray(0);

            XWPFParagraph title = document.getParagraphArray(0);
            XWPFRun run = title.createRun();
            run.setText(org.openide.util.NbBundle.getMessage(CreateMachineToolConfSheetAction.class,
                    "MachineToolConfSheet"));
            title = document.getParagraphArray(1);
            run = title.createRun();
            run.setText(org.openide.util.NbBundle.getMessage(CreateMachineToolConfSheetAction.class, "Tools"));

            String prog = String.join(", ", programs); //NOI18N
            table.getRow(0).getCell(0).setText(
                    org.openide.util.NbBundle.getMessage(CreateMachineToolConfSheetAction.class, "ProgNr"));
            table.getRow(0).getCell(1).setText(prog);

            table.getRow(1).getCell(0).setText(
                    org.openide.util.NbBundle.getMessage(CreateMachineToolConfSheetAction.class, "Filename"));
            table.getRow(1).getCell(1).setText(path);

            table.getRow(2).getCell(0).setText(
                    org.openide.util.NbBundle.getMessage(CreateMachineToolConfSheetAction.class, "Date"));
            table.getRow(2).getCell(1).setText(ft.format(dNow));

            ArrayList<ArrayList<String>> table_text = new ArrayList<>();
            for (int i = 0; i < header.size(); i++) {

                ArrayList<String> line = new ArrayList<>();
                String name; // first column
                String desc; // second column

                int splitpos = header.get(i).indexOf(":");//NOI18N
                if (splitpos > 1 && splitpos < 25) {
                    name = header.get(i).substring(0, splitpos).trim();
                    desc = header.get(i).substring(splitpos + 1).trim();
                } else {
                    name = "";//NOI18N
                    desc = header.get(i).trim();
                }
                line.add(name);
                line.add(desc);

                table_text.add(line);

            }
            XWPFTableRow tableRowHeader;
            //tableRowHeader = table.createRow();
            tableRowHeader = null;
            XWPFRun run_table;
            String prev_name = "dummy_1234567890sadfsaf"; //NOI18N
            for (int i = 0; i < table_text.size(); i++) {
                String name = table_text.get(i).get(0);
                String desc = table_text.get(i).get(1);

                if (name.length() > 0) {
                    tableRowHeader = table.createRow();
                    run_table = tableRowHeader.getCell(1).getParagraphs().get(0).createRun();
                    tableRowHeader.getCell(0).setText(name);
                    run_table.setText(desc);
                } else if (prev_name.length() > 0 && name.length() == 0) {
                    tableRowHeader = table.createRow();
                    run_table = tableRowHeader.getCell(1).getParagraphs().get(0).createRun();
                    tableRowHeader.getCell(0).setText(""); //NOI18N                    
                    run_table.setText(desc);
                } else if (prev_name.length() == 0 && name.length() == 0) {
                    if (tableRowHeader == null) {
                        tableRowHeader = table.createRow();
                    }
                    run_table = tableRowHeader.getCell(1).getParagraphs().get(0).createRun();
                    run_table.addBreak();
                    run_table.setText(desc);

                }
                prev_name = name;
            }

            table = document.getTableArray(1);
            boolean first_line = true;
            Set keys = tools.keySet();
            for (Iterator i = keys.iterator(); i.hasNext();) {
                int toolnr = (Integer) i.next();
                Tool t = tools.get(toolnr);
                XWPFTableRow tableRowTwo;
                if (first_line) {
                    tableRowTwo = table.getRow(0);
                    first_line = false;
                } else {
                    tableRowTwo = table.createRow();
                }
                tableRowTwo.getCell(0).setText("T" + String.valueOf(toolnr)); //NOI18N

                // The lines are in the reverse order, therfore reordering:
                for (int j = t.text.size() - 1; j >= 0; j--) {
                    XWPFRun run_tool = tableRowTwo.getCell(1).getParagraphs().get(0).createRun();
                    run_tool.setText(t.text.get(j));
                    if (j > 0) {
                        run_tool.addBreak();
                    }
                }
            }

            document.write(out);
        }
        System.out.println("create_table.docx written successully"); //NOI18N

        Runtime rt = Runtime.getRuntime();
        String os = System.getProperty("os.name").toLowerCase();//NOI18N
        String[] command = new String[2];
        //command[0] = "soffice";
        Preferences pref = NbPreferences.forModule(WordProcessingProgramPanel.class);
        command[0] = pref.get("executeable", "").trim();//NOI18N
        command[1] = tempFile.getCanonicalPath();
        File f = new File(command[0]);
        if (!f.exists()) {
            JOptionPane.showMessageDialog(null, "Error: program not found: " + command[0]); //NOI18N
            return;
        }

        Process proc = rt.exec(command); //NOI18N
        //System.out.println("ready created: " + tempFile.getCanonicalPath()); //NOI18N

    } catch (IOException | MissingResourceException ex) {
        JOptionPane.showMessageDialog(null, "Error: " + ex.getLocalizedMessage()); //NOI18N
    }

}

From source file:ParserDoc.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);/* w ww  .j  a  va  2s .c o  m*/
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    //  r2.setStrikeThrough(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    //  r3.setStrikeThrough(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    // p3.setWordWrapped(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    // p3.setSpacingBetween(15, LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
    r4.setItalic(true);
    //This would imply that this break shall be treated as a simple line break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("simple.docx");
    doc.write(out);
    out.close();
    // out.close ();
}