aspose word for Java handles word templates, replacing strings or pictures

Posted by unkwntech on Mon, 20 Jan 2020 15:53:19 +0100

I've been looking for a long time for Java to deal with word. poi can deal with docx fairly well. When dealing with DOC, there are always various bug s. Spice.doc is a bit slow, and it's charged. The free version has various limitations. jacob needs to rely on the server to install Microsoft word, and there will be conflicts during the concurrency. Finally, I found a relatively good aspose word. Although there are charges, there are available licenses that can be used directly. Support doc and docx.

Required jar package:

Links: https://pan.baidu.com/s/1KERDQ4bb1Mn0tT4S9raPrw 
Extraction code: zc9j

Code:

package aspose;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.IReplacingCallback;
import com.aspose.words.Node;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplacingArgs;

public class ReplaceAndInsertImage implements IReplacingCallback{
	public String url;
	public ReplaceAndInsertImage(String url){
        this.url = url;
    }
	@Override
	public int replacing(ReplacingArgs e) throws Exception {
		//Get current node
         Node node = e.getMatchNode();
        //Get current document
         Document document =  (Document) node.getDocument();
        DocumentBuilder builder = new DocumentBuilder(document);
//        //Move the cursor to the specified node
        builder.moveTo(node);
//        //Insert picture
        builder.insertImage(url);
        return ReplaceAction.REPLACE;
	}
}
package aspose;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.Range;

public class AsPoseWordUtil {
	private static License aposeLic = new License();
	private static boolean getlic(){
		if(!aposeLic.getIsLicensed()){
			InputStream is = AsPoseWordUtil.class.getClassLoader()
					.getResourceAsStream("aspose/asposewordLiences.xml"); 
			try {
				aposeLic.setLicense(is);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	/**
	 * aspose word Replace text / pictures.
	 * @param url  Original file path
	 * @param saveurl Save path
	 * @param params {key:value,"$Year $":" 2020 "," signature $1$":"$pic:d/qz1.jpg"} key is the string to be replaced, value is the string to be replaced, replace with picture, then value is $pic: + picture path
	 * @return
	 */
	public static boolean replace(String url,String saveurl,Map<String, String> params){
		if(!getlic()){
			return false;
		}
		File file = new File(url);
		if(!file.exists()){
			return false;
		}
		try {
			Document doc = new Document(url);
			Range range = doc.getRange();
			for(String key:params.keySet()){
				String value = params.get(key);
				if(value.startsWith("$pic:")){
					value = value.substring(5);
					key = key.replace("\\", "\\\\");
					key = key.replace("$", "\\$");
					key = key.replace("[", "\\[");
					key = key.replace("]", "\\]");
					key = key.replace("(", "\\(");
					key = key.replace(")", "\\)");
					key = key.replace("|", "\\|");
					key = key.replace("+", "\\+");
					key = key.replace("?", "\\?");
					key = key.replace("*", "\\*");
					range.replace( Pattern.compile(key),new ReplaceAndInsertImage(value) , false);
				}else{
					range.replace(key, value, true, false);
				}
			}
			doc.save(saveurl);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("[$number $]", "BH-2020-01-20");
		params.put("[$pic$]", "$pic:D:/test/sign.jpg");
		AsPoseWordUtil.replace("D:/test/poi Replacement source.docx", "D:/test/aspose After replacement.docx", params);
		AsPoseWordUtil.replace("D:/test/poi Replacement source.doc", "D:/test/aspose After replacement.doc", params);
	}
}

asposewordLiences.xml:

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
  </Data>
  <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>

Design sketch:

After replacement:

 

 

Published 13 original articles, won praise 11, visited 20000+
Private letter follow

Topics: Java xml