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

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

Introduction

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

Prototype

@Override
public void setItalic(boolean value) 

Source Link

Document

Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a document

This formatting property is a toggle property, which specifies that its behavior differs between its use within a style definition and its use as direct formatting.

Usage

From source file:AkashApplications.src.GenerateInvoice.java

public boolean printInvoice(boolean printCommand)
        throws FileNotFoundException, InvalidFormatException, IOException {

    XWPFDocument document = new XWPFDocument();
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(460L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(460L));

    XWPFParagraph dateP = document.createParagraph();
    XWPFRun dateRun = dateP.createRun();
    dateRun.addBreak();/*from   ww w  . j  ava  2 s.c  om*/
    dateRun.setText(copyType);
    dateRun.setFontSize(8);
    dateRun.setItalic(true);
    dateP.setAlignment(ParagraphAlignment.RIGHT);
    dateRun.addBreak();

    XWPFParagraph title = document.createParagraph();
    XWPFRun titleRun = title.createRun();
    titleRun.addBreak();
    titleRun.setText("Delivery Challan");
    titleRun.setBold(true);
    title.setAlignment(ParagraphAlignment.CENTER);
    titleRun.addBreak();

    //header table
    XWPFTable productTable = document.createTable();
    productTable.setCellMargins(50, 50, 50, 50);
    productTable.getCTTbl().getTblPr().unsetTblBorders();
    productTable.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000));

    XWPFTableRow row1 = productTable.getRow(0);

    XWPFTableCell cellSellerBuyer = row1.getCell(0);
    cellSellerBuyer.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(5000));

    CTTc ctTc = cellSellerBuyer.getCTTc();
    CTTcPr tcPr = ctTc.getTcPr();
    CTTcBorders border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    //        border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph seller = cellSellerBuyer.addParagraph();
    XWPFRun sellerRun = seller.createRun();
    seller.setAlignment(ParagraphAlignment.LEFT);
    sellerRun.setText("Buyer :");
    sellerRun.addBreak();
    String[] ar = buyerAddress.split("\n");
    for (String s : ar) {
        sellerRun.setText(s);
        sellerRun.addBreak();
    }

    XWPFTableCell cellProductDesc = row1.createCell();
    cellProductDesc.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6000));

    ctTc = cellProductDesc.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    //border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph productDesc = cellProductDesc.addParagraph();
    productDesc.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun runInvoice = productDesc.createRun();

    //        if(table.getRowCount() == 1)
    //        {
    //            runInvoice.addTab();
    //            runInvoice.addTab();
    //            runInvoice.addTab();
    //            String imgLoc = "Barcodes/"+ dtm.getValueAt(0, 6) +".jpg";
    //            FileInputStream image = new FileInputStream(imgLoc);
    //            runInvoice.addPicture(image, XWPFDocument.PICTURE_TYPE_JPEG,imgLoc, Units.toEMU(200), Units.toEMU(50));
    //            runInvoice.addBreak();
    //            runInvoice.addBreak();
    //        }

    runInvoice.setText("Delivery Note : ");
    runInvoice.addTab();
    runInvoice.setText(invoiceNote);
    runInvoice.addBreak();
    runInvoice.addBreak();
    runInvoice.addBreak();
    runInvoice.setText("Date : ");
    runInvoice.addTab();
    runInvoice.setText(date);
    runInvoice.addBreak();
    runInvoice.addBreak();
    //document.createParagraph().createRun().addBreak();

    XWPFTable productDetails = document.createTable();
    productDetails.setCellMargins(50, 50, 50, 50);
    //productDetails.getCTTbl().getTblPr().addNewJc().setVal(STJc.RIGHT);
    productDetails.getCTTbl().getTblPr().unsetTblBorders();
    productDetails.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000));

    XWPFTableRow pDetailHeader = productDetails.getRow(0);

    XWPFTableCell header0 = pDetailHeader.getCell(0);
    header0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
    XWPFParagraph headerText = header0.addParagraph();
    XWPFRun run = headerText.createRun();
    run.setText("Sl. No.");
    run.setBold(true);
    ctTc = header0.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFTableCell header1 = pDetailHeader.createCell();
    header1.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
    XWPFParagraph headerText1 = header1.addParagraph();
    XWPFRun run1 = headerText1.createRun();
    run1.setText("Description of Goods");
    run1.setBold(true);
    ctTc = header1.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFTableCell header4 = pDetailHeader.createCell();
    header4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
    XWPFParagraph headerText4 = header4.addParagraph();
    XWPFRun run4 = headerText4.createRun();
    run4.setText("Quantity");
    run4.setBold(true);
    ctTc = header4.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    for (int i = 0; i < 1; i++) {
        XWPFTableRow fTableRow = productDetails.createRow();
        for (int j = 0; j < 4; j++) {
            XWPFTableCell cell = fTableRow.getCell(j);
            try {
                ctTc = cell.getCTTc();
                tcPr = ctTc.addNewTcPr();
                border = tcPr.addNewTcBorders();
                //border.addNewBottom().setVal(STBorder.SINGLE);
                border.addNewRight().setVal(STBorder.SINGLE);
                border.addNewLeft().setVal(STBorder.SINGLE);
                if (i == 0)
                    border.addNewTop().setVal(STBorder.SINGLE);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

            switch (j) {
            case 0:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
                break;
            case 1:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
                break;
            case 2:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
                break;

            }

        }
    }

    int qty = 0;

    for (int i = 0; i < table.getRowCount(); i++) {
        XWPFTableRow fTableRow = productDetails.createRow();
        for (int j = 0; j < table.getColumnCount() - 1; j++) {
            XWPFTableCell cell = fTableRow.getCell(j);
            try {
                ctTc = cell.getCTTc();
                tcPr = ctTc.addNewTcPr();
                border = tcPr.addNewTcBorders();
                if (i == table.getRowCount() - 1)
                    border.addNewBottom().setVal(STBorder.SINGLE);
                border.addNewRight().setVal(STBorder.SINGLE);
                border.addNewLeft().setVal(STBorder.SINGLE);
                //                    border.addNewTop().setVal(STBorder.SINGLE); 
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

            switch (j) {
            case 0:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                break;
            case 1:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                break;
            case 2:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                qty += Integer.parseInt(String.valueOf(dtm.getValueAt(i, j)));
                break;

            }

        }
    }

    XWPFTableRow finalRow = productDetails.createRow();
    XWPFTableCell fc0 = finalRow.getCell(0);
    fc0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));

    XWPFTableCell fc3 = finalRow.getCell(1);
    fc3.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
    XWPFParagraph fp1 = fc3.addParagraph();
    XWPFRun fRun1 = fp1.createRun();
    fRun1.setBold(true);
    fRun1.setText("TOTAL");

    XWPFTableCell fc4 = finalRow.getCell(2);
    fc4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
    XWPFParagraph fp2 = fc4.addParagraph();
    XWPFRun fRun2 = fp2.createRun();
    fRun2.setBold(true);
    fRun2.setText(String.valueOf(qty));

    ctTc = fc0.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    //border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    ctTc = fc3.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    //border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    ctTc = fc4.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph netPMethod = document.createParagraph();
    XWPFRun pMethodRun = netPMethod.createRun();
    pMethodRun.addBreak();
    pMethodRun.addBreak();
    pMethodRun.setItalic(true);
    pMethodRun.setFontSize(10);
    pMethodRun.setText("NB. - Goods sold to the above buyer is in " + paymentType + ".");

    try {

        File f = new File(filePath);
        if (!f.exists()) {
            f.mkdir();
        }
        FileOutputStream outputStream = new FileOutputStream(filePath + File.separator + "Challan"
                + invoiceNote.replace("/", "_") + copyType.replace("/", "_") + ".docx");
        document.write(outputStream);
        outputStream.close();
        new InvoiceNoteManager().setProperty();
        if (printCommand)
            printDocument();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

    return true;
}

