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:nl.architolk.ldt.processors.WordSerializer.java

License:Open Source License

protected void readInput(final PipelineContext pipelineContext, final ProcessorInput input, Config config,
        OutputStream outputStream) {

    try {//from w  w w. j av  a  2 s.  c  om
        // Test
        BigInteger markId = BigInteger.ONE;
        // Read the input as a DOM
        final Document domDocument = readInputAsDOM(pipelineContext, input);

        // create document (docx)
        XWPFDocument doc = new XWPFDocument();

        //iterate through paragraphs;
        NodeList paragraphNodes = domDocument.getElementsByTagName("p");

        for (short i = 0; i < paragraphNodes.getLength(); i++) {
            Node paragraphNode = paragraphNodes.item(i);
            if (paragraphNode.getNodeType() == Node.ELEMENT_NODE) {
                //Create new paragraph
                XWPFParagraph paragraph = doc.createParagraph();

                //iterate through paragraph parts
                NodeList textNodes = paragraphNode.getChildNodes();
                for (short r = 0; r < textNodes.getLength(); r++) {
                    Node textNode = textNodes.item(r);
                    if (textNode.getNodeType() == Node.TEXT_NODE) {
                        XWPFRun run = paragraph.createRun();
                        run.setText(textNode.getTextContent());
                    }
                    if (textNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element textElement = (Element) textNode;
                        if (textNode.getLocalName().toUpperCase().equals("B")) {
                            //Eigenlijk op een andere plaats, maar nu ff voor de test
                            String anchor = textElement.getAttribute("id");
                            if (!anchor.isEmpty()) {
                                CTBookmark bookStart = paragraph.getCTP().addNewBookmarkStart();
                                bookStart.setName(anchor);
                                bookStart.setId(markId);
                            }
                            XWPFRun run = paragraph.createRun();
                            run.setBold(true);
                            run.setText(textNode.getTextContent());
                            if (!anchor.isEmpty()) {
                                CTMarkupRange bookEnd = paragraph.getCTP().addNewBookmarkEnd();
                                bookEnd.setId(markId);
                                markId = markId.add(BigInteger.ONE);
                            }
                        } else if (textNode.getLocalName().toUpperCase().equals("A")) {
                            addHyperlink(paragraph, textNode.getTextContent(),
                                    textElement.getAttribute("href"));
                        } else {
                            XWPFRun run = paragraph.createRun();
                            run.setText(textNode.getTextContent());
                        }
                    }
                }
            }
        }
        // write workbook to stream
        doc.write(outputStream);
        outputStream.close();

    } catch (Exception e) {
        throw new OXFException(e);
    }

}

From source file:nl.detoren.ijc.io.OutputIntekenlijst.java

License:Open Source License

public boolean export(Groepen groepen) {
    try {/*from  ww  w .j a v  a2s. c  o m*/
        logger.log(Level.INFO, "Wedstrijden wegschrijven naar Excel");
        FileInputStream file = new FileInputStream("Leeg.docx");
        XWPFDocument document = new XWPFDocument(file);
        XWPFParagraph paragraph = document.getLastParagraph();
        setDoubleLineSpacing(paragraph);
        XWPFRun run = paragraph.createRun();
        run.setFontFamily("Courier New");
        run.setFontSize(12);
        String result;
        result = "Intekenlijst aangemaakt met " + IJCController.c().appTitle + " voor "
                + IJCController.c().verenigingNaam;
        run.setText(result);
        run.addCarriageReturn(); // separate previous text from break
        for (int i = 0; i < groepen.getAantalGroepen(); ++i) {
            if (i >= 1)
                run.addBreak();
            Groep groep = groepen.getGroepById(i);
            result = "Stand na " + groepen.getRonde() + "e ronde, " + groepen.getPeriode();
            result += "e periode                " + groep.getNaam() + " (" + groep.getSpelers().size() + ")\n";
            run.setText(result);
            run.addBreak();
            result = "    Naam                           ini   zw rating  gespeeld tegen  pnt\n";
            run.setText(result);
            run.addBreak();
            result = "-----------------------------------------------------------------------\n";
            run.setText(result);
            run.addBreak();
            for (Speler s : groep.getSpelers()) {
                result = s.toPrintableString(false);
                run.setText(result);
                run.addBreak();
            }

            if (IJCController.c().exportDoorschuivers) {
                int ndoor = IJCController.c().bepaalAantalDoorschuiversVolgendeRonde(groep.getNiveau(),
                        groepen.getPeriode(), groepen.getRonde());
                if (i - 1 >= 0) {
                    run.setText(IJCController.c().exportDoorschuiversStart + "\n");
                    run.addBreak();
                    Groep lager = groepen.getGroepById(i - 1);
                    if (ndoor > 1) {
                        for (int j = 0; j < ndoor; j++) {
                            Speler s = lager.getSpelerByID(j + 1);
                            run.setText(s.toPrintableString(false, true) + "\n");
                            run.addBreak();
                        }
                        run.setText(IJCController.c().exportDoorschuiversStop + "\n" + "\n");
                    } else {
                        // Bij n doorschuiver, alleen doorschuiven als
                        // kampioen
                        Speler s1 = lager.getSpelerByID(1);
                        Speler s2 = lager.getSpelerByID(2);
                        if ((s2 != null) && ((s1.getPunten() - s2.getPunten()) > 4)) {
                            run.setText(s1.toPrintableString(false, true) + "\n");
                            run.addBreak();
                        }

                    }
                }
            }
            run.addCarriageReturn(); // separate previous text from break
            run.addBreak(BreakType.PAGE);
        }
        // Close input file
        file.close();
        // Store Excel to new file
        String dirName = "R" + groepen.getPeriode() + "-" + groepen.getRonde();
        new File(dirName).mkdirs();
        String filename = dirName + File.separator + "IntekenlijstR" + groepen.getPeriode() + "-"
                + groepen.getRonde() + ".docx";
        File outputFile = new File(filename);
        FileOutputStream outFile = new FileOutputStream(outputFile);
        document.write(outFile);
        // Close output file
        document.close();
        outFile.close();
        // And open it in the system editor
        //Desktop.getDesktop().open(new File(outputFile));
        return true;
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Export mislukt :" + ex.getMessage());
        Utils.stacktrace(ex);
        return false;
    }
}

