Search in sources :

Example 41 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project cloud-pipeline by epam.

the class ImageTemplateProcessor method insertData.

@Override
boolean insertData(XWPFParagraph splittedParagraph, XWPFRun runTemplate, XmlCursor cursor, Object data) {
    byte[] byteArray = (byte[]) data;
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
    XWPFParagraph dataParagraph = splittedParagraph.getDocument().insertNewParagraph(cursor);
    this.copyParagraphProperties(splittedParagraph, dataParagraph);
    XWPFRun newRun = dataParagraph.createRun();
    this.copyRunProperties(runTemplate, newRun);
    try {
        int width = DEFAULT_IMAGE_WIDTH_PX;
        int height = DEFAULT_IMAGE_HEIGHT_PX;
        try {
            ByteArrayInputStream imageStream = new ByteArrayInputStream(byteArray);
            BufferedImage bufferedImage = ImageIO.read(imageStream);
            width = bufferedImage.getWidth();
            height = bufferedImage.getHeight();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        double ratio = height / (width + 0.0);
        final int widthInPoints = 450;
        newRun.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(widthInPoints), Units.toEMU(widthInPoints * ratio));
    } catch (InvalidFormatException | IOException e) {
        log.error(e.getMessage(), e);
    }
    cursor.toNextToken();
    return true;
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) BufferedImage(java.awt.image.BufferedImage)

Example 42 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project cloud-pipeline by epam.

the class SplitParagraphTemplateProcessor method replacePlaceholderWithData.

@Override
void replacePlaceholderWithData(XWPFParagraph paragraph, Object data) {
    if (this.xwpfRun == null || paragraph == null) {
        return;
    }
    int globalStartIndex = 0;
    boolean shouldMoveRun = false;
    int runToRemoveIndex = 0;
    XWPFParagraph currentParagraph = null;
    final List<XWPFRun> runs = paragraph.getRuns();
    XmlCursor xmlCursor = paragraph.getCTP().newCursor();
    xmlCursor.toNextSibling();
    for (XWPFRun run : runs) {
        if (!shouldMoveRun) {
            runToRemoveIndex++;
        }
        String runText = run.getText(0);
        if (runText == null) {
            continue;
        }
        int globalEndIndex = globalStartIndex + runText.length();
        if (globalStartIndex > this.end || globalEndIndex < this.start) {
            globalStartIndex = globalEndIndex;
            if (shouldMoveRun && currentParagraph != null) {
                XWPFRun newRun = currentParagraph.createRun();
                this.copyRunProperties(run, newRun, true);
            }
            continue;
        }
        int replaceFrom = Math.max(globalStartIndex, this.start) - globalStartIndex;
        int replaceTo = Math.min(globalEndIndex, this.end) - globalStartIndex;
        if (this.xwpfRun.equals(run)) {
            String beforePlaceholderText = runText.substring(0, replaceFrom);
            run.setText(beforePlaceholderText, 0);
            this.insertData(paragraph, run, xmlCursor, data);
            if (!xmlCursor.isStart()) {
                break;
            }
            currentParagraph = paragraph.getDocument().insertNewParagraph(xmlCursor);
            this.copyParagraphProperties(paragraph, currentParagraph);
            String afterPlaceholderText = runText.substring(replaceTo);
            shouldMoveRun = true;
            if (currentParagraph != null) {
                XWPFRun newRun = currentParagraph.createRun();
                this.copyRunProperties(run, newRun);
                newRun.setText(afterPlaceholderText, 0);
            }
        } else {
            runText = runText.substring(0, replaceFrom).concat(runText.substring(replaceTo));
            run.setText(runText, 0);
            if (shouldMoveRun && currentParagraph != null) {
                XWPFRun newRun = currentParagraph.createRun();
                this.copyRunProperties(run, newRun, true);
            }
        }
        globalStartIndex = globalEndIndex;
    }
    while (paragraph.getRuns().size() > runToRemoveIndex) {
        paragraph.removeRun(runToRemoveIndex);
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 43 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project cloud-pipeline by epam.

the class BulletListTemplateProcessor method insertDataItem.

void insertDataItem(XWPFParagraph splittedParagraph, XWPFRun runTemplate, XmlCursor cursor, Object item, BigInteger bulletListStyleId) {
    XWPFParagraph dataParagraph = splittedParagraph.getDocument().insertNewParagraph(cursor);
    this.copyParagraphProperties(splittedParagraph, dataParagraph);
    if (bulletListStyleId != null) {
        dataParagraph.setStyle("ListParagraph");
        dataParagraph.setNumID(bulletListStyleId);
    }
    XWPFRun newRun = dataParagraph.createRun();
    this.copyRunProperties(runTemplate, newRun);
    newRun.setText(item.toString(), 0);
    cursor.toNextToken();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun)

Example 44 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project cloud-pipeline by epam.

the class MultiLineTemplateProcessor method insertDataItem.

private void insertDataItem(XWPFParagraph splittedParagraph, XWPFRun runTemplate, XmlCursor cursor, Object dataItem) {
    XWPFParagraph dataParagraph = splittedParagraph.getDocument().insertNewParagraph(cursor);
    this.copyParagraphProperties(splittedParagraph, dataParagraph);
    XWPFRun newRun = dataParagraph.createRun();
    this.copyRunProperties(runTemplate, newRun);
    newRun.setText(dataItem.toString(), 0);
    cursor.toNextToken();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun)

Example 45 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project ed-springboot-learning by QQ986945193.

the class PoiUtil method replaceInPara.

/**
 * 替换段落里面的变量
 *
 * @param para
 *            要替换的段落
 * @param params
 *            参数
 */
private static void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
    List<XWPFRun> runs;
    Matcher matcher;
    if (matcher(para.getParagraphText()).find()) {
        runs = para.getRuns();
        for (int i = 0; i < runs.size(); i++) {
            XWPFRun run = runs.get(i);
            String runText = run.toString();
            matcher = matcher(runText);
            if (matcher.find()) {
                while ((matcher = matcher(runText)).find()) {
                    runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
                }
                // 直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
                // 所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
                para.removeRun(i);
                para.insertNewRun(i).setText(runText);
            }
        }
    }
}
Also used : XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) Matcher(java.util.regex.Matcher)

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