POI 签章 签字 实现


通过文档关键字搜索实现:盖章 签字 ,因客户要求 盖章签字在同一位置,所以盖章 签字图片 必须是透明的。
(1)通过文档中表格的关键字 搜索盖章
public static void addStampImageTable(String sourceFile, String targetFile, String imageGzurl, String imageQzurl, String keyword, String contenttext, Boolean gz, Boolean qz, int leftsetGz, int leftsetQz) throws Exception {
//由于poi 不支持word2003版doc文件,通过spire 转成 docx 文件
Document doc1 = new Document();
doc1.loadFromFile(sourceFile);
doc1.saveToFile(sourceFile, FileFormat.Docx_2013);
XWPFDocument doc;
Boolean isFind = false;
try {
doc = new XWPFDocument(new FileInputStream(sourceFile));
for (XWPFTable table : doc.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {//遍历每一个单元格
if (cell.getText().contains(contenttext)) {//如果遇到"&章"则进行替换
try {
System.out.println(cell.getText() + "kkkkkkkkkkkk");
isFind = true;
insertCellStamp(cell, imageGzurl, imageQzurl, keyword, gz, qz, leftsetGz, leftsetQz);//给带有要盖章字样的单元格 加上章的图片
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

        FileOutputStream fos = new FileOutputStream(targetFile);
        doc.write(fos);
        fos.close();
        doc.write(new FileOutputStream(targetFile));
        InputStream is = new FileInputStream(targetFile);
        XWPFDocument document = new XWPFDocument(is);
        //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
        document.removeBodyElement(0);
        //输出word内容文件流,新输出路径位置
        OutputStream os = new FileOutputStream(targetFile);
        document.write(os);
        os.close();
        doc1.close();
     /*   if(!isFind){
            throw new Exception("没有发现关键字 ");
        }*/
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());

    }
}

private static void insertCellStamp(XWPFTableCell cell, String imageGzurl, String imageQzurl, String keyword, Boolean gz, Boolean qz, int leftsetGz, int leftsetQz) throws Exception {

    Boolean isfind = false;
    for (XWPFParagraph para : cell.getParagraphs()) {
        for (int i = 0; i < para.getRuns().size(); i++) {
            XWPFRun run = para.getRuns().get(i);
            String text = run.text();
            System.out.println(text + "ppppppppppppppppp");
            if (text.contains("&")) {
                isfind = true;
                break;
            }
        }

    }


    for (XWPFParagraph para : cell.getParagraphs()) {
        int k = 0;
        Boolean isfindyear = false;
        //插入图片
        for (int i = 0; i < para.getRuns().size(); i++) {
            try {
                XWPFRun run = para.getRuns().get(i);
                String text = run.text();
                System.out.println(text + "ppppppppppppppppp");
                if (text.contains("章") && isfind) {
                    /*删除盖章关键字*/
                    run.setText(text.replace(keyword, ""), 0);
                    Random random = new Random();
                    int number = random.nextInt(999) + 1;
                    if (gz) {
                        System.out.println("盖章开始------------------------");
                        FileInputStream is1 = new FileInputStream(imageGzurl);
                        run.addPicture(is1, XWPFDocument.PICTURE_TYPE_JPEG, imageGzurl, Units.toEMU(60), Units.toEMU(60));
                        is1.close();
                        CTDrawing drawing1 = run.getCTR().getDrawingArray(0);
                        CTGraphicalObject graphicalobject1 = drawing1.getInlineArray(0).getGraphic();
                        //拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
                        CTAnchor anchor1 = getAnchorWithGraphic(graphicalobject1, "Seal" + number,
                                Units.toEMU(120), Units.toEMU(120),//图片大小
                                Units.toEMU(leftsetGz), Units.toEMU(-30), true);//相对当前段落位置及偏移
                        drawing1.setAnchorArray(new CTAnchor[]{anchor1});//添加浮动属性
                        drawing1.removeInline(0);//删除行内属性
                        System.out.println("盖章结束------------------------");
                    }

                    if (qz) {
                        //添加签名图片
                        System.out.println("签名开始------------------------");
                        run = para.createRun();
                        FileInputStream is2 = new FileInputStream(imageQzurl);
                        run.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, imageQzurl, Units.toEMU(60), Units.toEMU(60));
                        is2.close();
                        random = new Random();
                        CTDrawing drawing2 = run.getCTR().getDrawingArray(0);
                        CTGraphicalObject graphicalobject2 = drawing2.getInlineArray(0).getGraphic();
                        number = random.nextInt(999) + 1;
                        CTAnchor anchor2 = null;
                        if (!gz) {
                            /*只是签字*/
                            anchor2 = getAnchorWithGraphic(graphicalobject2, "Seal" + number,
                                    Units.toEMU(120), Units.toEMU(60),//图片大小 负值 代表向上
                                    Units.toEMU(leftsetQz), Units.toEMU(-20), false);//  相对当前段落位置及偏移
                        } else {
                            /*盖章+签字*/
                            anchor2 = getAnchorWithGraphic(graphicalobject2, "Seal" + number,
                                    Units.toEMU(120), Units.toEMU(60),//图片大小 负值 代表向上
                                    Units.toEMU(leftsetQz), Units.toEMU(-10), false);
                        }
                        drawing2.setAnchorArray(new CTAnchor[]{anchor2});//添加浮动属性
                        drawing2.removeInline(0);//删除行内属性
                        System.out.println("签名结束------------------------");
                    }
                }


                if (text.indexOf("年") != -1 && !isfindyear) {
                    /**/
                    String strDate = DateUtil.formatDate(new Date(), "yyyy年MM月dd日");
                    run.setText(strDate, 0);
                    isfindyear = true;
                    k = i;
                }
                if (isfindyear && i > k) {
                    /*将多余的年月日 替换成空*/
                    if (text.indexOf("年") != -1 || text.indexOf("月") != -1 || text.indexOf("日") != -1) {
                        run.setText("", 0);
                    }
                }
            } catch (Exception e) {
                System.out.println("Error: ========  插入公章图片时出错了:可能是图片路径不存在");
                e.printStackTrace();
                throw new Exception("Error: ========  插入公章图片时出错了:可能是图片路径不存在");

            }
        }
    }
}

(2)通过文档中关键字搜索(非表格中)
public static void addStampBykeyWord(String sourceFile, String targetFile, String imageurl, String keyword) throws Exception {
Document doc1 = new Document();
doc1.loadFromFile(sourceFile);
doc1.saveToFile(sourceFile, FileFormat.Docx_2013);
InputStream is = null;
try {
XWPFDocument doc;
doc = new XWPFDocument(new FileInputStream(sourceFile));
List paragraphs = doc.getParagraphs();
XWPFRun targetRun = null;
XWPFParagraph paragraph1 = null;
Boolean isfind = false;
for (XWPFParagraph paragraph : paragraphs) {
if (!"".equals(paragraph.getText()) && paragraph.getText().contains(keyword)) {
List runs = paragraph.getRuns();
paragraph1 = paragraph;
targetRun = runs.get(runs.size() - 1);
isfind = true;
break;
}
}
if (isfind) {
if (targetRun != null) {
System.out.println("签章操作开始---------------------");
InputStream in = new FileInputStream(imageurl);//设置图片路径
//创建Random类对象
Random random = new Random();
//产生随机数
int number = random.nextInt(999) + 1;
targetRun = paragraph1.createRun();
targetRun.addPicture(in, XWPFDocument.PICTURE_TYPE_JPEG, "Seal" + number, Units.toEMU(60), Units.toEMU(60));
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(150), Units.toEMU(150),//图片大小
Units.toEMU(200), Units.toEMU(-20), false);//相对当前段落位置 需要计算段落已有内容的左偏移
drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
drawing.removeInline(0);//删除行内属性
System.out.println("签章操作结束---------------------");
}
}

        FileOutputStream fos = new FileOutputStream(targetFile);
        doc.write(fos);
        fos.close();
        doc.write(new FileOutputStream(targetFile));
        is = new FileInputStream(targetFile);
        XWPFDocument document = new XWPFDocument(is);
        //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
        document.removeBodyElement(0);
        //输出word内容文件流,新输出路径位置
        OutputStream os = new FileOutputStream(targetFile);
        document.write(os);
        os.close();
        is.close();
        doc1.close();
        if (!isfind) {
            throw new Exception("没有发现关键字 " + keyword);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("操作失败:" + e.getMessage());

    }
}
posted @ 2021-01-30 18:40  永不止步快乐的奋斗  阅读(598)  评论(0编辑  收藏  举报