1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 利用aspose实现ppt doc docx pptx xlsx xls txt 图片类型转pdf以及实现预

利用aspose实现ppt doc docx pptx xlsx xls txt 图片类型转pdf以及实现预

时间:2021-02-03 07:08:08

相关推荐

利用aspose实现ppt doc docx pptx xlsx xls txt 图片类型转pdf以及实现预

声明:对于本文章上的这个aspose相关的jar,切勿用于商业用途,后果我概不负责

1,先准备jar

链接:jar连接

提取码:bccn

下载后选中自己合适的

我使用的jar,而我的环境是jdk1.8

<dependency><groupId>com.aspose</groupId><artifactId>aspose-word</artifactId><version>18.10</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-cells</artifactId><version>18.9</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-slides</artifactId><version>19.6</version></dependency>

2,开始上主要代码:

主要是调用这个方法来选择

/*** 选择类型来转换* @param pdfUrl 目标pdf路径* @param officeUrl 实现要转换的文档* @param type 文档后缀* @param content pdf文字内容*/public static void getChange(String pdfUrl, String officeUrl, String type,String content) {if (type.equals("doc") || type.equals("docx")) {writePdf.wordToPdf(pdfUrl, officeUrl);} else if (type.equals("xls") || type.equals("xlsx")) {writePdf.excelToPdf(pdfUrl, officeUrl);} else if (type.equals("ppt")||type.equals("pptx")) {pptToPdf(pdfUrl, officeUrl);} else if (type.equals("txt")) {textToPdf(pdfUrl,content);}else if(type.equals("jpg")||type.equals("png")||type.equals("PNG")||type.equals("jpeg")){imgToPdf(pdfUrl,officeUrl);}}

ppt和pptx转pdf

private static void pptToPdf(String pdfUrl, String pptlUrl) {if (!PptLences.getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {Presentation ppt = new Presentation(pptlUrl);FileOutputStream fileOS = new FileOutputStream(new File(pdfUrl));ppt.save(fileOS, com.aspose.slides.SaveFormat.Pdf);fileOS.close();} catch (Exception e) {e.printStackTrace();}}

引用License验证(注意:是引用的import com.aspose.slides.License;)

public static boolean getLicense() {boolean result = false;try {InputStream is = WordLences.class.getClassLoader().getResourceAsStream("license.xml");// license.xml应放在..\WebRoot\WEB-INF\classes路径下License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}

xls和xlsx转pdf

private static void excelToPdf(String pdfUrl, String excelUrl) {if (!ExcelLences.getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {long old = System.currentTimeMillis();Workbook wb = new Workbook(excelUrl);// 原始excel路径FileOutputStream fileOS = new FileOutputStream(new File(pdfUrl));wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);fileOS.close();long now = System.currentTimeMillis();System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时} catch (Exception e) {e.printStackTrace();}}

引用License验证(注意:引用import com.aspose.cells.License;)

代码同上

doc和docx转pdf

private static void wordToPdf(String pdfUrl, String docxUrl) {if (!WordLences.getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生return;}File file = new File(pdfUrl); //新建一个pdf文档FileOutputStream os = null;try {os = new FileOutputStream(file);Document doc = new Document(docxUrl); //Address是将要被转化的word文档doc.save(os, com.aspose.words.SaveFormat.PDF);long now = System.currentTimeMillis();os.close();} catch (Exception e) {e.printStackTrace();}}

引用License验证(注意:引用import com.aspose.words.License;)

代码同上

不同类型转pdf就要引用不同的License,否则要有水印

3, txt类型和图片类型转pdf

准备jar(选择自己合适的)

链接:网盘链接

提取码:bq5n

我使用的jar

<dependency><groupId>com.itext</groupId><artifactId>itextpdf</artifactId><version>5.5.8</version></dependency><dependency><groupId>com.itext</groupId><artifactId>itext-asian</artifactId><version>1.0</version></dependency><dependency><groupId>com.itext</groupId><artifactId>itext</artifactId><version>2.1.5</version></dependency>

3.1 txt类型转pdf,我是通过读取txt内容来写入pdf中

//File对象转MultipartFileif(substring.equals("txt")){//url是txt文件路径File file = new File(url);FileInputStream input = new FileInputStream(file);MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", input);read= read(multipartFile);input.close();}

//读取文件内容private String read(MultipartFile file) {String content = "";String originalFilename = file.getOriginalFilename();String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);if (substring.equals("doc")) {content = readUtils.readDoc(file);System.out.println("content=" + content);} else if (substring.equals("docx")) {content = readUtils.readDocx(file);} else if (substring.equals("pdf")) {content = readUtils.readPDF(file);} else if (substring.equals("xls") || substring.equals("xlsx")) {List<List<String>> lists = readUtils.readExcel(file, substring);for (List<String> list : lists) {System.out.println("list=" + list.toArray().toString());content += list.toString() + "\n";}} else if (substring.equals("txt")) {content = readUtils.readTxt(file);}return content;}

/*** 读取txt内容* @param file 上传的文件流* @return 内容*/public static String readTxt(MultipartFile file){try {InputStream fileInputStream = file.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);StringBuffer sb = new StringBuffer();String text = null;while((text = bufferedReader.readLine()) != null){sb.append(text+"\n");}return sb.toString();} catch (Exception e) {e.printStackTrace();}return null;}

主要写入方法

private static void textToPdf(String pdfUrl, String content) {try {com.itextpdf.text.Document document = new com.itextpdf.text.Document();//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。PdfWriter.getInstance(document, new FileOutputStream(new File(pdfUrl)));BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font font=new Font(bfChinese);//打开文档document.open();//添加内容document.add(new Paragraph(content,font));document.close();} catch (Exception e) {e.printStackTrace();}}

图片写入到pdf中

private static void imgToPdf(String pdfUrl,String imgUrl){try {com.itextpdf.text.Document document = new com.itextpdf.text.Document();//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream(new File(pdfUrl)));Image img= Image.getInstance(imgUrl);//图片是否精准放置instance.setStrictImageSequence(true);//打开文档document.open();//添加图片document.add(img);document.close();} catch (Exception e) {e.printStackTrace();}}

如果想浏览器实现预览可以用一个pdf.js的插件,如果不用直接返回pdf文件流就可以预览,设置一下响应头基本信息,

但是这种预览会看到下载,打印按钮

response.setCharacterEncoding("UTF-8");FileBean fileBean = fileService.getFileBean(id);response.setContentType("application/pdf");response.setHeader("Content-Disposition","inline; filename=" + .URLEncoder.encode(fileBean.getFileName(), "UTF-8"));

如果想用pdf.js实现预览(因为我的项目预览时要不能看到下载和打印按钮)

链接:网盘链接,把下载和打印注解掉了

提取码:uasy

下载后放到static中新文件夹,名称为pdf中

前台vue的请求

window.open(dbFileUrl + "/pdf/web/viewer.html?file=" + dbFileUrl + "/file/viewFile?parm%3D" + this.shareUserId + "-" + info.fileId, info.fileName);

说明:

dbFileUrl 为 http://localhost:8083

/pdf/web/viewer.html 后台pdf.js插件存放路径

/file/viewFile 为后台接口名称

%3D 为转义字符,类似于“=”

因为这个好像不能同时传两个参数,所以我就拼接参数了

利用aspose实现ppt doc docx pptx xlsx xls txt 图片类型转pdf以及实现在线预览(可用于window和linux上 无水印)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。