From source file:offishell.word.Word.java

License:MIT License

/**
 * <p>/*  w w  w . ja v  a 2 s. com*/
 * Create header with the specified text.
 * </p>
 * 
 * @param headerText A header text.
 */
public Word header(String headerText) {
    try {
        CTSectPr section = calculated.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(calculated, section);
        XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
        XWPFParagraph para = header.createParagraph();
        XWPFRun run = para.createRun();
        run.setText(headerText);
        styles().base().apply(run);
    } catch (Exception e) {
        throw I.quiet(e);
    }
    return this;
}

From source file:offishell.word.Word.java

License:MIT License

/**
 * <p>/* w w w .j a va2  s. c  o m*/
 * Add new paragraph.
 * </p>
 * 
 * @param text A paragraph text.
 * @return
 */
public Word text(String text) {
    XWPFParagraph para = createParagraph();
    XWPFRun run = para.createRun();
    run.setText(text, 0);
    styles().base().apply(run);

    return this;
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*from   w w  w . j  a  v  a2s.com*/
 * Helper method to clone {@link XWPFParagraph}.
 * </p>
 * 
 * @param in
 * @param out
 * @param converter
 */
public static void copy(XWPFParagraph in, XWPFParagraph out, UnaryOperator<String> converter) {
    // copy context
    ppr(out).set(in.getCTP().getPPr());

    // copy(doc, out.getDocument(), doc.getStyles().getStyle(in.getStyleID()));

    // copy children
    for (XWPFRun inRun : in.getRuns()) {
        copy(inRun, out.createRun(), converter);
    }
}

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 {/* ww  w.  j a va 2  s  .  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

public void insertString(String text, int offset) {
    try {//w ww  .j a v  a  2 s .  c o m
        currentOffset = 0;
        StringTokenizer stText = new StringTokenizer(text, "\n", true);
        while (stText.hasMoreTokens()) {
            String textPart = stText.nextToken();
            DocumentPosition position = searchPart(document.getBodyElements(), offset);
            if (position != null) {
                String oldText = position.text.getStringValue();
                String newText = oldText.substring(0, position.offsetInText) + textPart
                        + (position.offsetInText == oldText.length() ? ""
                                : oldText.substring(position.offsetInText));
                position.text.setStringValue(newText);
                offset += textPart.length();
            } else {
                XWPFParagraph paragraph = (XWPFParagraph) document.getBodyElements()
                        .get(document.getBodyElements().size() - 1);
                paragraph.createRun().setText(textPart);
            }
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}

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

License:Apache License

DocumentPosition createRun(XWPFParagraph paragraph) {
    XWPFRun run = paragraph.createRun();
    DocumentPosition position = new DocumentPosition();
    position.run = run;//  w w  w  .ja v a 2  s. c  om
    position.text = run.getCTR().addNewT();
    return position;
}

From source file:org.cgiar.ccafs.marlo.utils.POISummary.java

License:Open Source License

/**
 * Head 1 Title/*www .ja v a  2s. c om*/
 * 
 * @param h1
 * @param text
 */
public void textHead1Title(XWPFParagraph h1, String text) {
    h1.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun h1Run = h1.createRun();
    this.addParagraphTextBreak(h1Run, text);
    h1Run.setColor(TITLE_FONT_COLOR);
    h1Run.setBold(true);
    h1Run.setFontFamily(FONT_TYPE);
    h1Run.setFontSize(16);
}

From source file:org.cgiar.ccafs.marlo.utils.POISummary.java

License:Open Source License

public void textHead2Title(XWPFParagraph h2, String text) {
    h2.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun h2Run = h2.createRun();
    this.addParagraphTextBreak(h2Run, text);
    h2Run.setColor(TITLE_FONT_COLOR);/*from   w  ww.  j  a  v a  2 s.com*/
    h2Run.setBold(true);
    h2Run.setFontFamily(FONT_TYPE);
    h2Run.setFontSize(14);
}