Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getDocument

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getDocument

Introduction

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

Prototype

@Override
    public XWPFDocument getDocument() 

Source Link

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
public void generateWorkOrderWordDoc(long id) {
    try {// ww  w  . j a  v  a  2s.  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 {//from   w ww .j  a v a2  s. 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.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   w ww . j  av  a  2s .c om*/
    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:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private TmpFormatting processRun(XWPFRun run, XWPFParagraph paragraph, XHTMLContentHandler xhtml,
        TmpFormatting tfmtg) throws SAXException, XmlException, IOException {
    // True if we are currently in the named style tag:
    if (run.isBold() != tfmtg.isBold()) {
        if (tfmtg.isItalic()) {
            xhtml.endElement("i");
            tfmtg.setItalic(false);//from   w w w .j  a  va  2 s . c  o  m
        }
        if (run.isBold()) {
            xhtml.startElement("b");
        } else {
            xhtml.endElement("b");
        }
        tfmtg.setBold(run.isBold());
    }

    if (run.isItalic() != tfmtg.isItalic()) {
        if (run.isItalic()) {
            xhtml.startElement("i");
        } else {
            xhtml.endElement("i");
        }
        tfmtg.setItalic(run.isItalic());
    }

    boolean addedHREF = false;
    if (run instanceof XWPFHyperlinkRun) {
        XWPFHyperlinkRun linkRun = (XWPFHyperlinkRun) run;
        XWPFHyperlink link = linkRun.getHyperlink(document);
        if (link != null && link.getURL() != null) {
            xhtml.startElement("a", "href", link.getURL());
            addedHREF = true;
        } else if (linkRun.getAnchor() != null && linkRun.getAnchor().length() > 0) {
            xhtml.startElement("a", "href", "#" + linkRun.getAnchor());
            addedHREF = true;
        }
    }

    xhtml.characters(run.toString());

    // If we have any pictures, output them
    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        if (paragraph.getDocument() != null) {
            XWPFPictureData data = picture.getPictureData();
            if (data != null) {
                AttributesImpl attr = new AttributesImpl();

                attr.addAttribute("", "src", "src", "CDATA", "embedded:" + data.getFileName());
                attr.addAttribute("", "alt", "alt", "CDATA", picture.getDescription());

                xhtml.startElement("img", attr);
                xhtml.endElement("img");
            }
        }
    }

    if (addedHREF) {
        xhtml.endElement("a");
    }

    return tfmtg;
}

From source file:offishell.word.Word.java

License:MIT License

/**
 * <p>//  w  w w  .  j a va  2s  .c o m
 * Merge the specified {@link Word} to this document.
 * </p>
 * 
 * @param after
 * @return
 */
private Word merge(List<IBodyElement> elements) {
    XmlCursor cursor = cursorLast();

    // copy children
    for (int i = 0; i < elements.size(); i++) {
        IBodyElement element = elements.get(i);

        if (element instanceof XWPFParagraph) {
            XWPFParagraph para = (XWPFParagraph) element;
            XWPFParagraph created = createParagraph(cursor);
            WordHeleper.copy(para, created, v -> v);
            cursor = cursorAfter(created);

            if (i == 0) {
                created.setPageBreak(true);

                CTSectPr inSec = WordHeleper.section(para.getDocument());
                CTSectPr outSec = WordHeleper.section(created.getDocument());
                CTPageMar inMargin = inSec.getPgMar();
                CTPageMar outMargin = outSec.addNewPgMar();
                outMargin.setBottom(inMargin.getBottom());
                outMargin.setLeft(inMargin.getLeft());
                outMargin.setRight(inMargin.getRight());
                outMargin.setTop(inMargin.getTop());

                // List<XWPFFooter> inFooter = para.getDocument().getFooterList();
                // List<XWPFFooter> outFooter = created.getDocument().getFooterList();
                // XWPFHeaderFooterPolicy headerFooterPolicy =
                // created.getDocument().getHeaderFooterPolicy();
            }
        } else if (element instanceof XWPFTable) {
            XWPFTable table = (XWPFTable) element;
            XWPFTable created = calculated.createTable();
            created.removeRow(0); // new table has one row and one column, so we must remove it
            WordHeleper.copy(table, created, v -> v);
            cursor = cursorAfter(created);
        }
    }
    return this;
}

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

License:Open Source License

public void textHyperlink(String url, String text, XWPFParagraph paragraph) {

    // Add the link as External relationship
    String id = paragraph.getDocument().getPackagePart()
            .addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId();

    // Append the link and bind it to the relationship
    CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
    cLink.setId(id);//from  w ww.  j  a  v  a2 s.co m

    // // Create the linked text
    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(text);

    CTR ctr = CTR.Factory.newInstance();
    ctr.setTArray(new CTText[] { ctText });
    ctr.addNewRPr().addNewColor().setVal("0000FF");
    ctr.addNewRPr().addNewU().setVal(STUnderline.SINGLE);
    ctr.addNewRPr().addNewRFonts().setAscii(FONT_TYPE);
    // Insert the linked text into the link
    cLink.setRArray(new CTR[] { ctr });

}

From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java

License:Open Source License

/**
 * Copy.//from w ww.j  a v a  2  s .c  o m
 * 
 * @param userContent
 *            UserContent EObject
 * @param outputParagraphBeforeUserDocContent
 *            Output Paragraph Before User Doc Dest content (User Code dest is writen by {@link M2DocEvaluator} )
 * @param outputBody
 *            output body
 * @return last paragraph created by copy
 * @throws InvalidFormatException
 *             InvalidFormatException
 * @throws XmlException
 *             XmlException
 * @throws IOException
 *             if the copy fails
 */
@SuppressWarnings("resource")
public XWPFParagraph copy(UserContent userContent, XWPFParagraph outputParagraphBeforeUserDocContent,
        IBody outputBody) throws InvalidFormatException, XmlException, IOException {
    XWPFDocument containerOutputDocument = outputParagraphBeforeUserDocContent.getDocument();
    // Test if run before userContent is in same XWPFParagraph than first run of userContent
    if (!userDocContentIsFirstRunOfParagraph(userContent)) {
        previousInputParagraph = (XWPFParagraph) userContent.getRuns().get(userContent.getRuns().size() - 1)
                .getParent();
        currentInputParagraph = previousInputParagraph;
        currentOutputParagraph = outputParagraphBeforeUserDocContent;
    }
    XWPFParagraph currentRunParagraph = null;
    for (IConstruct abstractConstruct : userContent.getBody().getStatements()) {
        for (XWPFRun inputRun : abstractConstruct.getRuns()) {
            currentRunParagraph = (XWPFParagraph) inputRun.getParent();
            if (currentRunParagraph != currentInputParagraph) {
                currentInputParagraph = currentRunParagraph;
                // currentOutputParagraph = outputDocument.createParagraph();
                currentOutputParagraph = createNewParagraph(outputBody);
                // Copy new paragraph
                currentOutputParagraph.getCTP().set(currentInputParagraph.getCTP());
                listOutputParagraphs.add(currentOutputParagraph);
            }
            // Test if some run exist between userContent tag and first paragraph in this tag
            if (currentRunParagraph == previousInputParagraph) {
                // Clone run directly, paragraph is already generate by normal processing
                XWPFRun outputRun = currentOutputParagraph.createRun();
                outputRun.getCTR().set(inputRun.getCTR());
                // Keep run to change relation id later
                listOutputRuns.add(outputRun);
            }
            // Create picture embedded in run and keep relation id in map (input to output)
            createPictures(inputRun, containerOutputDocument);
        }
        // In case of table (no run in abstractConstruct)
        if (abstractConstruct instanceof Table) {
            Table table = (Table) abstractConstruct;
            XWPFTable inputTable = table.getTable();
            // XWPFTable outputTable = contenerOutputDocument.createTable();
            XWPFTable outputTable = createNewTable(outputBody, inputTable);
            outputTable.getCTTbl().set(inputTable.getCTTbl());
            copyTableStyle(inputTable, containerOutputDocument);
            listOutputTables.add(outputTable);
            // Inspect table to extract all picture ID in run
            collectRelationId(inputTable, containerOutputDocument);

        }
    }
    // Change Picture Id by xml replacement
    changePictureId();

    if (userContent.getClosingRuns().size() != 0
            && currentInputParagraph == userContent.getClosingRuns().get(0).getParent()) {
        needNewParagraph = false;
    }
    return currentOutputParagraph;
}