OpenOffice 转换pdf->图片 代码
来源:互联网 发布:淘宝如何改规格分类 编辑:程序博客网 时间:2024/06/12 00:39
现在action中获取文件路径
private File file1;
private String file1FileName;
public String doCreate() {
this.initContext();
try {
LoginInfo loginInfo = (LoginInfo) request.getSession()
.getAttribute("loginInfo");
String userid = loginInfo.getUserid();
String apath = request.getSession().getServletContext()
.getRealPath(upload);
System.out.println("获取upload路径:" + apath);
File af = new File(apath);
if (!af.exists()) {
af.mkdirs();
}
UpLoadUtil upUtil = new UpLoadUtil();
if (file1 != null) {
String uuid = UUID.randomUUID().toString();
String imguuid = "";
if (image1 != null) {
String imgext = image1FileName.substring(image1FileName
.lastIndexOf(".") + 1);
imguuid = uuid + "." + imgext;
if (!imgext.equals("jpg") && !imgext.equals("png")
&& !imgext.equals("gif")) {
this.msg = "上传图片类型必须为:jpg、png、gif";
this.success = false;
return "file";
}
if (image1.length() / 1024 > 100) {
this.msg = "图片大小需小于100KB";
this.success = false;
return "file";
}
UpLoadUtil.fileUpLoad(image1, imguuid, apath);
imguuid = upload + imguuid;
}
String ext = file1FileName.substring(file1FileName
.lastIndexOf(".") + 1);
String fileuuid = uuid + "." + ext;
if (UpLoadUtil.fileUpLoad(file1, fileuuid, apath) == "SUCCESS") {
FileuploadInfo fileuploadInfo = new FileuploadInfo();
fileuploadInfo.setUptime(DateUtil
.getNowTime("yyyy-MM-dd HH:mm:ss"));
fileuploadInfo.setFilename(file1FileName);
fileuploadInfo.setAftername(fileuuid);
fileuploadInfo.setUserid(userid);
String filepath = apath + "/" + fileuuid;
System.out.println("保存路径:" + filepath);
DocConverter d = new DocConverter(filepath);
d.imgconver();
fileuploadInfo.setPagenum(d.getPagenum());
fileuploadInfo.setFilepath(upload + fileuuid);
fileuploadInfo.setImgpath(imgpath + uuid);
fileuploadInfo.setCoverpath(imguuid);
fileuploadInfo.setPageviews(0);
fileuploadInfo.setClassify(this.fileuploadInfo
.getClassify());
fileuploadInfo.setKname(this.fileuploadInfo.getKname());
fileuploadInfo.setAuthor(this.fileuploadInfo.getAuthor());
fileuploadInfo.setRemark(this.fileuploadInfo.getRemark());
}
this.id = fileuploadService
.createFileupload(fileuploadInfo);
this.msg = "上传成功!," + id;
} else {
this.msg = "上传文件不能为空!";
this.success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
this.success = false;
this.msg = "上传失败!";
}
return "file";
}
被action调用的方法
@SuppressWarnings("unused")
public boolean imgconver() {
try {
doc2pdf();
} catch (Exception e) {
e.printStackTrace();
return false;
}
if(pdfFile.exists()){
pdftopng();
}
return true;
}
/**
* 转为PDF
*
* @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = null;
converter = new StreamOpenOfficeDocumentConverter(connection);
//判断结束
if(extName.equals("txt") || extName.equals("TXT")){
converter.convert(odtFile, pdfFile);
}else{
converter.convert(docFile, pdfFile);}
// 关闭连接
connection.disconnect();
} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}
/**
* pdf 转成 图片(png格式) 使用ICEPDF
*/
public void pdftopng(){
String filePath = pdfFile.getPath();
Document document = new MyDocument();
try {
document.setFile(filePath);
} catch (Exception ex) {
}
// save page caputres to file.
float scale = 2f;
float rotation = 0f;
File af = new File(fileName);
if (!af.exists()) {
af.mkdirs();
}
pagenum = document.getNumberOfPages();
System.out.println("页数:" + document.getNumberOfPages());
// Paint each pages content to an image and write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)
document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
// capture the page image to file
try {
System.out.println("page " + (i+1));
File file = new File(fileName + "/" + (i+1) + ".png");
System.out.println(fileName + "/" + (i+1) + ".png");
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
// clean up resources
document.dispose();
if(!extName.equals("pdf"))
pdfFile.delete();
}
以上就是OpenOffice 先转 pdf 再转 图片 代码,如果想只转成pdf,直接不用调用pdftopng()就可以了
- OpenOffice 转换pdf->图片 代码
- 通过OpenOffice转换PDF
- 通过OpenOffice转换PDF
- Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF
- 用java写的jodconverter借用openoffice来转换office成pdf的示例代码
- 利用OpenOffice将word转换成PDF
- 利用openoffice转换ppt、doc转化pdf
- Linux Openoffice转换Office为pdf
- JAVA OpenOffice+jodconverter转换WORD为PDF
- 利用openoffice转换ppt、doc转化pdf
- 利用openOffice实现各种文档转换成pdf格式
- OpenOffice纯文本转换pdf乱码问题的解决
- 使用OpenOffice .org(Portable)将WORD转换为PDF
- Java用OpenOffice将word转换为PDF
- 利用OpenOffice将word等office文档转换成PDF
- openoffice实现office文件转换为pdf实现文件预览
- Linux下OpenOffice转换Office为PDF的java实现
- openoffice jodconverter 文档转换pdf过坑记录
- 设置springboot启动端口
- Git 学习第一天
- gcc内置原子操作函数
- c++ 在函数后加const是什么含义?
- Maven搭建SpringMVC+Mybatis项目详解
- OpenOffice 转换pdf->图片 代码
- 笔试题易错点
- for语句的简单应用②
- PHP常用正则表达式汇总
- Android实战简易教程<十九>(SwipeRefreshLayout下拉刷新使用实例)
- 栈的应用——四则运算表达式求值
- 采用dlopen、dlsym、dlclose加载动态链接库
- AMiner Author-Paper-Citation (APC) Network
- 使用数据库统一管理ssh登陆用户密钥信息