Example usage for org.apache.poi.xwpf.usermodel Document PICTURE_TYPE_PNG

List of usage examples for org.apache.poi.xwpf.usermodel Document PICTURE_TYPE_PNG

Introduction

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

Prototype

int PICTURE_TYPE_PNG

To view the source code for org.apache.poi.xwpf.usermodel Document PICTURE_TYPE_PNG.

Click Source Link

Document

PNG format

Usage

From source file:de.micromata.tpsb.doc.renderer.MicrosoftWordRenderer.java

License:Apache License

/**
 * Fgt Screenshots zum aktuellen Test in die Word-Vorlage ein, sofern einer
 * oder mehrere vorhanden sind./*from   w  w  w.j av a  2  s  .c o m*/
 * 
 * @param mInfo
 *            die MethodInfos
 */
private void addScreenShotsIfExists(MethodInfo mInfo) {
    int counter = 1;
    do {
        String screenshotFileName = getScreenshotFileName(mInfo, counter);
        File screenshot = new File(screenshotFileName);
        if (screenshot.exists() == false) {
            return;
        }

        addPar("Screenshot " + counter, "");
        try {
            FileInputStream pic = new FileInputStream(screenshot);
            document.createParagraph().createRun().addPicture(pic, Document.PICTURE_TYPE_PNG, "my pic",
                    Units.toEMU(450), Units.toEMU(254));
        } catch (IOException e) {
            log.error("Fehler beim Rendern des Screenshots", e);
        } catch (InvalidFormatException e) {
            log.error("Fehler beim Rendern des Screenshots", e);
        }
        counter++;
    } while (true);
}

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

@Override
public void actionPerformed(ActionEvent e) {
    resultChanged(null);/*from  www. ja v  a2  s  .  c  om*/
    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.TemplateProcessor.java

License:Open Source License

/**
 * Returns the picture type based on the filename's extension.
 * //from   w  w w  .j  a  va 2s . co m
 * @param fileName
 *            the picture file
 * @return the picture's file extension
 */
private int getPictureType(String fileName) {
    String[] segments = fileName.split("\\.");
    int result;
    if (segments.length > 1) {
        String extension = segments[segments.length - 1].trim();
        if ("jpg".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_JPEG;
        } else if ("gif".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_GIF;
        } else if ("png".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_PNG;
        } else if ("emf".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_EMF;
        } else if ("wmf".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_WMF;
        } else if ("pict".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_PICT;
        } else if ("dib".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_DIB;
        } else if ("tiff".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_TIFF;
        } else if ("eps".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_EPS;
        } else if ("bmp".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_BMP;
        } else if ("wpg".equalsIgnoreCase(extension)) {
            result = Document.PICTURE_TYPE_WPG;
        } else {
            result = 0;
        }
    } else {
        result = 0;
    }
    return result;
}

From source file:org.ohdsi.ooxml.CustomXWPFDocument.java

License:Apache License

public void addPicture(BufferedImage image, int width, int height)
        throws InvalidFormatException, FileNotFoundException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//from  www.j a  v  a2  s .  c  om
        ImageIO.write(image, "PNG", out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    addPicture(in, Document.PICTURE_TYPE_PNG, width, height);
}