可预览文件转PDF-使用Aspose

Word、Excel、TXT、PDF文件等转PDF--使用Aspose

需求背景

支持各种文件上传,并支持部分文件的预览。
由于项目前端的文件预览需要支持以下格式,但是ppt、txt无法预览,需要转换为PDF查看。
支持的文件类型
1.docx、doc文件起初使用documents4j,因为项目是部署在Linux上的,本地转换是正常的,在Linux上执行需要ms office,执行不了vbs,会报错,最后换成了Aspose。
2.Excel文件用documents4j转换时效果不好,列数太多,宽度太宽会分页显示,也换成Aspose来处理。
word和excel都参考了这个地址 传送门
3.ppt使用 itextpdf 和 poi,转换为图片再写入PDF中。

Word、Excel文档转PDF

下载aspose-word的jar包,在项目的resource下创建lib,放入jar包。网上找个license.xml也放在resource下。没有license会有水印。
maven项目,需要在pom文件中引入依赖,项目打包时也要把jar包打上。

<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>cells</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>words</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/aspose-words-16.8.0.jar</systemPath>
        </dependency>

项目结构
原帖是写的接口,在我的项目中我写成了工具类,方便调用。
1.载入license授权文件,License导入的是com.aspose.words.License下的,excel导入的是cells下的。

    protected static boolean matchLicense() {
        boolean result = false;
        InputStream is = Word2Pdf.class.getClassLoader().getResourceAsStream("licenses/license.xml");
        License wordLicense = new License();
        try {
            wordLicense.setLicense(is);
            result = true;
        } catch (Exception e) {
            log.warn("载入word授权文件失败");
        }
        return result;
    }

2.匹配文件类型,doc、docx、txt都支持

   protected static boolean matchFileType(String fileName) {
   		//fileTypes为static变量
        fileTypes.add("doc");
        fileTypes.add("docx");
        fileTypes.add("txt");
        int index = fileName.indexOf(".");
        String suffix = fileName.substring(index+1, fileName.length());
        if(fileTypes.contains(suffix)) {
            return true;
        }
        return false;
    }

3.word 文件转换,依次调用

public static void convert2PDF(File sourceFile, File targetFile) {
        FileOutputStream os = null;
        Document doc = null;
        try {
            os = new FileOutputStream(targetFile);
            doc = new Document(sourceFile.getAbsolutePath());
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("输出到" + targetFile.getAbsolutePath() + "错误:" + e);
        }finally {
        //关闭流
        ...
        }
    }

4.excel文件转换,设置表页的列宽度自适应

