Java使用aspose把word文档转换为HTML格式

一、需求说明

把word文档转换为HTML格式,要求:css样式和图片保留,解决图片被压缩的问题

二、技术选型

使用aspose库进行实现,但是aspose是收费的,所以使用第三方版本进行开发,maven配置如下

   
        
            com.luhuiguo
            aspose-words
            23.1
        

如果maven无法正常引入,需要加上仓库地址

  
        
            AsposeJavaAPI
            https://repository.aspose.com/repo/
        
    

三、功能实现

  /**
     * 解析word
     */
    public static String wordAnalysis(String filePath) {

        try {
            // 把文件转化为Document对象
            com.aspose.words.Document doc = new com.aspose.words.Document(filePath);
            //注意:此for循环可以不写,如果不写  导出的图片宽高将会被压缩,加上这段代码图片会保持原尺寸
            for (Shape shape : (Iterable) doc.getChildNodes(NodeType.SHAPE, true)) {
                if (shape.hasImage()) {
                    // 获取图片的原始尺寸
                    ImageSize imageSize = shape.getImageData().getImageSize();
                    // 设置图片的宽度和高度,以保持原始尺寸
                    shape.setWidth(imageSize.getWidthPoints());
                    shape.setHeight(imageSize.getHeightPoints());
                }
            }
            // 设置转化的格式,HtmlSaveOptions转换为HTML格式
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            //图片是否保存为base64,如果为true,图片将不会保存在本地,而是以base64的方式引用
            saveOptions.setExportImagesAsBase64(false);
            // 将所有word中的图片放在临时文件夹中,并将html中的链接替换为临时文件夹中绝对路径
            String imagePath = filePath.substring(0, filePath.lastIndexOf("."));
            saveOptions.setImagesFolder(imagePath);
            // 保存为html文件
            // doc.save("output.html", options);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //保存为html字符串
            doc.save(baos, saveOptions);
            return baos.toString();
            // 将html文件转化为Document,方便后续使用jsoup的操作
            //Document htmlDoc = Jsoup.parse(baos.toString());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

四、实战

        1.带格式和图片的word文档

Java使用aspose把word文档转换为HTML格式_第1张图片

2.执行代码

   public static void main(String[] args) throws IOException {
        System.out.println(wordAnalysis("C:\\Users\\Administrator\\Desktop\\1.docx"));
    }

3.得到结果

Java使用aspose把word文档转换为HTML格式_第2张图片

4.本地图片文件

Java使用aspose把word文档转换为HTML格式_第3张图片

五、总结

1.转化过程中图片会默认压缩为jpeg格式并且宽高会缩小,如果不想改变图片分辨率就需要设置为原尺寸

//注意:此for循环可以不写,如果不写  导出的图片宽高将会被压缩,加上这段代码图片会保持原尺寸
            for (Shape shape : (Iterable) doc.getChildNodes(NodeType.SHAPE, true)) {
                if (shape.hasImage()) {
                    // 获取图片的原始尺寸
                    ImageSize imageSize = shape.getImageData().getImageSize();
                    // 设置图片的宽度和高度,以保持原始尺寸
                    shape.setWidth(imageSize.getWidthPoints());
                    shape.setHeight(imageSize.getHeightPoints());
                }
            }

2.如果有图片格式需求,可以通过实现 IResourceSavingCallback
接口来实现。这个接口允许您在将文档保存为 HTML 时控制 Aspose.Words 如何保存外部资源
import com.aspose.words.Document;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.IResourceSavingCallback;
import com.aspose.words.ResourceSavingArgs;

// 实现 IResourceSavingCallback 接口
class MyResourceSavingCallback implements IResourceSavingCallback {
    public void resourceSaving(ResourceSavingArgs args) throws Exception {
        // 自定义资源保存逻辑,例如指定图像保存的路径和文件名
        if (args.getResourceType() == ResourceType.IMAGE) {
            // 设置图像的保存路径和文件名
            args.setResourceFileName("custom_image_path/" + args.getResourceFileName());
        }
    }
}

public class CustomResourceSaving {
    public static void main(String[] args) throws Exception {
        // 加载 Word 文档
        Document doc = new Document("input.docx");
        
        // 创建 HtmlSaveOptions 对象
        HtmlSaveOptions options = new HtmlSaveOptions();
        
        // 创建 MyResourceSavingCallback 对象并设置到 HtmlSaveOptions
        options.setResourceSavingCallback(new MyResourceSavingCallback());
        
        // 保存文档到 HTML
        doc.save("output.html", options);
    }
}

你可能感兴趣的:(JAVA开发,word,html,java,后端)