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:com.dexter.fms.mbean.FleetMBean.java

License:Open Source License

private byte[] generateWorkOrderWordDoc(WorkOrder workder, Vendor vendor) {
    byte[] data = null;
    try {//  w  w  w. j  a v a2 s  .c o m
        if (workder != null) {
            GeneralDAO gDAO = new GeneralDAO();

            XWPFDocument document = new XWPFDocument();

            XWPFParagraph paragraphOne = document.createParagraph();
            paragraphOne.setAlignment(ParagraphAlignment.LEFT);
            /*paragraphOne.setBorderBottom(Borders.SINGLE);
            paragraphOne.setBorderTop(Borders.SINGLE);
            paragraphOne.setBorderRight(Borders.SINGLE);
            paragraphOne.setBorderLeft(Borders.SINGLE);
            paragraphOne.setBorderBetween(Borders.SINGLE);
            */
            XWPFRun paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Work Order No. - " + workder.getWorkOrderNumber());
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Type - " + workder.getWorkOrderType());
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Prepared by - " + workder.getCreatedBy().getPersonel().getFirstname()
                    + " " + workder.getCreatedBy().getPersonel().getLastname());
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Prepared on - " + workder.getCrt_dt());
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Description - " + workder.getSummaryDetailsOfWorkOrder());
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Status - BIDDING");
            paragraphOneRunOne.addBreak();

            paragraphOneRunOne = paragraphOne.createRun();
            paragraphOneRunOne.setBold(true);
            paragraphOneRunOne.setItalic(true);
            paragraphOneRunOne.setText("Vendor - " + vendor.getName());
            paragraphOneRunOne.addBreak();

            XWPFParagraph paragraph2 = document.createParagraph();
            paragraph2.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun paragraph2Run = paragraph2.createRun();
            paragraph2Run.setBold(true);
            paragraph2Run.setItalic(true);
            paragraph2Run.setUnderline(UnderlinePatterns.DOUBLE);
            paragraph2Run.setText("Vehicles");
            paragraph2Run.addBreak();

            for (WorkOrderVehicle v : workder.getVehicles()) {
                XWPFParagraph paragraph = document.createParagraph();
                paragraph.setAlignment(ParagraphAlignment.LEFT);
                XWPFRun paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                paragraphRun.setText("Registration Number: " + v.getVehicle().getRegistrationNo());
                paragraphRun.addBreak();

                paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                paragraphRun.setText("Model: " + v.getVehicle().getModel().getName() + "["
                        + v.getVehicle().getModel().getYear() + "]");
                paragraphRun.addBreak();

                paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                paragraphRun.setText("Work Required: " + v.getDetailsOfWork());
                paragraphRun.addBreak();

                paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                paragraphRun.setText("Vendor Cost: <please fill your cost here>");
                paragraphRun.addBreak();

                XWPFTable table = paragraph.getDocument().createTable();
                XWPFTableRow tableRowOne = table.getRow(0);
                addHeaderCell(tableRowOne, "Item", true);
                addHeaderCell(tableRowOne, "Type", false);
                addHeaderCell(tableRowOne, "Action", false);
                addHeaderCell(tableRowOne, "Count", false);

                addHeaderCell(tableRowOne, "Vendor Cost", false);
                addHeaderCell(tableRowOne, "Comment", false);

                for (WorkOrderItem itm : v.getItems()) {
                    XWPFTableRow tableRow = table.createRow();
                    tableRow.getCell(0).setText(itm.getItem().getName());
                    tableRow.getCell(1).setText(itm.getItem().getType().getName());
                    tableRow.getCell(2).setText(itm.getAction());
                    tableRow.getCell(3).setText(String.valueOf(itm.getCount()));

                    tableRow.getCell(4).setText("");
                    tableRow.getCell(5).setText("");
                }
            }
            gDAO.destroy();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            document.write(baos);

            data = baos.toByteArray();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return data;
}

From source file:com.dexter.fms.mbean.ReportsMBean.java

public void createWord(int type, String filename) {
    try {// w w w  .  ja  va2s.  c om
        FacesContext context = FacesContext.getCurrentInstance();
        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        paragraphOne.setBorderBottom(Borders.SINGLE);
        paragraphOne.setBorderTop(Borders.SINGLE);
        paragraphOne.setBorderRight(Borders.SINGLE);
        paragraphOne.setBorderLeft(Borders.SINGLE);
        paragraphOne.setBorderBetween(Borders.SINGLE);

        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setItalic(true);
        paragraphOneRunOne.setText(getReport_title());
        paragraphOneRunOne.addBreak();

        exportWordTable(type, document);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        document.write(baos);

        String fileName = filename + ".docx";

        writeFileToResponse(context.getExternalContext(), baos, fileName,
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

        context.responseComplete();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.ecm.poi.SimpleDocument.java

License:Apache License

@Test
public void xwpfTest() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);//from w  w  w.  ja v  a2 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("d:\\temp\\simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.ecm.poi.SimpleImages.java

License:Apache License

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

    XWPFRun r = p.createRun();
    //        args[0] = "d:\\temp\\Koala.jpg";
    String imgFile = "d:\\temp\\img\\Koala.jpg";
    if (null != imgFile) {
        int format = 0;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {/*from  w ww  .  jav  a  2s . c o  m*/
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("d:\\temp\\images.docx");
    doc.write(out);
    out.close();
}

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

public void poiAddPictureTest() {
    try {/*from  w w  w  .  ja  v a  2  s  .  c  om*/
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();

        XWPFRun run = paragraph.createRun();
        int format;

        String imgFilePath = "C://Users//user//Pictures//forward.png";

        format = XWPFDocument.PICTURE_TYPE_PNG;
        run.setText(imgFilePath);
        run.addBreak();
        run.setText("This is where a picture would be.\r\nAnd here would be another image");
        run.addBreak(BreakType.PAGE);

        FileOutputStream out = new FileOutputStream("C://Users//user//Desktop//images.docx");
        byte[] byts = new byte[1000000];
        out.write(byts);
        document.addPictureData(byts, format);
        document.write(out);
        out.close();
    } catch (Exception e) {

    }
}

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

public static void fillWordDocument(XWPFDocument doc) {
    try {//from  ww  w  .ja v a  2  s  . com
        XWPFParagraph p1 = doc.createParagraph();
        p1.setAlignment(ParagraphAlignment.CENTER);
        p1.setBorderBottom(Borders.DOUBLE);
        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.BOTH);
        p3.setSpacingLineRule(LineSpacingRule.EXACT);

        p3.setIndentationFirstLine(600);

        XWPFRun r4 = p3.createRun();
        r4.setTextPosition(200);
        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"
                + ".......");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordLabel.java

License:Apache License

@Override
public void write(IMSWordContainer container) {
    try {// w w w.  j ava2 s.co m
        XWPFParagraph paragraph = container.insertParagraph();
        XWPFRun run = paragraph.createRun();
        applyStyleToFocWordLabel(run, paragraph);

        //         FImageProperty imageProperty = (FImageProperty) getParent().getFocData().iFocData_getDataByPath(getXmlAttribute().getValue("name"));
        //         
        //         BufferedImage bufferedImage = imageProperty.getImageValue();
        //         ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
        //         ImageIO.write(bufferedImage, "png", imagebuffer);
        //
        //         InputStream inputStream = new ByteArrayInputStream(imagebuffer.toByteArray());
        //      addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
        //         run.addPicture(inputStream, 0, "temp", 20, 20);

        run.setText(value, 0);
    } catch (Exception e) {
        Globals.logException(e);
    }
}

From source file:com.gezipu360.cashier.bean.word.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 w w  .  j ava  2 s.  com*/
    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("simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.gezipu360.cashier.bean.word.SimpleImages.java

License:Apache License

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

    XWPFRun r = p.createRun();

    for (String imgFile : args) {
        int format;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {//from w w  w  .j a  v  a  2  s .c o  m
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

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

From source file:com.glodon.tika.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   www  . j  av  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.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.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("simple.docx");
    doc.write(out);
    out.close();
    doc.close();
}