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()就可以了


0 0