package com.ship.util;/**
* @Author zzt
* @Date 2022/2/28
*/
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
//import com.aspose.pdf.Document;
//import com.aspose.pdf.Image;
//import com.aspose.pdf.Page;
//import com.aspose.pdf.SaveFormat;
//import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author zzt
* @Date 2022/2/28
* @class_name Word2PdfAsposeUtil
* @description
*/
public class Word2PdfAsposeUtil {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Word2PdfAsposeUtil.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;
}
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static boolean pdf2doc(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
public static void main(String[] args) {
Word2PdfAsposeUtil.pdf2doc("C:\\Users\\Eric\\Downloads\\Documents\\test.pdf","C:\\Users\\Eric\\Desktop\\1.doc");
}
/**
* 图片转PDF
*
* @param sourcePath
* @param targetPath
* @param imgType
* @throws IOException
*/
public static void image2pdf(String sourcePath, String targetPath, String imgType) throws IOException {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
//创建文档
Document doc = new Document();
//新增一页
Page page = doc.getPages().add();
//设置页边距
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
//创建图片对象
Image image = new Image();
BufferedImage bufferedImage = ImageIO.read(new File(sourcePath));
//获取图片尺寸
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, imgType, baos);
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
image.setImageStream(bais);
//设置pdf页的尺寸与图片一样
page.getPageInfo().setHeight(height);
page.getPageInfo().setWidth(width);
//添加图片
page.getParagraphs().add(image);
//保存
doc.save(targetPath, SaveFormat.Pdf);
}
}
评论0