java word转pdf、word中关键字位置插入图片 工具类

07-17 1051阅读

java word转pdf、word中关键字位置插入图片 工具类

java word转pdf、word中关键字位置插入图片 工具类
(图片来源网络,侵删)

1.pom依赖

		
			org.apache.poi
			poi
			3.15
		
		
			org.apache.poi
			poi-ooxml
			3.15
		
		
			org.apache.poi
			poi-scratchpad
			3.15
		
		
			org.apache.poi
			ooxml-schemas
			1.4
		
		
			org.apache.poi
			poi-ooxml-schemas
			3.15

2.依赖jar包

		
			com.aspose
			aspose-words
			15.8.0
			system
			${pom.basedir}/src/main/webapp/WEB-INF/lib/aspose-words-15.8.0-jdk16.jar
		

aspose-words是需要在我们项目中引入的,并且使用时要在resouces目录下导入license.xml文件,否则生成的文件抬头会有红色的license信息。

aspose-words-15.8.0-jdk16.jar 与license.xml

3.工具类

import com.aspose.words.*;
import com.aspose.words.Document;
import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * @author dume
 * @ClassName WordToPdf
 * @description: TODO
 * @date 2024年07月02日
 * @version: 1.0
 */
public class WordUtils {
    private static Logger logger = LoggerFactory.getLogger(WordUtils.class);
    private static final String BASE_PATH = WordUtils.class.getClassLoader().getResource("").getPath();
    /**
     *  Word转Pdf
     * @param sourcePath 原路径
     * @param targetPath 转出路径
     * @return
     */
    public static boolean  WordToPdf(String sourcePath,String targetPath){
        FileOutputStream os = null;
        try{
            // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            logger.info("license验证失败");
            return false;
        }
        File file = new File(targetPath);
         os = new FileOutputStream(file);
        FontSettings.setFontsFolder(BASE_PATH, true);
        FontSettings.setDefaultFontName("STFANGSO");
        Document doc = new Document(sourcePath);
        doc.save(os, SaveFormat.PDF);
        os.flush();
        os.close();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        }finally {
            if(os!=null){
               try{
                   os.close();
               }catch (Exception e){
               }
            }
        }
       return true;
    }
    /**
     *  word中插入图片方法
     * @param sourcePath  word路径
     * @param imgPath     图片路径
     * @param keyWords    关键字
     * @return   插入是否成功
     */
    public static boolean InsertImg(String sourcePath,String imgPath,String keyWords){
        try{
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            if (!getLicense()) {
                logger.info("license验证失败");
                return false;
            }
            Document doc = new Document(sourcePath);
            DocumentBuilder builder = new DocumentBuilder(doc);
            //插入图片的方法
            NodeCollection runs = doc.getChildNodes(NodeType.PARAGRAPH, true);
            for (int i = 0; i 
                
                
                
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]