java generate custom certificate Picture 2 - the word template to modify the contents of custom content

The next generation of custom data to complete the picture of the certificate Part II: Replace the template fields docx format for custom data

 

Specific examples of the code and herein  https://github.com/xuhaojin/certificate-generator

 

code show as below:

package com.x.certificate.doc;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;


import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.IRunBody;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlCursor;
importorg.apache.xmlbeans.XmlException;
 Import org.apache.xmlbeans.XmlObject;
 Import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; 


Import com.google.common.collect.Lists;
 Import com.x.certificate. doc.domain.CertificateData;
 Import com.x.certificate.doc.domain.CertificateField; 


/ ** 
* copy the certificate doc template file and modify the contents of a custom data 
* @author xuhaojin 
* @version [version number, 2020 March 15]
 * / 
public  class DocOperator { 


    public  static File toCumstomDoc (the templatePath String, String OutputPath, certificateData certificateData)
            throws IOException, XmlException {
        XWPFDocument document = new XWPFDocument(new FileInputStream(templatePath));


        Set<String> keys = certificateData.getKeys();


        for (XWPFParagraph paragraph : document.getParagraphs()) {
            XmlCursor cursor = paragraph.getCTP().newCursor();
            cursor.selectPath(
                    "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//*/w:txbxContent/w:p/w:r");


            List<XmlObject> xmlObjects = toXmlObjects(cursor);


            for (XmlObject xmlObject : xmlObjects) {
                CTR ctr = CTR.Factory.parse(xmlObject.xmlText());
                XWPFRun bufferrun = new XWPFRun(ctr, (IRunBody) paragraph);
                String text = bufferrun.getText(0);
                String conformingKey = containsKey(text, keys);
                if (conformingKey != null) {
                    CertificateField fieldInfo = certificateData.getValue(conformingKey);
                    text = text.replace(toTemplateKey(conformingKey), fieldInfo.getContent());
                    bufferrun.setFontSize(fieldInfo.getFontSize());
                    bufferrun.setFontFamily(fieldInfo.getFontFamily());
                    bufferrun.setText(text, 0);
                    bufferrun.setBold(fieldInfo.getIsBold());
                }


                xmlObject.set(bufferrun.getCTR());
            }
        }


        FileOutputStream out = new FileOutputStream(outputPath);
        document.write(out);


        out.close();
        document.close();


        return new File(outputPath);
    }


    public static List<XmlObject> toXmlObjects(XmlCursor docXmlCursor) {
        List<XmlObject> xmlObjects = Lists.newArrayList();


        while (docXmlCursor.hasNextSelection()) {
            docXmlCursor.toNextSelection();
            xmlObjects.add(docXmlCursor.getObject());
        }


        return xmlObjects;
    }


    public static String containsKey(String text, Set<String> keys) {
        String conforming = null;


        if (StringUtils.isEmpty(text)) {
            return conforming;
        }


        for (String key : keys) {
            if (text.contains(key)) {
                conforming = key;
                break;
            }
        }


        return conforming;
    }


    public static String toTemplateKey(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }


        return "${" + key + "}";
    }


    public static String addBlankSpace(String text) {
        StringBuffer sb = new StringBuffer();


        if (text == null) {
            return null;
        }


        char[] chars = text.toCharArray();


        String regex = "[\u4E00-\u9FA5]{1}";
        for (char aChar : chars) {
            String str = aChar + "";
            if (StringUtils.isBlank(str)) {
                continue;
            }


            sb.append(aChar);


            if (str.matches(regex)) {
                sb.append(" ");
            }
        }


        return sb.toString();
    }


    public static void main(String[] args) throwsIOException, an XmlException {
        The templatePath String = "C: \\ a1579 the Users \\ \\ \\ Template.docx Desktop" ; 
        String OutputPath = "C: \\ a1579 the Users \\ \\ \\ custom.docx Desktop" ; 
        CertificateData Data = new new CertificateData ( ); 
        data.put ( new new CertificateField ( "holder", addBlankSpace ( "Joe Smith"), 34 )); 
        data.put ( new new CertificateField ( "certificate of Chinese information", "congratulations on completing \" qualified helicopter pilot jobs \ "special permits issued this training course.!", 18 )); 
        data.put ( new new CertificateField ( "certificate information in English" ,
                 "Congratulations ON Completion of training program of \"));
        data.put ( new new CertificateField ( "Certificate No.", "100 224 512", 18 )); 
        data.put ( new new CertificateField ( "Certificate Signing", addBlankSpace ( "John Doe"), 25 )); 
        data.put ( new new CertificateField ( "certificate date", "2020 March 21", 15 )); 
        toCumstomDoc (the templatePath, OutputPath, the Data); 
    } 


}

 

Wherein the primary reference poi jar package, quoted below:

           <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
           </dependency>
           <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.7 </version>
           </dependency>
           <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>25.1-jre</version>
           </dependency>

 

Docx certificate template is:

 

Generated by the certificate Certificate as a custom docx:

 

docx format with such certificates of custom data to generate good

 

Guess you like

Origin www.cnblogs.com/xhj123/p/12539642.html