Example usage for org.apache.poi.xwpf.usermodel XWPFDocument createParagraph

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument createParagraph

Introduction

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

Prototype

public XWPFParagraph createParagraph() 

Source Link

Document

Appends a new paragraph to this document

Usage

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

License:Open Source License

private void fillLicenseList(XWPFDocument document,
        Collection<LicenseInfoParsingResult> projectLicenseInfoResults) {
    List<LicenseNameWithText> licenseNameWithTexts = OutputGenerator
            .getSortedLicenseNameWithTexts(projectLicenseInfoResults);
    XWPFRun licenseHeaderRun = document.createParagraph().createRun();
    addFormattedText(licenseHeaderRun, "License texts", FONT_SIZE + 2, true);
    addNewLines(document, 0);//from   w w  w  . j  a  v  a 2  s . c o m

    for (LicenseNameWithText licenseNameWithText : licenseNameWithTexts) {
        XWPFParagraph licenseParagraph = document.createParagraph();
        licenseParagraph.setStyle(STYLE_HEADING);
        String licenseName = licenseNameWithText.isSetLicenseName() ? licenseNameWithText.getLicenseName()
                : UNKNOWN_LICENSE_NAME;
        addBookmark(licenseParagraph, licenseName, licenseName);
        addNewLines(document, 0);
        setText(document.createParagraph().createRun(),
                nullToEmptyString(licenseNameWithText.getLicenseText()));
        addNewLines(document, 1);
    }
}

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

License:Open Source License

public static void addNewLines(XWPFDocument document, int numberOfNewlines) {
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();
    addNewLines(run, numberOfNewlines);/* ww  w  .  j av a2  s  .  co m*/
}

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

License:Open Source License

public static void addPageBreak(XWPFDocument document) {
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();
    run.addBreak(BreakType.TEXT_WRAPPING);
    run.addBreak(BreakType.PAGE);/* w  w  w.j  a  v  a  2  s  . co  m*/
}

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

License:Open Source License

public static void addBulletList(XWPFDocument document, List<String> bulletListItems,
        boolean bulletListItemsAsLink) throws XmlException {
    CTNumbering cTNumbering = CTNumbering.Factory.parse(cTAbstractNumBulletXML);
    CTAbstractNum cTAbstractNum = cTNumbering.getAbstractNumArray(0);
    XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
    XWPFNumbering numbering = document.createNumbering();
    BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
    BigInteger numID = numbering.addNum(abstractNumID);

    for (int i = 0; i < bulletListItems.size(); i++) {
        String bulletItem = bulletListItems.get(i);
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setNumID(numID);//w ww .j  a va 2s  .co  m
        if (bulletListItemsAsLink) {
            addBookmarkHyperLink(paragraph, bulletItem, bulletItem);
        } else {
            setText(paragraph.createRun(), bulletItem);
        }
        if (i < bulletListItems.size() - 1) {
            paragraph.setSpacingAfter(0);
        }
    }
}

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   www .j  av  a 2s  .c  o  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.test.VariousTest.java

License:Open Source License

@Test
public void testStaticFragmentWithFieldProcessing()
        throws InvalidFormatException, IOException, DocumentParserException, DocumentGenerationException {
    FileInputStream is = new FileInputStream("templates/test2.docx");
    OPCPackage oPackage = OPCPackage.open(is);
    XWPFDocument document = new XWPFDocument(oPackage);
    // insert some static content.
    document.createParagraph().createRun().setText("static part that will not contain any link");
    CTBookmark bookmark = document.getDocument().getBody().addNewBookmarkStart();
    bookmark.setName("bookmark1");
    bookmark.setId(new BigInteger("66"));
    document.createParagraph().createRun().setText("bookmarked part");
    CTMarkupRange range = document.getDocument().getBody().addNewBookmarkEnd();
    range.setId(new BigInteger("66"));
    document.createParagraph().createRun().setText("another static part that will not contain any link");
    // save the document in another file
    FileOutputStream fos = new FileOutputStream("results/bookmarkTest.docx");
    document.write(fos);/*from   w w  w.j  a v  a2s  .c  o  m*/
    document.close();
    fos.close();
}

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

License:Open Source License

/**
 * Creates a new {@link DocumentTemplate} with the given {@link DocumentTemplate#getBody() body}. The body is linked to {@link XWPFRun}.
 * /* w  ww  . j ava  2 s .  c o m*/
 * @param body
 *            the {@link Template}
 * @return a new {@link DocumentTemplate}
 */
public static DocumentTemplate createDocumentTemplate(Template body) {
    final DocumentTemplate res = TemplatePackage.eINSTANCE.getTemplateFactory().createDocumentTemplate();

    final XWPFDocument document = new XWPFDocument();
    res.setDocument(document);
    res.setBody(body);

    final XWPFParagraph paragraph = document.createParagraph();

    linkRuns(paragraph, body);

    return res;
}

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

License:Open Source License

/**
 * Creates a new {@link DocumentTemplate} with the given {@link DocumentTemplate#getBody() body}. The body is linked to {@link XWPFRun}.
 * /*from   w w w  .  ja  v a2  s  .  c o m*/
 * @param body
 *            the {@link Template}
 * @return a new {@link DocumentTemplate}
 */
@SuppressWarnings("resource")
public static DocumentTemplate createDocumentTemplate(Template body) {
    final DocumentTemplate res = TemplatePackage.eINSTANCE.getTemplateFactory().createDocumentTemplate();

    final XWPFDocument document = new XWPFDocument();
    res.setDocument(document);
    res.setBody(body);

    final XWPFParagraph paragraph = document.createParagraph();

    linkRuns(paragraph, body);

    return res;
}

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  .ja v a  2  s. c  om*/
    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: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);/*from ww  w . j a  v a2 s.co  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 ();
}