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

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

Introduction

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

Prototype

public void addBreak() 

Source Link

Document

Specifies that a break shall be placed at the current location in the run content.

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
public void generateWorkOrderWordDoc(long id) {
    try {/*from  w  w w . j a va2  s. c  om*/
        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  a v a 2s . 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 {/*ww w .  j a  v  a2 s. c  o  m*/
        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.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 ww  w. j  ava 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 ww .j a va 2s . co  m
        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.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  ww w. j ava 2 s .  c om*/
            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.SimpleImages.java

License:Apache License

public static void main(String[] args) throws IOException, InvalidFormatException {
    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 {/*  w  w w  .ja va  2s  .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();
    doc.close();
}

From source file:com.min.word.core.MakeWordFileTest.java

License:Apache License

public static void main(String[] args) throws Exception {
    String fileName = "test.docx";
    System.out.println("---------- Word Create Start ------------");
    //  word ? ?/*from  www  .  ja  v a  2  s.  co m*/
    XWPFDocument document = new XWPFDocument();
    FileOutputStream out = new FileOutputStream(new File(fileName));
    System.out.println("---------- Create Blank Success ------------");

    //Paragraph ?
    XWPFParagraph paragraph = document.createParagraph();
    System.out.println("---------- Create Paragraph Success ------------");

    //border ?
    paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
    System.out.println("---------- Create Border Success ------------");

    XWPFRun run = paragraph.createRun();
    run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning "
            + "purpose in the domains of Academics, Information "
            + "Technology, Management and Computer Programming Languages.");
    System.out.println("---------- Text Write to File ------------");

    //Table ?
    XWPFTable table = document.createTable();
    //row
    XWPFTableRow rowOne = table.getRow(0);
    rowOne.getCell(0).setText("Col One, Row One");
    rowOne.addNewTableCell().setText("Col Tow, Row One");
    rowOne.addNewTableCell().setText("Col Three, Row One");
    //row
    XWPFTableRow rowTow = table.createRow();
    rowTow.getCell(0).setText("Col One, Row Tow");
    rowTow.getCell(1).setText("Col Tow, Row Tow");
    rowTow.getCell(2).setText("Col Three, Row Tow");
    //row
    XWPFTableRow rowThree = table.createRow();
    rowThree.getCell(0).setText("Col One, Row Three");
    rowThree.getCell(1).setText("Col Tow, Row Three");
    rowThree.getCell(2).setText("Col Three, Row Three");
    System.out.println("---------- Create Table Success ------------");

    //Add Image
    XWPFParagraph imageParagraph = document.createParagraph();
    XWPFRun imageRun = imageParagraph.createRun();
    imageRun.addPicture(new FileInputStream("test.png"), XWPFDocument.PICTURE_TYPE_PNG, "test.png",
            Units.toEMU(300), Units.toEMU(300));
    System.out.println("---------- Create Image Success ------------");

    //Hyperlink
    XWPFParagraph hyperlink = document.createParagraph();
    String id = hyperlink.getDocument().getPackagePart()
            .addExternalRelationship("http://niee.kr", XWPFRelation.HYPERLINK.getRelation()).getId();
    CTR ctr = CTR.Factory.newInstance();
    CTHyperlink ctHyperlink = hyperlink.getCTP().addNewHyperlink();
    ctHyperlink.setId(id);

    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue("Hyper-Link TEST");
    ctr.setTArray(new CTText[] { ctText });

    // ???? ?
    CTColor color = CTColor.Factory.newInstance();
    color.setVal("0000FF");
    CTRPr ctrPr = ctr.addNewRPr();
    ctrPr.setColor(color);
    ctrPr.addNewU().setVal(STUnderline.SINGLE);

    // 
    CTFonts fonts = ctrPr.isSetRFonts() ? ctrPr.getRFonts() : ctrPr.addNewRFonts();
    fonts.setAscii("?? ");
    fonts.setEastAsia("?? ");
    fonts.setHAnsi("?? ");

    // ? 
    CTHpsMeasure sz = ctrPr.isSetSz() ? ctrPr.getSz() : ctrPr.addNewSz();
    sz.setVal(new BigInteger("24"));
    ctHyperlink.setRArray(new CTR[] { ctr });
    hyperlink.setAlignment(ParagraphAlignment.LEFT);
    hyperlink.setVerticalAlignment(TextAlignment.CENTER);
    System.out.println("---------- Create Hyperlink Success ------------");

    //Font style
    XWPFParagraph fontStyle = document.createParagraph();

    //set Bold an Italic
    XWPFRun boldAnItalic = fontStyle.createRun();
    boldAnItalic.setBold(true);
    boldAnItalic.setItalic(true);
    boldAnItalic.setText("Bold an Italic");
    boldAnItalic.addBreak();

    //set Text Position
    XWPFRun textPosition = fontStyle.createRun();
    textPosition.setText("Set Text Position");
    textPosition.setTextPosition(100);

    //Set Strike through and font Size and Subscript
    XWPFRun otherStyle = fontStyle.createRun();
    otherStyle.setStrike(true);
    otherStyle.setFontSize(20);
    otherStyle.setSubscript(VerticalAlign.SUBSCRIPT);
    otherStyle.setText(" Set Strike through and font Size and Subscript");
    System.out.println("---------- Set Font Style ------------");

    //Set Alignment Paragraph
    XWPFParagraph alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.RIGHT);

    XWPFRun alignRight = alignment.createRun();
    alignRight.setText(
            "At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning "
                    + "purpose in the domains of Academics, Information "
                    + "Technology, Management and Computer Programming " + "Languages.");

    //Alignment to Center
    alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun alignCenter = alignment.createRun();
    alignCenter.setText("The endeavour started by Mohtashim, an AMU "
            + "alumni, who is the founder and the managing director "
            + "of Tutorials Point (I) Pvt. Ltd. He came up with the "
            + "website tutorialspoint.com in year 2006 with the help"
            + "of handpicked freelancers, with an array of tutorials"
            + " for computer programming languages. ");
    System.out.println("---------- Set Alignment ------------");

    //word ? 
    document.write(out);
    out.close();
    System.out.println("---------- Save File Name : " + fileName + " ------------");
    System.out.println("---------- Word Create End ------------");
}

From source file:demo.poi.image.SimpleImages.java

License:Apache License

static void writeImageToDoc(String[] args) throws InvalidFormatException, IOException, FileNotFoundException {
    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 {//www . ja  v a2 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();
    // doc.close();
}

From source file:edu.gatech.pmase.capstone.awesome.impl.output.DisasterResponseTradeStudyOutputer.java

License:Open Source License

/**
 * Creates the report details paragraph.
 *
 * @param xdoc the document to create the paragraph in
 *///from  w  w  w.  jav  a 2s. c o m
private void createReportDetails(final XWPFDocument xdoc) {
    final Locale currentLocale = Locale.getDefault();
    LOGGER.debug("Creating report details");

    final XWPFParagraph para = xdoc.getParagraphs().get(REPORT_DETAILS_ROW_INDEX);
    final XWPFRun run1 = para.createRun();
    run1.setBold(true);
    run1.setText("Date Report Generated: ");

    final XWPFRun run2 = para.createRun();
    run2.setBold(false);
    run2.setText(outputFileFormatter.format(now));
    run2.addBreak();

    final XWPFRun run3 = para.createRun();
    run3.setBold(true);
    run3.setText("Country Report Generated: ");

    final XWPFRun run4 = para.createRun();
    run4.setBold(false);
    run4.setText(currentLocale.getDisplayCountry());
}