吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1013|回复: 2
收起左侧

[学习记录] word、excel、ppt转pdf使用记录

[复制链接]
qiuban 发表于 2023-11-13 14:18
话不多说,直接上代码,亲测可用,但是Linux需要添加字体,包在网上搜,太大无法上传<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
</dependency>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>21.8.0</version>
</dependency><dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>15.9.0</version>
</dependency>import com.aspose.cells.Workbook;
import com.aspose.words.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.springframework.http.MediaType;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;

public class PdfConverUtil {

   
/**
     * @Param inputStream 源文件输入流
     * @param fileName    pdf文件名称
     **/
    public static InputStream wordToPdf(InputStream inputStream, String fileName) throws Exception {
        
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
        
OutputStream outputStream = fileItem.getOutputStream();
        
// 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            
return null;
        }
        
// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法
        com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
        
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
        FontSettings.setFontsFolder("/usr/share/fonts/chinese", false);
        
doc.save(outputStream, SaveFormat.PDF);
        
outputStream.flush();
        
outputStream.close();
        
return fileItem.getInputStream();
    }


   
/**
     * excel pdf
     *
     * @param inputStream   excel文件
     * @param convertSheets 需要转换的sheet
     * @param fileName      pdf名称
     */
    public static InputStream excelToPdf(InputStream inputStream, int[] convertSheets, String fileName) throws Exception {
        
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
        
OutputStream outputStream = fileItem.getOutputStream();
        
// 验证 License
        if (!getExeclLicense()) {
            
return null;
        }
        
Workbook wb = new Workbook(inputStream);
        
com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();
        
pdfSaveOptions.setOnePagePerSheet(true);
        
if (null != convertSheets) {
            
printSheetPage(wb, convertSheets);
        }
        
wb.save(outputStream, pdfSaveOptions);
        
outputStream.flush();
        
outputStream.close();
        
return fileItem.getInputStream();
    }

   
/**
     * ppt pdf
     *
     * @param inputStream ppt文件
     * @param fileName    pdf名称
     */
    public static InputStream pptToPdf(InputStream inputStream, String fileName) throws Exception {
        
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
        
OutputStream outputStream = fileItem.getOutputStream();
        
// 验证 License
        if (!getPPTLicense()) {
            
return null;
        }
        
com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(inputStream);//输入pdf路径
        pres.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
        outputStream.flush();
        outputStream.close();
        
return fileItem.getInputStream();
    }

   
/**
     * 隐藏workbook中不需要的sheet页。
     *
     * @param sheets 显示页的sheet数组
     */
    private static void printSheetPage(Workbook wb, int[] sheets) {
        
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
            wb.getWorksheets().get(i).setVisible(
false);
        }
        
if (null == sheets || sheets.length == 0) {
            wb.getWorksheets().get(
0).setVisible(true);
        }
else {
            
for (int i = 0; i < sheets.length; i++) {
                wb.getWorksheets().get(i).setVisible(
true);
            }
        }
    }

   
// 官方文档的要求 无需理会
    public static boolean getLicense() {
        
boolean result = false;
        
try {
            String s =
"<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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is =
new ByteArrayInputStream(s.getBytes());
            License aposeLic =
new License();
            aposeLic.setLicense(is);
            result =
true;
        }
catch (Exception e) {
            e.printStackTrace();
        }
        
return result;
    }

   
public static boolean getExeclLicense() {
        
boolean result = false;
        
try {
            String s =
"<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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is =
new ByteArrayInputStream(s.getBytes());
            com.aspose.cells.License aposeLic =
new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result =
true;
        }
catch (Exception e) {
            e.printStackTrace();
        }
        
return result;
    }

   
public static boolean getPPTLicense() {
        
boolean result = false;
        
try {
            String s =
"<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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is =
new ByteArrayInputStream(s.getBytes());
            com.aspose.slides.License aposeLic =
new com.aspose.slides.License();
            aposeLic.setLicense(is);
            result =
true;
        }
catch (Exception e) {
            e.printStackTrace();
        }
        
return result;
    }

   
public static void main(String[] args) throws IOException {
        Charset defaultCharset = Charset.defaultCharset();
        String charsetName = defaultCharset.name();

        
if (charsetName.equalsIgnoreCase("UTF-8")) {
            System.out.println(
"Java环境的编码是UTF-8");
        }
else if (charsetName.equalsIgnoreCase("GBK")) {
            System.out.println(
"Java环境的编码是GBK");
        }
else {
            System.out.println(
"Java环境的编码是" + charsetName);
        }
        
long old = System.currentTimeMillis();
        
if (!getPPTLicense()) {
            System.out.println(
"PDF格式转化失败");
        }
        FileInputStream inputStream =
new FileInputStream("C:\\Users\\fanbo\\Desktop\\bbb.ppt");
        FileOutputStream byteArrayOutputStream =
new FileOutputStream("C:\\Users\\fanbo\\Desktop\\bbb1.pdf");
        com.aspose.slides.Presentation pres =
new com.aspose.slides.Presentation(inputStream);//输入pdf路径

        pres.save(byteArrayOutputStream, com.aspose.slides.SaveFormat.Pdf);
        byteArrayOutputStream.close();
        
//ArrayList<String> imageUrllist = new ArrayList<String>(); // 图片list集合
        //imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\001.png");
        //imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\不上班001.png");
        //imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\不上班002.png");
        //File file = ImgToPdf(imageUrllist, "C:\\Users\\fanbo\\Desktop\\img.pdf");// 生成pdf
        //file.createNewFile();

        long now = System.currentTimeMillis();
        System.out.println(
"共耗时:" + ((now - old) / 1000.0) + "");
    }
}

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
lizonglin313 + 1 + 1 我很赞同!
oldcat8876 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yaphoo 发表于 2023-11-13 17:26
牛人,高手在论坛
 楼主| qiuban 发表于 2023-11-14 09:46
yaphoo 发表于 2023-11-13 17:26
牛人,高手在论坛

谢谢些,可不敢这么说,也是网上找的,参考的,总结了一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 17:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表