package com.product.print.util;
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.xwpf.usermodel.*;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.List;
|
|
/**
|
* @Author cheng
|
* @Date 2023/8/5 16:40
|
* @Desc
|
*/
|
public class PrintPoiUtil {
|
/**
|
* 复制行,从source到target
|
*
|
* @param target
|
* @param source
|
*/
|
public static void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
|
// 复制样式
|
if (source.getCtRow() != null) {
|
target.getCtRow().setTrPr(source.getCtRow().getTrPr());
|
}
|
// 复制单元格
|
for (int i = 0; i < source.getTableCells().size(); i++) {
|
XWPFTableCell cell1 = target.getCell(i);
|
XWPFTableCell cell2 = source.getCell(i);
|
if (cell1 == null) {
|
cell1 = target.addNewTableCell();
|
}
|
copyTableCell(cell1, cell2);
|
}
|
|
}
|
|
/**
|
* 复制单元格,从source到target
|
*
|
* @param target
|
* @param source
|
*/
|
public static void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
|
// 列属性
|
if (source.getCTTc() != null) {
|
target.getCTTc().setTcPr(source.getCTTc().getTcPr());
|
}
|
// 删除段落
|
for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
|
target.removeParagraph(pos);
|
}
|
// 添加段落
|
for (XWPFParagraph sp : source.getParagraphs()) {
|
XWPFParagraph targetP = target.addParagraph();
|
copyParagraph(targetP, sp);
|
}
|
}
|
|
/**
|
* 复制图片到target
|
*
|
* @param target
|
* @param picture
|
* @throws IOException
|
* @throws InvalidFormatException
|
*/
|
public static void copyPicture(XWPFRun target, XWPFPicture picture) throws IOException, InvalidFormatException {
|
|
String filename = picture.getPictureData().getFileName();
|
InputStream pictureData = new ByteArrayInputStream(picture
|
.getPictureData().getData());
|
int pictureType = picture.getPictureData().getPictureType();
|
int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
|
.getCx();
|
|
int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
|
.getCy();
|
|
// target.addBreak();
|
target.addPicture(pictureData, pictureType, filename, width, height);
|
// target.addBreak(BreakType.PAGE);
|
}
|
|
/**
|
* 复制段落,从source到target
|
*
|
* @param target
|
* @param source
|
*/
|
public static void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
|
|
// 设置段落样式
|
target.getCTP().setPPr(source.getCTP().getPPr());
|
|
// 移除所有的run
|
for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
|
target.removeRun(pos);
|
}
|
|
// copy 新的run
|
for (XWPFRun s : source.getRuns()) {
|
XWPFRun targetrun = target.createRun();
|
copyRun(targetrun, s);
|
}
|
|
|
}
|
|
/**
|
* 复制RUN,从source到target
|
*
|
* @param target
|
* @param source
|
*/
|
public static void copyRun(XWPFRun target, XWPFRun source) {
|
// 设置run属性
|
target.getCTR().setRPr(source.getCTR().getRPr());
|
// 设置文本
|
target.setText(source.text());
|
// 处理图片
|
List<XWPFPicture> pictures = source.getEmbeddedPictures();
|
|
for (XWPFPicture picture : pictures) {
|
try {
|
copyPicture(target, picture);
|
} catch (InvalidFormatException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|