From source file:ch.admin.searchreplace.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  va2 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("simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.deepoove.poi.policy.TextRenderPolicy.java

License:Apache License

private void styleRun(XWPFRun run, Style style) {
    if (null != style) {
        String color = style.getColor();
        String fontFamily = style.getFontFamily();
        int fontSize = style.getFontSize();
        Boolean bold = style.isBold();
        Boolean italic = style.isItalic();
        Boolean strike = style.isStrike();
        if (null != color)
            run.setColor(color);//from w w  w  . j  a va  2  s . c o  m
        if (0 != fontSize)
            run.setFontSize(fontSize);
        if (null != fontFamily)
            run.setFontFamily(fontFamily);
        if (null != bold)
            run.setBold(bold);
        if (null != italic)
            run.setItalic(italic);
        if (null != strike)
            run.setStrike(strike);
    }
}

From source file:com.deepoove.poi.resolver.TemplateResolver.java

License:Apache License

private static void styleRun(XWPFRun destRun, XWPFRun srcRun) {
    if (null == destRun || null == srcRun)
        return;//from ww  w.ja  v a2 s.  c om
    destRun.setBold(srcRun.isBold());
    destRun.setColor(srcRun.getColor());
    destRun.setFontFamily(srcRun.getFontFamily());
    int fontSize = srcRun.getFontSize();
    if (-1 != fontSize)
        destRun.setFontSize(fontSize);
    destRun.setItalic(srcRun.isItalic());
    destRun.setStrike(srcRun.isStrike());
    destRun.setUnderline(srcRun.getUnderline());
}

From source file:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

public static void createSimpleTable() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFTable table = doc.createTable(3, 3);

    table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

    // table cells have a list of paragraphs; there is an initial
    // paragraph created when the cell is created. If you create a
    // paragraph in the document to put in the cell, it will also
    // appear in the document following the table, which is probably
    // not the desired result.
    XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);//from  ww w. j a v a2  s.com
    r1.setText("The quick brown fox");
    r1.setItalic(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    table.getRow(2).getCell(2).setText("only text");

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

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

License:Open Source License

private void addHeaderCell(XWPFTableRow row, String text, boolean first) {
    XWPFParagraph tp = null;//from w  w  w. j a va  2 s .  c o  m
    if (first)
        tp = row.getCell(0).getParagraphs().get(0);
    else {
        tp = row.addNewTableCell().getParagraphs().get(0);
    }
    XWPFRun tpr = tp.createRun();
    tpr.setBold(true);
    tpr.setItalic(true);
    tpr.setText(text);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void generateWorkOrderWordDoc(long id) {
    try {//  www  .  ja  v  a 2  s.co m
        setSelectedWorkOrder(null);
        for (WorkOrder w : getRountineWorkOrders()) {
            if (w.getId().longValue() == id) {
                setSelectedWorkOrder(w);
                break;
            }
        }

        if (getSelectedWorkOrder() != null) {
            GeneralDAO gDAO = new GeneralDAO();

            FacesContext context = FacesContext.getCurrentInstance();
            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. - " + getSelectedWorkOrder().getWorkOrderNumber());
            paragraphOneRunOne.addBreak();

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

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

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

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

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

            if ((getSelectedWorkOrder().getStatus().equalsIgnoreCase("IN-PROGRESS")
                    || getSelectedWorkOrder().getStatus().equalsIgnoreCase("COMPLETED"))) {
                paragraphOneRunOne = paragraphOne.createRun();
                paragraphOneRunOne.setBold(true);
                paragraphOneRunOne.setItalic(true);
                if (getSelectedWorkOrder().getVendor() != null)
                    paragraphOneRunOne.setText("Vendor - " + getSelectedWorkOrder().getVendor().getName());
                else
                    paragraphOneRunOne.setText("Vendor - N/A");
                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();

            double totalCost = 0;
            for (WorkOrderVehicle v : getSelectedWorkOrder().getVehicles()) {
                VehicleRoutineMaintenance vrm = null;
                if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("IN-PROGRESS")
                        || getSelectedWorkOrder().getStatus().equalsIgnoreCase("COMPLETED")) {
                    Hashtable<String, Object> params = new Hashtable<String, Object>();
                    params.put("vehicle", v.getVehicle());
                    params.put("workOrder", getSelectedWorkOrder());

                    Object vrmObj = gDAO.search("VehicleRoutineMaintenance", params);
                    if (vrmObj != null) {
                        Vector<VehicleRoutineMaintenance> list = (Vector<VehicleRoutineMaintenance>) vrmObj;
                        vrm = list.get(0);
                    }
                }

                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();

                if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("IN-PROGRESS") && vrm != null) {
                    paragraphRun = paragraph.createRun();
                    paragraphRun.setBold(true);
                    paragraphRun.setItalic(true);
                    paragraphRun.setText("Start Date: " + vrm.getStart_dt());
                    paragraphRun.addBreak();
                } else if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("COMPLETED") && vrm != null) {
                    paragraphRun = paragraph.createRun();
                    paragraphRun.setBold(true);
                    paragraphRun.setItalic(true);
                    paragraphRun.setText("Start Date: " + vrm.getStart_dt());
                    paragraphRun.addBreak();

                    paragraphRun = paragraph.createRun();
                    paragraphRun.setBold(true);
                    paragraphRun.setItalic(true);
                    paragraphRun.setText("Close Date: " + vrm.getClose_dt());
                    paragraphRun.addBreak();
                }

                paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("NEW"))
                    paragraphRun.setText("Vendor Cost: ");
                else if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("IN-PROGRESS")) {
                    if (vrm != null) {
                        paragraphRun.setText("Vendor Cost: "
                                + NumberFormat.getInstance().format(vrm.getInitial_amount().doubleValue()));
                        totalCost += vrm.getInitial_amount().doubleValue();
                    } else
                        paragraphRun.setText("Vendor Cost: ");
                } else if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("COMPLETED")) {
                    if (vrm != null) {
                        paragraphRun.setText("Vendor Cost: "
                                + NumberFormat.getInstance().format(vrm.getClosed_amount().doubleValue()));
                        totalCost += vrm.getClosed_amount().doubleValue();
                    } else
                        paragraphRun.setText("Vendor Cost: ");
                }
                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);

                if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("NEW")) {
                    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()));
                    if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("NEW")) {
                        tableRow.getCell(4).setText("");
                        tableRow.getCell(5).setText("");
                    }
                }
            }
            gDAO.destroy();

            if (getSelectedWorkOrder().getStatus().equalsIgnoreCase("IN-PROGRESS")
                    || getSelectedWorkOrder().getStatus().equalsIgnoreCase("COMPLETED")) {
                XWPFParagraph paragraph = document.createParagraph();
                paragraph.setAlignment(ParagraphAlignment.LEFT);
                XWPFRun paragraphRun = paragraph.createRun();
                paragraphRun.setBold(true);
                paragraphRun.setItalic(true);
                paragraphRun.setText("Total Cost: " + NumberFormat.getInstance().format(totalCost));
            }

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

            String fileName = getSelectedWorkOrder().getWorkOrderNumber() + ".docx";

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

            context.responseComplete();
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed: ", "No work order selected!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR: ", ex.getMessage());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

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  av a2s .  com*/
        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 {//from  www  .  j a v a 2  s  .  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);//  ww  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("d:\\temp\\simple.docx");
    doc.write(out);
    out.close();

}