ppt和pdf转图片(Spire.Presentation)word转pdf
在官网下载的
Spire.Presentation.jar 包
https://www.e-iceblue.com/downloads/java/Spire.Presentation-FE_2.6.1.zip
package zyntest02; import com.spire.pdf.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class TestPdfToImg { public static void main(String[] args) throws IOException { PdfDocument pdf = new PdfDocument("f:\\123.pdf"); BufferedImage image; for(int i = 0; i< pdf.getPages().getCount();i++){ image = pdf.saveAsImage(i); File file = new File( String.format("f:\\ToImage-img-%d.png", i)); ImageIO.write(image, "PNG", file); } pdf.close(); } }
package zyntest02; import com.spire.presentation.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PPTtoPNG { public static void main(String[] args) throws Exception{ //创建Presentation对象 Presentation ppt = new Presentation(); //加载示例文档 ppt.loadFromFile("sample.pptx"); //遍历幻灯片 for (int i = 0; i < ppt.getSlides().getCount(); i++) { //将幻灯片保存为BufferedImage对象 BufferedImage image = ppt.getSlides().get(i).saveAsImage(); //将BufferedImage保存为PNG格式文件 String fileName = String.format("ToImage.png", i); ImageIO.write(image, "PNG",new File(fileName)); } ppt.dispose(); } }
将整个工作簿转为PDF
import com.spire.xls.*; public class ExcelToPDF { public static void main(String[] args) { //加载Excel文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //调用方法保存为PDF格式 wb.saveToFile("ToPDF.pdf",FileFormat.PDF); } }
转换结果
将指定工作表转为PDF
import com.spire.xls.*; public class ExcelToPDF { public static void main(String[] args) { //加载Excel文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //获取第2个工作表 Worksheet sheet = wb.getWorksheets().get(1); //调用方法保存为PDF格式 sheet.saveToPdf("ToPDF2.pdf"); } }
转换结果
Spire.XLS for Java支持将Excel文档转为多种文档格式,包括PDF、图片、html、XPS、XML、CSV、PCL、ODS、PostScript、以及Office Excel不同版本如,version97-2003,version2007,version2010,version2013,version2016等。
import com.spire.xls.*; public class ExcelToImg { public static void main(String[] args) { //加载Excel工作表 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //获取工作表 Worksheet sheet = wb.getWorksheets().get(0); //调用方法将Excel工作表保存为图片 sheet.saveToImage("ToImg.png"); //调用方法,将指定Excel单元格数据范围保存为图片 //sheet.saveToImage("ToImg2.png",8,1,30,7); //调用方法将Excel保存为HTML sheet.saveToHtml("ToHtml.html"); //调用方法将Excel保存为XPS sheet.saveToFile("ToXPS.xps", String.valueOf(FileFormat.XPS)); //调用方法将Excel保存为CSV sheet.saveToFile("ToCSV.csv", String.valueOf(FileFormat.CSV)); //调用方法将Excel保存为XML sheet.saveToFile("ToXML.xml", String.valueOf(FileFormat.XML)); //调用方法将Excel保存为PostScript sheet.saveToFile("ToPostScript.postscript", String.valueOf(FileFormat.PostScript)); //调用方法将Excel保存为PCL sheet.saveToFile("ToPCL.pcl", String.valueOf(FileFormat.PCL)); } }
word 转pdf
1.下载地址:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html
2.官方搭建有官方Maven仓库:http://repo.e-iceblue.com/nexus/content/groups/public/
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>free.spire.doc</artifactId>
<version>2.7.3</version>
</dependency>
三、项目中基于Spire.Doc封装工具类实现Word转换Pdf
直接上示例代码
package com.demo.utils;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class WordToPdfUtil {
public static void changeWordToPdf(String infile, String outFile) {
//加载word示例文档
Document document = new Document();
document.loadFromFile(infile, FileFormat.Docx);
//保存结果文件
document.saveToFile(outFile, FileFormat.PDF);
document.close();
}
}