Word file insert signature or seal picture (original code)

 /*
     * Test report signature
     * Signature call method
     *
     * fileid file id
     * userid user id
     * typeid placeholder
     * gid primary key
     * code file server encoding
     * */
    @RequestMapping("insertimages")
    public <string> String insertimages(String Fileid, String Userid, String Typeid, String gid, String code) throws Exception {
        FileInfo fileinfo = (FileInfo) baseDao. get(Fileid, FileInfo. class);
        /*Get the real path of the report*/
        String diskname = fileinfo.getDiskname();
        String fieldname = fileinfo. getFoldername();
        String specifiedname = fileinfo.getSpecifiedname();
        String fullpath="";
        if (fieldname==null){
            fullpath= diskname + specifiedname;
        } else {
            fullpath = diskname + fieldname + File.separator + specifiedname;
        }

        /*Get the path of the current user's signature image*/
        User user = (User) baseDao. get(Userid, User. class);
        String images1 = user.getImages();/*This is the field added before users to store the id of the signature image*/
        FileInfo images = (FileInfo) baseDao. get(images1, FileInfo. class);
        //The root directory of image storage
        String diskname2 = images. getDiskname();
        //Subdirectory for image storage
        String foldername = images. getFoldername();
        //picture name
        String specifiedname2 = images. getSpecifiedname();
        String imagepath="";
        if (foldername==null){
            imagepath= diskname2 + File.separator + specifiedname2;
        }
        else {
            imagepath= diskname2 + File.separator + foldername + File.separator + specifiedname2;
// imagepath= diskname2 + foldername + File.separator + specifiedname2;
        }
        System.out.println("imagepath = " + imagepath);
        /* Set replacement identifier */
        String signature=null;

        if (Typeid. equals("write")){
            signmark="${marksign1}";
        }
        if (Typeid. equals("check")){
            signmark="${marksign2}";
        }
        if (Typeid. equals("post")){
            signmark="${marksign3}";
        }
        /**
         * doc and docx difference compatible ?
         */
        /*FileInputStream fis = new FileInputStream(fullpath);
        XWPFDocument xwpfDocument = new XWPFDocument(fis);
        List<XWPFParagraph> xwpfParagraphs = xwpfDocument.getParagraphs();
        List<XWPFTable> xwpftables = xwpfDocument.getTables();
        FileInputStream fileInputStream = new FileInputStream(imagepath);*/
        //The text box is displayed in Word. The second step is to get the XmlObject of the XWPFParagraph, and then get the cursor of the XmlObject object.

// convertDocFmt(fullpath,"C:\JavaProduct\productFile\\
ewFile\\
ew.docx",12);

// OPCPackage opcPackage = POIXMLDocument. openPackage(fullpath);
// OPCPackage opcPackage = POIXMLDocument.openPackage("C:\JavaProduct\productFile\\
ewFile\\
ew.doc");
        OPCPackage opcPackage = POIXMLDocument. openPackage(fullpath);
        XWPFDocument xwpfDocument = new XWPFDocument(opcPackage);
        List<XWPFParagraph> xwpfParagraphs = xwpfDocument.getParagraphs();
        List<XWPFTable> xwpftables = xwpfDocument.getTables();
        FileInputStream fileInputStream = new FileInputStream(imagepath);

        for (XWPFTable xwpftable : xwpftables) {
            List<XWPFTableRow> rows = xwpftable.getRows();
            for (XWPFTableRow row : rows) {
                List<XWPFTableCell> tableCells = row.getTableCells();
                for (XWPFTableCell tableCell : tableCells) {
                    String signInfo=tableCell. getText();
                    if(signInfo. equals(signmark)){

                        List<XWPFParagraph> paragraphs = tableCell. getParagraphs();
                        for (XWPFParagraph paragraph : paragraphs) {

                            List<XWPFRun> runs = paragraph. getRuns();

                            for (XWPFRun run : runs) {
                                run.addPicture(fileInputStream,XWPFDocument.PICTURE_TYPE_PNG,specifiedname2, Units.toEMU(100), Units.toEMU(30));
                                run.setText(" ",0);

                            }
                        }
                        tableCell.setText(" ");
                    }

                }
            }

        }
        int num=0;
        for(int i=0;i<xwpfParas. size();i ++ ){
            if(num==3) {
                break;
            }
            XWPFParagraph xwpfParagraph = xwpfParas.get(i);
            String text=xwpfParagraph. getText();

            //insert picture
            if(text. equals(signmark)){
                XmlCursor cursor = xwpfParagraph.getCTP().newCursor();
                XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
                newPara.setAlignment(ParagraphAlignment.CENTER);//Center
                XWPFRun newParaRun = newPara.createRun();
                newParaRun.addPicture(fileInputStream,XWPFDocument.PICTURE_TYPE_PNG,specifiedname2, Units.toEMU(100), Units.toEMU(30));
                xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
            }
        }
// String url="D:\images\\
ewFile" + "\" + specifiedname;

        //Modify the report temporary address to the project root directory
        File fileDir;
        fileDir = new File(PathHelper.getApplicationHome().getAbsoluteFile() + File.separator + "REPORT_TEMPORARY");
        // Create the directory if it does not exist
        if (fileDir.isDirectory() || !fileDir.exists()) {
            fileDir.mkdir();
        }
        String url= fileDir + File.separator + specifiedname;
        write(xwpfDocument, url);

        /*EntReportTaskEntity entReportTaskEntity = (EntReportTaskEntity) baseDao.load(gid, EntReportTaskEntity.class);
        String consignGid = entReportTaskEntity. getConsignGid();
        DetConsignEntEntity entEntity = (DetConsignEntEntity) baseDao.load(consignGid, DetConsignEntEntity.class);*/
        /**
         * ? Introduce cross-package of physical and chemical orders
         */
        DetTestreportEntity dntReportTaskEntity = (DetTestreportEntity) baseDao.load(gid, DetTestreportEntity.class);
        String consignGid = dntReportTaskEntity. getConsignid();
        DetConsignPct consignPctEntity = (DetConsignPct) baseDao.load(consignGid, DetConsignPct.class);

        String code1 = consignPctEntity.getCode() + ".docx";


        //Upload the report instance file to the file server
        FileInputStream inputStream = new FileInputStream(url);
        String fileID = distributedFileServiceClientService.loadupLocalFile2FileServerWithFullPath(code, inputStream, code1);

      /* String finalMargeName=url;
        finalMargeName=url.substring(0,url.lastIndexOf(".")) + "pdf";
        specifiedname=consignPctEntity.getCode() + ".pdf";
        File file = new File(finalMargeName);
        FileOutputStream os = new FileOutputStream(file);
        com.aspose.words.Document doc= new com.aspose.words.Document(url);
        doc. updateFields();
        doc. save(os, SaveFormat. PDF);
        os. close();*/
        //Upload the report instance file to the file server
// FileInputStream inputStreampdf = new FileInputStream(code1);
// String filePDFID = distributedFileServiceClientService.loadupLocalFile2FileServerWithFullPath(code, inputStreampdf, specifiedname);
        inputStream. close();

        /* EntReportTaskEntity entReportTaskEntity = (EntReportTaskEntity) baseDao.load(gid, EntReportTaskEntity.class);*/

        dntReportTaskEntity.setReportfile(fileID);
        /* entReportTaskEntity.setPdfid(filePDFID);*/
        baseDao.update(dntReportTaskEntity);

        return fileID;
    }

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHome pageOverview 108208 people are studying systematically