这里展示aspose系列word、ppt、excel转换pdf文件的java示例代码
aspose相关jar在本人资源中可以找到
- 统一调用入口
private void file2Pdf(String filePath, String pdfPath){
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(() -> {
Boolean isTrue = false;
String suffix = filePath.substring(filePath.lastIndexOf("."), filePath.length());
if(suffix.equalsIgnoreCase(".txt")){
logger.info("开始转换txt:"+filePath+"-----"+pdfPath);
File file = new File(filePath);
logger.info("文件大小:"+file.length());
isTrue = Txt2Pdf.txtToPdf(filePath, pdfPath);
logger.info("isTrue:"+isTrue);
}
if(suffix.equalsIgnoreCase(".doc") || suffix.equalsIgnoreCase(".docx")){
logger.info("开始转换"+suffix+":"+filePath+"-----"+pdfPath);
File file = new File(filePath);
logger.info("文件大小:"+file.length());
isTrue = Word2Pdf.word2Pdf(filePath, pdfPath);
logger.info("isTrue:"+isTrue);
}
if(suffix.equalsIgnoreCase(".xls") || suffix.equalsIgnoreCase(".xlsx") || suffix.equalsIgnoreCase(".csv")){
logger.info("开始转换"+suffix+":"+filePath+"-----"+pdfPath);
File file = new File(filePath);
logger.info("文件大小:"+file.length());
isTrue = Excel2Pdf.excel2Pdf(filePath, pdfPath);
logger.info("isTrue:"+isTrue);
}
if(suffix.equalsIgnoreCase(".ppt") || suffix.equalsIgnoreCase(".pptx")){
logger.info("开始转换"+suffix+":"+filePath+"-----"+pdfPath);
File file = new File(filePath);
logger.info("文件大小:"+file.length());
isTrue = PPT2Pdf.ppt2Pdf(filePath, pdfPath);
logger.info("isTrue:" + isTrue);
}
if(isTrue){
logger.info(filePath.subSequence(filePath.lastIndexOf("/") + 1, filePath.length())+"成功转换成pdf");
}
});
}
- Word2Pdf工具类
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
public class Word2Pdf {
private static final Logger LOG = LoggerFactory.getLogger(Word2Pdf.class);
/**
* word2Pdf
* @param path
* @author
* @date: 2019年10月29日 下午5:25:26
*/
/*public static Boolean word2Pdf(String wordPath, String pdfPath) {
//加载Word测试文档
com.spire.license.LicenseProvider.setLicenseKey("your license key");
com.spire.license.LicenseProvider.setLicenseKey();
Document doc = new Document();
try {
doc.loadFromFile(wordPath);
//保存为PDF格式的文件
doc.saveToFile(pdfPath, FileFormat.PDF);
} catch (Exception e){
e.printStackTrace();
return false;
} finally {
doc.close();
}
return true;
}*/
public static void main(String[] args) throws Exception {
String PDFTIMEDIR = "/document/";
String text = PDFTIMEDIR + "中国.docx";
String path = PDFTIMEDIR + "中国.pdf";
word2Pdf(text, path);
}
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String licenseStr =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
LOG.error("error:", e);
}
return result;
}
/**
* Word 2 pdf.
*
* @param wordPath the word file Path
* @param pdfFilePath the pdf file path
*/
public static Boolean word2Pdf(String wordPath, String pdfFilePath) {
FileOutputStream fileOS = null;
// 验证License
if (!getLicense()) {
LOG.error("验证License失败!");
return false;
}
try {
Document doc = new Document(wordPath);
fileOS = new FileOutputStream(new File(pdfFilePath));
// 保存转换的pdf文件
doc.save(fileOS, SaveFormat.PDF);
} catch (Exception e) {
LOG.error("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
}
} catch (IOException e) {
LOG.error("error:", e);
}
}
return true;
}
}
- PPT2Pdf工具类
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.License;
public class PPT2Pdf {
private static final Logger LOG = LoggerFactory.getLogger(PPT2Pdf.class);
public static void main(String[] args) throws Exception {
String PDFTIMEDIR = "/document/";
String text = PDFTIMEDIR + "测试.pptx";
String path = PDFTIMEDIR + "测试.pdf";
ppt2Pdf(text, path);
}
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String licenseStr =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(license);
result = true;
} catch (Exception e) {
LOG.error("error:", e);
}
return result;
}
/**
* Word 2 pdf.
*
* @param wordPath the word file Path
* @param pdfFilePath the pdf file path
*/
public static Boolean ppt2Pdf(String pptPath, String pdfFilePath) {
FileOutputStream fileOS = null;
// 验证License
if (!getLicense()) {
LOG.error("验证License失败!");
return false;
}
try {
FileInputStream fileInput = new FileInputStream(new File(pptPath));
Presentation pptx = new Presentation(fileInput);
//Presentation pptx = new Presentation(pptPath);
fileOS = new FileOutputStream(new File(pdfFilePath));
pptx.save(fileOS, SaveFormat.Pdf);
} catch (Exception e) {
LOG.error("error:", e);
}
return true;
}
}
- Txt2Pdf工具类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.zrqx.core.util.charset.CharsetUtils;
public class Txt2Pdf {
public static boolean txtToPdf(String txtPath, String pdfPath) {
//新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
StringBuilder bodyText = new StringBuilder();
InputStreamReader read = null;
try {
File file = new File(txtPath);
String code = CharsetUtils.resolveCode(txtPath);
System.out.println(code);
read = new InputStreamReader(new FileInputStream(new File(txtPath)), code);//简体中文系统默认gb2312;gbk兼容之
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
bodyText.append(lineTxt);
bodyText.append("\r\n");
}
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
document.open();
// 写入一段文字,itext默认不支持亚洲语言,所以需要设置字体
document.add(new Paragraph(bodyText.toString(),getChineseFont()));
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if( read != null ){
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
document.close();
}
return true;
}
// 产生PDF字体
public static Font getChineseFont() {
Font font = null;
try {
BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
font = new Font(bf, 12, Font.NORMAL);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return font;
}
public static void main(String[] args) throws Exception {
String PDFTIMEDIR = "/document/";
String text = PDFTIMEDIR + "txt01.txt";
String pdf = PDFTIMEDIR + "txt01.pdf";
txtToPdf(text, pdf);
}
}
注意:Txt2Pdf需要注意文件的编码格式,不然转换会乱码,以下是获取文件编码类型
当用字节流输入读取时,将字节流转换成字符串的时候可以用工具判断编码类型或者直接设置编码格式,避免乱码
依赖jar包,请自行添加
antlr-2.7.7.jar
chardet-1.0.jar
commons-io-2.4.jar
cpdetector-1.0.7.jar
import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
public class CharsetUtils {
/**
* 获取编码格式
* @param path 文件路径
* @return
* @throws IOException
* @author
* @date: 2019年12月26日 下午4:27:30
*/
public static String resolveCode(String path) throws IOException {
InputStream inputStream = null;
BufferedInputStream bin;
/*
* detector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、
* JChardetFacade、ASCIIDetector、UnicodeDetector。
* detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的
* 字符集编码。使用需要用到三个第三方JAR包:antlr.jar、chardet.jar和cpdetector.jar
* cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
/*
* ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
* 指示是否显示探测过程的详细信息,为false不显示。
*/
detector.add(new ParsingDetector(false));
/*
* JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
* 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
* 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
*/
detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar
// ASCIIDetector用于ASCII编码测定
detector.add(ASCIIDetector.getInstance());
// UnicodeDetector用于Unicode家族编码的测定
detector.add(UnicodeDetector.getInstance());
Charset charset = null;
try {
inputStream = new FileInputStream(path);
bin = new BufferedInputStream(inputStream);
charset = detector.detectCodepage(bin, 128);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (charset != null)
return charset.name();
else
return null;
}
/**
* 获取编码格式
* @param path 文件路径
* @return
* @throws Exception
* @author
* @date: 2019年12月26日 下午4:27:30
*/
public static String resolveCode(InputStream inputStream) {
BufferedInputStream bin;
/*
* detector是探测器,它把探测任务交给具体的探测实现类的实例完成。
* cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、
* JChardetFacade、ASCIIDetector、UnicodeDetector。
* detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的
* 字符集编码。使用需要用到三个第三方JAR包:antlr.jar、chardet.jar和cpdetector.jar
* cpDetector是基于统计学原理的,不保证完全正确。
*/
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
/*
* ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
* 指示是否显示探测过程的详细信息,为false不显示。
*/
detector.add(new ParsingDetector(false));
/*
* JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
* 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
* 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
*/
detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar
// ASCIIDetector用于ASCII编码测定
detector.add(ASCIIDetector.getInstance());
// UnicodeDetector用于Unicode家族编码的测定
detector.add(UnicodeDetector.getInstance());
Charset charset = null;
try {
bin = new BufferedInputStream(inputStream);
charset = detector.detectCodepage(bin, 128);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (charset != null)
return charset.name();
else
return null;
}
}