Java 使用aspose-words实现word转pdf、html、图片

Java word转html、图片

1.下载aspose-words-20.12-jdk17-crack.jar

资源链接 https://download.csdn.net/download/qq_43601009/88466502

此jar包不包含在maven中心仓库中,所以下载手动下载并本地引入

2.将jar包放到项目中

建立src的同级目录lib,将下载好的jar包放到lib下

这里很多博客都写了在resources下创建lib,本人试过后会一直报错,放在src同级的lib可以正常运行。

3.将本地jar包以maven的方式引入

3.1

  com.aspose aspose-words system 15.8.0 ${basedir}/lib/aspose-words-20.12-jdk17-crack.jar 

3.2

 org.springframework.boot spring-boot-maven-plugin   true  

4.AsposeUtils

此工具可以word转图片、html,但是在转html时,可能会因为图片导致排版较乱的问题。

import com.aspose.words.Document;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AsposeUtils { //outputStream转inputStream
    public static ByteArrayInputStream parse(OutputStream out) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
    /**
     * word和txt文件 转换 图片
     *
     * @param inputStream
     * @param
     * @return
     * @throws Exception
     */
    public static List wordToImg(InputStream inputStream) throws Exception { try { Date start = new Date();
            Document doc = new Document(inputStream);
            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
            options.setPrettyFormat(true);
            options.setUseAntiAliasing(true);
            options.setUseHighQualityRendering(true);
            int pageCount = doc.getPageCount();
            //生成前pageCount张,这可以限制输出长图时的页数(方法入参可以传值pageNum)
           /*if (pageCount > pageNum) {
               pageCount = pageNum;
           }*/
            List imageList = new ArrayList();
            for (int i = 0; i < pageCount; i++) { OutputStream output = new ByteArrayOutputStream();
                options.setPageIndex(i);
                doc.save(output, options);
                ImageInputStream imageInputStream = javax.imageio.ImageIO.createImageInputStream(parse(output));
                imageList.add(javax.imageio.ImageIO.read(imageInputStream));
            }
            List imageList2 = new ArrayList();
            //这个重新生成新的图片是因为直接输出的图片底色为红色
            for (int j = 0; j < imageList.size(); j++) { // 生成新图片
                BufferedImage destImage = imageList.get(j);
                int w1 = destImage.getWidth();
                int h1 = destImage.getHeight();
                destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = (Graphics2D) destImage.getGraphics();
                g2.setBackground(Color.LIGHT_GRAY);
                g2.clearRect(0, 0, w1, h1);
                g2.setPaint(Color.RED);
                // 从图片中读取RGB
                int[] ImageArrayOne = new int[w1 * h1];
                ImageArrayOne = imageList.get(j).getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
                destImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
                imageList2.add(destImage);
            }
            Date end = new Date();
            long l = end.getTime() - start.getTime();
            long hour = l / (1000 * 60 * 60);
            long min = (l - hour * (1000 * 60 * 60)) / (1000 * 60);
            long s = (l - hour * (1000 * 60 * 60) - min * 1000 * 60) / (1000);
            long ss = (l - hour * (1000 * 60 * 60) - min * 1000 * 60 - s * 1000) / (1000 / 60);
            System.out.println("word转图片时间:" + min + "分" + s + "秒" + ss + "毫秒");//hour+"小时"+
            return imageList2;
        } catch (Exception e) { e.printStackTrace();
            throw e;
        }
    }
    /**
     * 合并任数量的图片成一张图片
     *
     * @param isHorizontal true代表水平合并,fasle代表垂直合并
     * @param imgs         待合并的图片数组
     * @return
     * @throws IOException
     */
    public static BufferedImage mergeImage(boolean isHorizontal, List imgs) throws IOException { // 生成新图片
        BufferedImage destImage = null;
        // 计算新图片的长和高
        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
        // 获取总长、总宽、最长、最宽
        for (int i = 0; i < imgs.size(); i++) { BufferedImage img = imgs.get(i);
            allw += img.getWidth();
            if (imgs.size() != i + 1) { allh += img.getHeight() + 5;
            } else { allh += img.getHeight();
            }
            if (img.getWidth() > allwMax) { allwMax = img.getWidth();
            }
            if (img.getHeight() > allhMax) { allhMax = img.getHeight();
            }
        }
        // 创建新图片
        if (isHorizontal) { destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
        } else { destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
        }
        Graphics2D g2 = (Graphics2D) destImage.getGraphics();
        g2.setBackground(Color.LIGHT_GRAY);
        g2.clearRect(0, 0, allw, allh);
        g2.setPaint(Color.RED);
        // 合并所有子图片到新图片
        int wx = 0, wy = 0;
        for (int i = 0; i < imgs.size(); i++) { BufferedImage img = imgs.get(i);
            int w1 = img.getWidth();
            int h1 = img.getHeight();
            // 从图片中读取RGB
            int[] ImageArrayOne = new int[w1 * h1];
            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
            if (isHorizontal) { // 水平方向合并
                destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            } else { // 垂直方向合并
                destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            }
            wx += w1;
            wy += h1 + 5;
        }
        return destImage;
    }
    /**
     * 当文档中含有中文字符时,该段代码的执行需要调用操作系统的本地字体库支持,否则所有中文字符都将乱码。
     * 

* 该段代码如果想要在Linux服务器上完美运行,需要给Linux服务器安装中文字体库 *

* 如何在Linux环境安装Windows字体库 * Java使用Spire.Pdf或Aspose-Words实现Word转换Pdf在Linux服务器上的中文乱码问题 * 解决: https://blog.csdn.net/neulily2005/article/details/106003527 */ public static boolean wordToPdf(String inPath, String outPath) { FileOutputStream os = null; try { long old = System.currentTimeMillis(); File file = new File(outPath); // 新建一个空白pdf文档 os = new FileOutputStream(file); Document doc = new Document(inPath); // Address是将要被转化的word文档 //排版问题解决(word文档在编辑的时候是采用的多页编辑。页面效果是单页,可是在apose将word文档转为pdf后就变成了两页) Document document = new Document();//新建一个空白pdf文档 document.removeAllChildren(); document.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);//保留样式 //表格样式处理 TableCollection tables = document.getFirstSection().getBody().getTables(); for (Table table : tables) { RowCollection rows = table.getRows(); table.setAllowAutoFit(false); for (Row row : rows) { CellCollection cells = row.getCells(); for (Cell cell : cells) { CellFormat cellFormat = cell.getCellFormat(); cellFormat.setFitText(false); cellFormat.setWrapText(true); } } } document.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } public static boolean wordToHtml(InputStream is, String outPath) { FileOutputStream os = null; try { HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML); opts.setExportXhtmlTransitional(true); opts.setExportImagesAsBase64(true); opts.setExportPageSetup(true); long old = System.currentTimeMillis(); File file = new File(outPath); // 新建一个空白pdf文档 os = new FileOutputStream(file); Document doc = new Document(is); // Address是将要被转化的word文档 doc.save(os, opts);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } catch (Exception e) { e.printStackTrace(); return false; } finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } }