一、开发工具:office 16、jacob-1.18-M2、jboss 1.6

二、开发配置:

1、解压缩---》

2、配置jacob:

A C:\Windows\System32 jacob-1.18-M2-x64.dll
                  B C:\Program Files\Java\jdk1.6.0_43\jre\bin jacob-1.18-M2-x64.dll
                  C D:\jboss-6.0.0.Final\server\default\lib jacob.jar

三、编写代码:

 package dh.hongyi.wed.asset;

 import java.io.BufferedInputStream;
import java.io.File; import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletResponse; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch; public class ToPdf {
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
//excelTohtml001();
//convert2PDF("F:/googledowload/1564733032856.xlsx","F:/googledowload/1564733032856.pdf");
//convert2PDF("http://testgq1.yuhong.com.cn/resource/accountStatement/20190802/1564733052458.xlsx","F:/googledowload/w.pdf");
// HttpServletResponse response = new HttpServletResponse();
// downloadFile(); } //直接调用这个方法即可
public static boolean convert2PDF(String inputFile, String pdfFile) {
String suffix = getFileSufix(inputFile);
String suffixF = getFileSufixF(inputFile);
File file = new File(inputFile);
if(!file.exists()){
System.out.println("文件不存在!");
return false;
}
if(suffix.equals("pdf")){ return false;
}
if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){
return word2PDF(inputFile,pdfFile);
}else if(suffix.equals("ppt")||suffix.equals("pptx")){
return ppt2PDF(inputFile,pdfFile);
}else if(suffix.equals("xls")||suffix.equals("xlsx")||suffix.equals("XLSX")){
return excel2PDF(inputFile,pdfFile);
}else{ return false;
}
}
public static String getFileSufix(String fileName){
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
public static String getFileSufixF(String fileName){
int splitIndex = fileName.lastIndexOf("resource")+7;
int splitIndexF = fileName.lastIndexOf(".");
int splitIndexM = fileName.lastIndexOf("/");
System.out.println(fileName.substring(splitIndex + 1));
System.out.println(fileName.substring(splitIndex));
System.out.println(fileName.substring(splitIndexF));
System.out.println(fileName.substring(splitIndexM+1, splitIndexF));
return fileName.substring(splitIndex + 1);
}
public static boolean word2PDF(String inputFile,String pdfFile){
ActiveXComponent app = null;
Dispatch doc = null;
boolean result=true;
try{
//打开word应用程序
app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs,
"Open",
inputFile,
false,
true
).toDispatch(); Dispatch.call(doc,
"ExportAsFixedFormat",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
); result= true;
}catch(Exception e){
result= false;
}finally {
if (doc != null) {
Dispatch.call(doc, "Close");
}
if (app != null) {
app.invoke("Quit");
}
}
return result;
} public static boolean excel2PDF(String inputFile,String pdfFile){
ActiveXComponent app = null;
Dispatch excel = null;
boolean result=true;
try{
app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
excel = Dispatch.call(excels,
"Open",
inputFile,
false,
true
).toDispatch();
Dispatch.call(excel,
"ExportAsFixedFormat",
xlTypePDF,
pdfFile
);
result= true;
}catch(Exception e){
result= false;
}finally {
if (excel != null) {
Dispatch.call(excel, "Close");
}
if (app != null) {
app.invoke("Quit");
}
}
System.out.println("excel转pdf结束");
return result;
} public static boolean ppt2PDF(String srcFilePath, String pdfFilePath){
ActiveXComponent app = null;
Dispatch ppt = null;
boolean result=true;
try {
ComThread.InitSTA();
app = new ActiveXComponent("PowerPoint.Application");
Dispatch ppts = app.getProperty("Presentations").toDispatch(); // 因POWER.EXE的发布规则为同步,所以设置为同步发布
ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); //ppSaveAsPDF为特定值32 result=true; // set flag true;
} catch (ComFailException e) {
result=false;
} catch (Exception e) {
result=false;
} finally {
if (ppt != null) {
Dispatch.call(ppt, "Close");
}
if (app != null) {
app.invoke("Quit");
}
ComThread.Release();
}
return result;
} /*文件下载*/
public static void downloadFile(HttpServletResponse response,String fileName,String path){
if (fileName != null) {
//设置文件路径
File file = new File(path);
if (file.exists()) {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
try {
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
} }

java web 开发之 office(excel、doc等)文件转pdf的更多相关文章

  1. Java Web开发之Servlet、JSP基础

    有好多年不搞Java Web开发了,这几天正好国庆放假,放松之余也有兴趣回头看看Java Web开发技术的基础. 我们都知道,Servlet是Java Web开发的重要基础,但是由于Servlet开发 ...

  2. Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用

    1.Spring与Mybatis整合 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web ...

  3. Java Web开发之Servlet获取ckeditor内容

    js: <!-- ckeditor插件 --> <script type="text/javascript" src="js/ckeditor/cked ...

  4. Web开发之Tomcat&Servlet

    <!doctype html>01 - JavaEE - Tomcat&Servlet figure:first-child { margin-top: -20px; } #wri ...

  5. Vim下的Web开发之html,CSS,javascript插件

    Vim下的Web开发之html,CSS,javascript插件   HTML 下载HTML.zip 解压HTML.zip,然后将里面的所有文件copy到C:\Program Files\Vim\vi ...

  6. 移动web开发之rem适配布局

    移动web开发之rem适配布局 方案: 页面布局文字能否随着屏幕大小变化而变化 流式布局和flex布局主要针对于宽度布局,那高度如何布局? 怎样让屏幕发生变化的时候元素高度和宽度等比例缩放? 1. r ...

  7. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

  8. Microsoft Office Excel 不能访问文件

    问题描述: Microsoft Office Excel 不能访问文件“XX.xls”.可能的原因有: 1 文件名称或路径不存在.2 文件正被其他程序使用.3 您正要保存的工作簿与当前打开的工作簿同名 ...

  9. Microsoft Office Excel 不能访问文件及COM无法访问

    Microsoft Office Excel 不能访问文件及COM无法访问 Microsoft Office Excel 不能访问文件“*.xls”. 可能的原因有: 1 文件名称或路径不存在. 2  ...

随机推荐

  1. 2007 NOIP T1奖学金

    奖学金(07NOIPT1): [题目描述] 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分从高到低排序,如果 ...

  2. Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId

    一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...

  3. 2019-2020-1 20199328《Linux内核原理与分析》第八周作业

    笔记部分 2019/11/4 17:55:22 elf文件代码默认加载到0x8048000,然后是一段首部信息,然后到达程序的真实入口 正常的系统调用会先进入内核态->用户态->系统调用下 ...

  4. [Inno Setup] 退出安装程序的两种方式

    1. 完全静默的退出 procedure ExitProcess(exitCode:integer); external 'ExitProcess@kernel32.dll stdcall'; ... ...

  5. 理解分布式一致性:Paxos协议之Generalized Paxos & Byzantine Paxos

    理解分布式一致性:Paxos协议之Generalized Paxos & Byzantine Paxos Generalized Paxos Byzantine Paxos Byzantine ...

  6. Springboot以Jetty为容器实现http重定向到https

    1 简介 之前讲解的Springboot整合https用的是tomcat作为容器,tomcat也是一个流行多年的老牌Java容器了.但针对不同的场景,还是会有不同的选择,如Jetty.Jetty是架构 ...

  7. LaTex中文article模板(支持代码、数学、TikZ)

    代码 请使用XeLatex编译 main.tex \documentclass{article} \usepackage{ctex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  8. 通过transmittable-thread-local源码理解线程池线程本地变量传递的原理

    前提 最近一两个月花了很大的功夫做UCloud服务和中间件迁移到阿里云的工作,没什么空闲时间撸文.想起很早之前写过ThreadLocal的源码分析相关文章,里面提到了ThreadLocal存在一个不能 ...

  9. 使用sys模块写一个软件安装进度条

    import sys,time for i in range(50): sys.stdout.write('#') sys.stdout.flush() #强制刷新将内存中的文件写一条,输出一条. t ...

  10. Hard filters (by GATK)

    Filter Symbol T. Definition QualByDepth QD 2.0 The variant confidence (from the QUAL field) divided ...