首页 技术 正文
技术 2022年11月15日
0 收藏 837 点赞 3,351 浏览 3808 个字

相关专题链接:

PDF解决方案(1)–文件上传

PDF解决方案(2)–文件转PDF

PDF解决方案(3)–PDF转SWF

PDF解决方案(4)–在线浏览

前言:上一篇中讲到的文件上传,文件上传在网络上有大量的范例,因为想提供一个完整的解决方案就放上了,仅供参考;这一篇主要介绍一些常用文件转换为PDF的实现。

1、word、excel转pdf

通过百度了解到现在office转换为pdf主要有三种形式:Jacob、JCom、openoffice,前两种方式均依赖windows平台和office软件(其中JCom还需要Acrobat_Pro且已注册),成本很高且局限性很大,第三种方式使用开源的openoffice支持跨平台使用,目前绝大部分的web应用都会部署在linux或UNIX平台,所以第三种方式是一种最好的实现方案,下面介绍第三种方案的实现(基于windows平台,linux平台在openoffice的安装和启动略有差异,但Java调用的代码是相同的,后面在提供专门篇幅来介绍linux平台的openoffice安装和启动),前两种方式会在下面提供demo供大家参考。

转换前需要先安装并以服务方式启动openoffice软件:

openoffice下载地址:http://www.openoffice.org/download/index.html

安装成功后在cmd中定位到program目录,执行命令:soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” –nofirststartwizard,然后执行:netstat -ano|findstr “8100” ,当出现如图红线所示则表示启动成功了。

PDF解决方案(2)–文件转PDF

调用openoffice代码:

调用openoffice进行转换需要用到openoffice提供的jar包:jodconverter、juh、jurt、ridl、slf4j-api、slf4j-jdk14、unoil、xstream,Apache的io包,在项目中引用就可以了,其中在jodconverter包的com.artofsolving.jodconverter路径下有一个document-formats.xml的文件,它定义了openoffice支持的文件转换类型,这里我们只选中其中的doc、docx、xls、xlsx四种类型,先获取openoffice连接,然后定义输入输出文件格式信息,执行转换,关闭连接即可,代码中有详细注释这里不再赘述了。

/**
* 调用openoffice把office转成pdf
* @param inStream 输入流
* @param fos 输出流
* @param extensionname 扩展名
*/
public static void converterOffiec2PDF(InputStream inStream,
FileOutputStream fos, String extensionname)
{
OpenOfficeConnection connection = null;
try
{
connection = new SocketOpenOfficeConnection(8100);//获取openoffice连接
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);//创建openoffice文件转换类
DocumentFormat inputFormat = null;
DocumentFormat pdf = new DocumentFormat("Portable Document Format",
"application/pdf", "pdf");//指定目标文件格式信息 //指定输出过滤器参数
pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
pdf.setExportFilter(DocumentFamily.PRESENTATION,
"impress_pdf_Export");
pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export"); //指定输入文件格式信息 定义信息在docment_formats.xml中
if (Constans.FileExtName.DOC.equalsIgnoreCase(extensionname))
{
inputFormat = new DocumentFormat("Microsoft Word",
DocumentFamily.TEXT, "application/msword", "doc");
inputFormat.setExportFilter(DocumentFamily.TEXT, "MS Word 97");
}
else if (Constans.FileExtName.DOCX.equalsIgnoreCase(extensionname))
{
inputFormat = new DocumentFormat(
"Microsoft Word 2007 XML",
DocumentFamily.TEXT,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"docx");
}
else if (Constans.FileExtName.XLS.equalsIgnoreCase(extensionname))
{
inputFormat = new DocumentFormat("Microsoft Excel",
DocumentFamily.SPREADSHEET, "application/vnd.ms-excel",
"xls");
inputFormat.setExportFilter(DocumentFamily.SPREADSHEET,
"MS Excel 97");
}
else if (Constans.FileExtName.XLSX.equalsIgnoreCase(extensionname))
{
inputFormat = new DocumentFormat(
"Microsoft Excel 2007 XML",
DocumentFamily.SPREADSHEET,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xlsx");
} //执行文件转换
converter.convert(inStream, inputFormat, fos, pdf);
} finally
{
if (connection != null)
{
connection.disconnect();
connection = null;
} closeStream(inStream, fos);
}
}

2、图片转pdf

图片转pdf采用itext的开源jar包itextpdf,itextpdf可以创建、修改pdf,这里我们利用itextpdf创建一个新的pdf然后把图片添加进去,实现图片转pdf的效果;代码步骤为:先创建文档,获取pdf写入器,打开文档,设置内容格式,写入内容到文档,关闭文档。

/**
* 图片转pdf
* @param inStream
* @param fos
* @throws MalformedURLException
* @throws IOException
* @throws
*/
public static void converterImg2Pdf(InputStream inStream,
FileOutputStream fos) throws MalformedURLException, IOException
{
//创建新文档
Document doc = new Document();
try
{
//pdf写入器装载文档、输出流
PdfWriter.getInstance(doc, fos);
doc.open();//打开文档准备写入,文档必须打开才能写入 BufferedImage bufferedImage = ImageIO.read(inStream);
float h = bufferedImage.getHeight();
float w = bufferedImage.getWidth();
Image image = Image.getInstance(bufferedImage, null);
image.setAlignment(Image.MIDDLE);//图片对齐方式,居中即可
image.scalePercent(getPercent2(h, w));//图片压缩比
doc.add(image);//添加图片
doc.close();//关闭文档,文档必须正确关闭 } catch (DocumentException e)
{
e.printStackTrace();
}
}

相关文件下载

jcom、jacob demo下载地址:http://pan.baidu.com/s/1dD297rz

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898