package cloud.user.utils.base;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.SaveFormat;
import com.aspose.words.SaveOptions;
import com.aspose.words.net.System.Data.DataRow;
import com.aspose.words.net.System.Data.DataTable;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
@Slf4j
public class ContractUtil {
private ContractUtil() {
}
public static void main(String[] args) throws Exception {
ContractUtil.sealInWord("D:/11111111.docx", "D:\\222222222.docx", "D:/111.png", "(盖章)", 1, 2, 70, 70, 230, -20, true);
}
/**
* <b> Word中添加图章
* </b><br><br><i>Description</i> :
* String srcPath, 源Word路径
* String storePath, 添加图章后的路径
* String sealPath, 图章路径(即图片)
* tString tabText, 在Word中盖图章的标识字符串,如:(签字/盖章)
* tString targetRow, 要盖章操作的行数
* int width, 图章宽度
* int height, 图章高度
* int leftOffset, 图章在编辑段落向左偏移量
* int topOffset, 图章在编辑段落向上偏移量
* boolean behind,图章是否在文字下面
*
* @return void
* <br><br>Date: 2019/12/26 15:12 <br>Author : dxl
*/
public static void sealInWord(String srcPath, String storePath, String sealPath, String tabText, int targetTable, int targetRow,
int width, int height, int leftOffset, int topOffset, boolean behind) throws Exception {
File fileTem = new File(srcPath);
InputStream is = new FileInputStream(fileTem);
XWPFDocument document = new XWPFDocument(is);
XWPFRun targetRun = null;
//表格里
List<XWPFTable> tables = document.getTables();
XWPFTable xwpfTable1 = tables.get(targetTable);
//取 targetRow行
XWPFTableRow row = xwpfTable1.getRow(targetRow);
if (row != null && row.getTableCells() != null) {
for (int i = 0; i < row.getTableCells().size(); i++) {
List<XWPFParagraph> paragraphs = row.getCell(i).getParagraphs();
for (XWPFParagraph xwpfParagraph1 : paragraphs) {
List<XWPFRun> runs1 = xwpfParagraph1.getRuns();
for (XWPFRun run : runs1) {
if (!"".equals(run.text()) && run.text().contains(tabText)) {
targetRun = run;
break;
}
}
}
}
}
if (targetRun != null) {
InputStream in = new FileInputStream(sealPath);//设置图片路径
if (width <= 0) {
width = 100;
}
if (height <= 0) {
height = 100;
}
//创建Random类对象
Random random = new Random();
//产生随机数
int number = random.nextInt(999) + 1;
targetRun.addPicture(in, org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG, "Seal" + number, Units.toEMU(width), Units.toEMU(height));
in.close();
// 2. 获取到图片数据
CTDrawing drawing = targetRun.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
//拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "Seal" + number,
Units.toEMU(width), Units.toEMU(height),//图片大小
Units.toEMU(leftOffset), Units.toEMU(topOffset), behind);//相对当前段落位置 需要计算段落已有内容的左偏移
drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
drawing.removeInline(0);//删除行内属性
}
document.write(new FileOutputStream(storePath));
document.close();
}
/**
* document :源文件的document文件
*
* @param sorceFileDocument
* @param storePath
* @param picPath
* @param tabText
* @param targetTable
* @param targetRow
* @param width
* @param height
* @param leftOffset
* @param topOffset
* @param behind
* @return
* @throws Exception
*/
public static XWPFDocument sealInWord(XWPFDocument sorceFileDocument, String picPath, String tabText, int targetTable, int targetRow,
int width, int height, int leftOffset, int topOffset, boolean behind) {
XWPFRun targetRun = null;
//表格里
List<XWPFTable> tables = sorceFileDocument.getTables();
XWPFTable xwpfTable1 = tables.get(targetTable);
//取 targetRow行
XWPFTableRow row = xwpfTable1.getRow(targetRow);
if (row != null && row.getTableCells() != null) {
for (int i = 0; i < row.getTableCells().size(); i++) {
List<XWPFParagraph> paragraphs = row.getCell(i).getParagraphs();
for (XWPFParagraph xwpfParagraph1 : paragraphs) {
List<XWPFRun> runs1 = xwpfParagraph1.getRuns();
for (XWPFRun run : runs1) {
if (!"".equals(run.text()) && run.text().contains(tabText)) {
targetRun = run;
break;
}
}
}
}
}
try {
if (targetRun != null) {
InputStream in = new FileInputStream(picPath);//设置图片路径
if (width <= 0) {
width = 100;
}
if (height <= 0) {
height = 100;
}
//创建Random类对象
Random random = new Random();
//产生随机数
int number = random.nextInt(999) + 1;
targetRun.addPicture(in, org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG, "Seal" + number, Units.toEMU(width), Units.toEMU(height));
in.close();
// 2. 获取到图片数据
CTDrawing drawing = targetRun.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
//拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "Seal" + number,
Units.toEMU(width), Units.toEMU(height),//图片大小
Units.toEMU(leftOffset), Units.toEMU(topOffset), behind);//相对当前段落位置 需要计算段落已有内容的左偏移
drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
drawing.removeInline(0);//删除行内属性
}
} catch (Exception e) {
log.error("建瓯生成申请审批表电子章接口 download/downloadWord 报错:", e);
return sorceFileDocument;
}
return sorceFileDocument;
}
/**
* @param ctGraphicalObject 图片数据
* @pa
- 1
- 2
前往页