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;
}
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);
}
}
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();
}
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();
}
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);
}
}
}
}
Aggregations