Search in sources :

Example 26 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project java-learning by HouariZegai.

the class WordAPIDemo method main.

public static void main(String[] args) throws InvalidFormatException {
    try {
        String machineName = System.getProperty("user.name");
        FileOutputStream file = new FileOutputStream("C:\\Users\\" + machineName + "\\Downloads\\Documents\\wordFromJava.docx");
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("I'm Houari ZEGAI coming from Java !");
        run.addBreak();
        run.setFontFamily("Comic Sans MS");
        run.setBold(true);
        XWPFRun run2 = paragraph.createRun();
        run2.setText("Second paragraph");
        run2.setColor("2196f3");
        run2.setFontSize(24);
        run2.setUnderline(UnderlinePatterns.DASHED_HEAVY);
        // Adding image
        XWPFRun run3 = paragraph.createRun();
        run3.addPicture(WordAPIDemo.class.getResourceAsStream("/images/dark.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "houari", Units.toEMU(400), Units.toEMU(200));
        // Adding table
        XWPFTable table = document.createTable();
        table.setCellMargins(20, 200, 20, 200);
        XWPFTableRow row0 = table.getRow(0);
        row0.getCell(0).setText("N°Dos");
        row0.createCell().setText("Nom");
        row0.createCell().setText("Prenom");
        row0.createCell().setText("Date Naissance");
        row0.createCell().setText("Sexe");
        row0.createCell().setText("Club");
        row0.createCell().setText("Code Wilaya");
        row0.createCell().setText("Eq/Ind");
        row0.getCell(0).setColor("2196f3");
        row0.getCell(1).setColor("2196f3");
        row0.getCell(2).setColor("2196f3");
        row0.getCell(3).setColor("2196f3");
        row0.getCell(4).setColor("2196f3");
        row0.getCell(5).setColor("2196f3");
        row0.getCell(6).setColor("2196f3");
        row0.getCell(7).setColor("2196f3");
        XWPFTableRow row1 = table.createRow();
        row1.getCell(0).setText("001");
        row1.getCell(1).setText("Houari");
        row1.getCell(2).setText("ZEGAI");
        row1.getCell(3).setText("01/01/1996");
        row1.getCell(4).setText("Homme");
        row1.getCell(5).setText("CAAT");
        row1.getCell(6).setText("TIARET");
        row1.getCell(7).setText("E");
        XWPFTableRow row2 = table.createRow();
        row2.getCell(0).setText("002");
        row2.getCell(1).setText("Khaled");
        row2.getCell(2).setText("ZEGAI");
        row2.getCell(3).setText("23/09/1968");
        row2.getCell(4).setText("Homme");
        row2.getCell(5).setText("CSST");
        row2.getCell(6).setText("TIARET");
        row2.getCell(7).setText("I");
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        document.write(file);
        file.close();
        System.out.println("Done !");
    } catch (FileNotFoundException e) {
        System.out.println("Error msg: (FileNotFoundException) " + e.getMessage());
    } catch (IOException ex) {
        System.out.println("Error msg: (IOException) " + ex.getMessage());
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) IOException(java.io.IOException) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Example 27 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project hutool by looly.

the class Word07Writer method addPicture.

/**
 * 增加图片,单独成段落,增加后图片流关闭
 *
 * @param in       图片流
 * @param picType  图片类型,见Document.PICTURE_TYPE_XXX
 * @param fileName 文件名
 * @param width    宽度
 * @param height   高度
 * @param align    图片的对齐方式
 * @return this
 * @since 5.2.4
 */
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height, ParagraphAlignment align) {
    final XWPFParagraph paragraph = doc.createParagraph();
    paragraph.setAlignment(align);
    final XWPFRun run = paragraph.createRun();
    try {
        run.addPicture(in, picType.getValue(), fileName, Units.toEMU(width), Units.toEMU(height));
    } catch (InvalidFormatException e) {
        throw new POIException(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(in);
    }
    return this;
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) POIException(cn.hutool.poi.exceptions.POIException)

Example 28 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project pmph by BCSquad.

the class BookChooseTrackWordHelper method fromDeclarationEtcBOList.

/**
 * 填充 word 数据
 * @param word_name
 * @return
 */
private HashMap<String, XWPFDocument> fromDeclarationEtcBOList(String word_name, Book bo) {
    InputStream is;
    XWPFDocument document;
    String path = this.getClass().getClassLoader().getResource("BookChooseTrackTemplate.docx").getPath();
    HashMap<String, XWPFDocument> map = new HashMap<>(1);
    int decSequese = 0;
    Object object = (Object) bo;
    try {
        is = new FileInputStream(path);
        document = new XWPFDocument(is);
    } catch (IOException ex) {
        logger.error("读取Word模板文件'ClicResumeTemplate.docx'时出现错误:{}", ex.getMessage());
        throw new CheckedServiceException(CheckedExceptionBusiness.CLINICAL_DECISION, CheckedExceptionResult.FILE_CREATION_FAILED, "未找到模板文件");
    }
    if (StringUtil.notEmpty(word_name)) {
        List<XWPFRun> runs = document.getParagraphs().get(0).getRuns();
        runs.get(0).setText(word_name.concat("图书纠错"), 0);
    }
    List<XWPFParagraph> xwpfParagraphs = document.getParagraphs();
    List<XWPFTable> tables = document.getTables();
    String filename = generateFileName(bo, (++decSequese) + ".");
    XWPFTable xwpfTable = tables.get(0);
    xwpfTable.getRow(0).getTableCells().get(1).setText(bo.getIsbn());
    xwpfTable.getRow(0).getCell(3).setText(bo.getBookname() == null ? "" : bo.getBookname());
    xwpfTable.getRow(0).getCell(5).setText(bo.getIsbn() == null ? "" : bo.getIsbn().toString());
    /*	map.put(filename, removeEmptyTables(document, filter));*/
    map.put(filename, document);
    return map;
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) HashMap(java.util.HashMap) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Example 29 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.

the class SimpleImages method main.

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 {
            System.err.println("Unsupported picture: " + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }
        r.setText(imgFile);
        r.addBreak();
        // 200x200 pixels
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
        r.addBreak(BreakType.PAGE);
    }
    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
    doc.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) FileInputStream(java.io.FileInputStream)

Example 30 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.

the class SimpleTable method createSimpleTable.

public static void createSimpleTable() throws Exception {
    XWPFDocument doc = new XWPFDocument();
    try {
        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);
        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");
        OutputStream out = new FileOutputStream("simpleTable.docx");
        try {
            doc.write(out);
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Aggregations

XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)45 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)39 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)20 FileOutputStream (java.io.FileOutputStream)11 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)10 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)8 IOException (java.io.IOException)7 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)7 File (java.io.File)5 OutputStream (java.io.OutputStream)5 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 FileInputStream (java.io.FileInputStream)4 XmlCursor (org.apache.xmlbeans.XmlCursor)4 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)3 HashMap (java.util.HashMap)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 IORuntimeException (cn.hutool.core.io.IORuntimeException)2 POIException (cn.hutool.poi.exceptions.POIException)2 Path (java.nio.file.Path)2 Matcher (java.util.regex.Matcher)2