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

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

Introduction

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

Prototype

public String addPictureData(InputStream is, int format) throws InvalidFormatException 

Source Link

Usage

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

License:Apache License

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

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

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

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

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

    }
}

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

License:Apache License

private void createWordDocument() {
    try {/*ww  w .  ja  va  2 s. co m*/
        XWPFDocument doc = new XWPFDocument();

        fillWordDocument(doc);
        InputStream pic = new FileInputStream("C://Users/user//Desktop//adobe.png");
        //      
        doc.addPictureData(pic, Document.PICTURE_TYPE_JPEG);

        FileOutputStream out = new FileOutputStream("C://temp//POIWord_1.docx");
        doc.write(out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:eremeykin.pete.reports.ui.ReportAction.java

@Override
public void actionPerformed(ActionEvent e) {
    resultChanged(null);//  w  w w  .j  av a  2s. c o  m
    if (model == null) {
        return;
    }

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("");
    r1.setBold(true);
    r1.setFontFamily("Times New Roman");
    r1.setFontSize(24);
    r1.setTextPosition(10);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.LEFT);
    p2.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r2 = p2.createRun();
    r2.setText(" ? : ");
    r2.setBold(false);
    r2.setFontFamily("Times New Roman");
    r2.setFontSize(14);
    r2.setTextPosition(10);

    XWPFTable table = doc.createTable(1, 2);
    table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(9000));
    ModelParameter root = model.getRoot();

    int row = 1;
    Map.Entry<ModelParameter, Integer> kv = model.getParameterAndLevelByID(root, 0);
    ModelParameter parameter = kv.getKey();
    Integer level = kv.getValue();

    ArrayList<Integer> ids = new ArrayList(model.asMap().keySet());
    Collections.sort(ids);
    for (Integer each : ids) {
        table.createRow();
        String text = "";
        kv = model.getParameterAndLevelByID(root, each);
        parameter = kv.getKey();
        level = kv.getValue();
        for (int c = 0; c < level; c++) {
            text += "        ";
        }
        table.getRow(row - 1).getCell(0).setText(text + parameter.toString());
        table.getRow(row - 1).getCell(1).setText(parameter.getValue());
        row++;
    }
    table.setWidth(80);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setAlignment(ParagraphAlignment.LEFT);
    p3.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r3 = p3.createRun();
    r3.addBreak();
    r3.setText("\n : ");
    r3.setBold(false);
    r3.setFontFamily("Times New Roman");
    r3.setFontSize(14);

    File uPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/uplot.png");
    try {
        byte[] picbytes = IOUtils.toByteArray(new FileInputStream(uPlotFile));
        doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG);
        XWPFRun pr = doc.createParagraph().createRun();
        pr.addPicture(new FileInputStream(uPlotFile), Document.PICTURE_TYPE_PNG, "plot.png", Units.toEMU(450),
                Units.toEMU(337));
        pr.addCarriageReturn();
        pr.addBreak(BreakType.PAGE);
        pr.addBreak(BreakType.TEXT_WRAPPING);

    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    XWPFParagraph p4 = doc.createParagraph();
    p4.setAlignment(ParagraphAlignment.LEFT);
    p4.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r4 = p4.createRun();
    r4.addBreak();
    r4.setText("\n ?: ");
    r4.setBold(false);
    r4.setFontFamily("Times New Roman");
    r4.setFontSize(14);

    File sPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/splot.png");
    try {
        byte[] picbytes = IOUtils.toByteArray(new FileInputStream(sPlotFile));
        doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG);
        XWPFParagraph pp = doc.createParagraph();
        pp.createRun().addPicture(new FileInputStream(sPlotFile), Document.PICTURE_TYPE_PNG, "plot.png",
                Units.toEMU(450), Units.toEMU(337));
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    File reportFile = new File("report.docx");
    try (FileOutputStream out = new FileOutputStream(reportFile)) {
        doc.write(out);
        if (Desktop.isDesktopSupported()) {
            Desktop.getDesktop().edit(reportFile);
        } else {
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

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

License:Open Source License

/**
 * createPictures in document.//w w w  .  j ava2s .co  m
 * 
 * @param inputRun
 *            input Run
 * @param outputDoc
 *            output Document
 * @throws InvalidFormatException
 *             InvalidFormatException
 */
private void createPictures(XWPFRun inputRun, XWPFDocument outputDoc) throws InvalidFormatException {
    // Add picture in document and keep relation id change idRelation reference
    for (XWPFPicture inputPic : inputRun.getEmbeddedPictures()) {
        byte[] img = inputPic.getPictureData().getData();
        // Put image in doc and get idRelation
        String idRelationOutput = outputDoc.addPictureData(img, inputPic.getPictureData().getPictureType());
        String idRelationInput = inputPic.getCTPicture().getBlipFill().getBlip().getEmbed();
        inputPicuteIdToOutputmap.put(idRelationInput, idRelationOutput);
    }
}