public static void convert2PDF(File sourceFile, File targetFile) {
        FileOutputStream fileOs = null;
        Workbook wb = null;
        try {
            fileOs = new FileOutputStream(targetFile);
            wb = new Workbook(sourceFile.getAbsolutePath());
            WorksheetCollection worksheets = wb.getWorksheets();
            Iterator<Worksheet> iterator = worksheets.iterator();
            while (iterator.hasNext()){
                Worksheet next = iterator.next();
                setColumnWithAuto(next);
            }
            PdfSaveOptions saveOptions = new PdfSaveOptions();
            saveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(fileOs, saveOptions);

        } catch (FileNotFoundException e) {
            log.error("输出到"+targetFile.getAbsolutePath()+"错误:"+e);
        } catch (Exception e) {
            log.error("转换excel出错:"+e);
        }finally {
        //.....关闭流
        }
    }
 /**
     * 设置表页的列宽度自适应
     * @param sheet
     */
    private static void setColumnWithAuto(Worksheet sheet) {
        Cells cells = sheet.getCells();
        int columnCount = cells.getMaxColumn() + 1;
        //获取表页的最大列数
        int rowCount = cells.getMaxRow();
        //获取表页的最大行数
        for (int col = 0; col < columnCount; col++)
        {
            try {
                sheet.autoFitColumn(col, 0, rowCount);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int col = 0; col < columnCount; col++)
        {
            int pixel = cells.getColumnWidthPixel(col)+30;
            if (pixel > 255)
            {
                cells.setColumnWidthPixel(col, 255);
            }
            else
            {
                cells.setColumnWidthPixel(col, pixel);
            }
        }
    }

PPT转PDF

抄袭的,贴上,下次用😋, 有ppt和pptx两种格式

package com.bridata.flightarea.document.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class PPT2PDFUtils {

    protected static final Logger log = LoggerFactory.getLogger(PPT2PDFUtils.class);

    public static int width  = 720;
    public static int height = 540;

    /**
     * 将pptx转换为图片,直接保存在内存中,ppt很大时可能会oom!!!
     *
     * @param is ppt 输入流
     * @return
     * @throws IOException
     */
    public static List<byte[]> pptx2Image(InputStream is) throws IOException {
        // 获取系统可用字体
        GraphicsEnvironment e          = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[]            fontNames  = e.getAvailableFontFamilyNames();
        List                availFonts = CollectionUtils.arrayToList(fontNames);

        XMLSlideShow ppt      = new XMLSlideShow(is);
        Dimension    pgSize   = ppt.getPageSize();
        int          pageSize = ppt.getSlides().size();
        List<byte[]> imgList  = new ArrayList<>(pageSize);

        log.info("ppt 总页数:{}, 尺寸: width={},height={}", pageSize, pgSize.width, pgSize.height);
        for (int i = 0; i < pageSize; i++) {
            //防止中文乱码
            for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {
                if (shape instanceof XSLFTextShape) {
                    XSLFTextShape tsh = (XSLFTextShape) shape;
                    for (XSLFTextParagraph p : tsh) {
                        for (XSLFTextRun r : p) {
                            String fontFamily = r.getFontFamily();
                            fontFamily = "宋体";
                            r.setFontFamily(fontFamily);
                        }
                    }
                }
            }
            BufferedImage img      = new BufferedImage(pgSize.width, pgSize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D    graphics = img.createGraphics();
            // clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgSize.width, pgSize.height));
            // render
            ppt.getSlides().get(i).draw(graphics);
            // save the output
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(img, "png", out);
            imgList.add(out.toByteArray());
            IOUtils.closeQuietly(out);
        }
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
        return imgList;
    }

    /**
     * 将ppt转换为图片,直接保存在内存中,ppt很大时可能会oom!!!
     *
     * @param is ppt 输入流
     * @return
     * @throws IOException
     */
    public static List<byte[]> ppt2Image(InputStream is) throws IOException {
        // 获取系统可用字体
        GraphicsEnvironment e          = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[]            fontNames  = e.getAvailableFontFamilyNames();
        List                availFonts = CollectionUtils.arrayToList(fontNames);

        HSLFSlideShow ppt      = new HSLFSlideShow(new HSLFSlideShowImpl(is));
        Dimension     pgsize   = ppt.getPageSize();
        int           pageSize = ppt.getSlides().size();
        List<byte[]>  imgList  = new ArrayList<>(pageSize);

        log.info("ppt 总页数:{}, 尺寸: width={},height={}", pageSize, pgsize.width, pgsize.height);
        for (int i = 0; i < pageSize; i++) {
            //防止中文乱码
            for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
                if (shape instanceof HSLFTextShape) {
                    HSLFTextShape tsh = (HSLFTextShape) shape;
                    for (HSLFTextParagraph p : tsh) {
                        for (HSLFTextRun r : p) {
                            String fontFamily = r.getFontFamily();
                            fontFamily = "宋体";
                            r.setFontFamily(fontFamily);
                        }
                    }
                }
            }
            BufferedImage img      = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D    graphics = img.createGraphics();
            // clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
            // render
            ppt.getSlides().get(i).draw(graphics);
            // save the output
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(img, "png", out);
            imgList.add(out.toByteArray());
            IOUtils.closeQuietly(out);
        }
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
        return imgList;
    }

    /**
     * 将图片转换成pdf
     *
     * @return
     * @throws Exception
     */
    public static byte[] img2PDF(List<byte[]> images) throws Exception {
        com.itextpdf.text.Rectangle pageSize = new com.itextpdf.text.Rectangle(0, 0, width, height); // 自定义页面大小
        Document doc= new Document(pageSize, 0, 0, 0, 0);
        log.info(">>>>A4尺寸:width={},height={}", pageSize.getWidth(), pageSize.getHeight());
        ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
        PdfWriter.getInstance(doc, pdfOut);
        doc.open();
        float scale = pageSize.getWidth() / width;
        for (byte[] image : images) {
            com.itextpdf.text.Image pic = com.itextpdf.text.Image.getInstance(image);
            pic.setScaleToFitLineWhenOverflow(true);
            pic.scaleToFit(pageSize.getWidth(), height * scale);
            doc.add(pic);
        }
        doc.close();
        byte[] pdf = pdfOut.toByteArray();
        IOUtils.closeQuietly(pdfOut);
        return pdf;
    }

    /**
     * ppt转化为pdf
     * @param is ppt 输入流
     * @return pdf 字节文件
     * @throws Exception
     */
    public static byte[] ppt2PDF(InputStream is) throws Exception {
        return img2PDF(ppt2Image(is));
    }
    /**
     * pptx转化为pdf
     * @param is pptx 输入流
     * @return pdf 字节文件
     * @throws Exception
     */
    public static byte[] pptx2PDF(InputStream is) throws Exception {
        return img2PDF(pptx2Image(is));
    }
}

图片转PDF

这个比较简单,用itextpdf,创建一个document,把图片转成Element中的Image,设置一些格式,比如居中之类的,写入document中。
再用document对象创建pdfWriter实例,输出即可。

private void imgToPdf(String input, OutputStream outputStream) throws IOException, DocumentException {
        Document document = new Document();
        //创建document对象
        document.setMargins(0, 0, 0, 0);
        //创建pdfWriter实例
        PdfWriter.getInstance(document, outputStream);
        //打开文档
        document.open();
        //在文档中增加图片
        Image img = Image.getInstance(input);
        img.setAlignment(Image.ALIGN_CENTER);
        document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
        document.newPage();
        document.add(img);
        document.close();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于Aspose.Words,可以使用它将Word文档换为PDF格式,然后使用PDF.js进行在线预览Aspose.Words是一个功能强大的文档处理库,可以帮助开发人员处理和换多种文档格式。通过使用Aspose.Words将Word文档换为PDF,可以确保预览的准确性和稳定性。 换步骤如下: 1. 使用Aspose.Words将服务器存储的Word文档换为PDF格式。 2. 使用PDF.js来加载和显示换后的PDF文件,从而实现在线预览。 这种方法相对于使用ce.office.extension将Word文件换为HTML预览,能够避免一些格式、图片和字体错乱的问题,因为PDF是一种更稳定和可靠的文档格式。 需要注意的是,使用Aspose.Words进行WordPDF时,可能会遇到试用版自动加水印的问题。如果需要去除水印,可以参考相应的教程进行操作。但是请注意,我们在这里只提供思路和参考,具体操作还需要根据你的实际需求和情况进行调整。 总结起来,aspose.words可以通过将Word换为PDF格式,然后使用PDF.js进行在线预览。这种方法可以提供更准确和稳定的预览效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Net Core3.1使用Aspose.Words18.4将WordPDF](https://blog.csdn.net/xiaomai4343/article/details/125384428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【2020.12】Aspose.words 20.12最新版Crack,wordpdf去水印方法](https://blog.csdn.net/xiaostuart/article/details/111479549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值