jodconverter水印java,OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚)
java OpenOffice officetopdf
最近项目需要实现下载Office文档时自动转成PDF文档,以下代码支持2003及2007版的Word,PPT,Excel转换,并支持自定义添加水印、页眉、页脚
实现需要事先安装OpenOffice(我这里安装的是OpenOffice 4)
OpenOffice 下载: http://www.openoffice.org/
JodConverter 下载地址 http://sourceforge.net/projects/jodconverter/files/JODConverter
需要代码正常运行还需要其他Jar包(见附件)
Java代码:
/**
* @filename: OfficeToPdf.java
* @package: common
* @description: OfficeToPdf
* @author: lsq
* @date: 2015年10月14日 下午5:25:32
* @version: V1.0
*
*/
package officetopdf;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfCopy.PageStamp;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
/**
* @className: OfficeToPdf
* @description: OfficeToPdf
* @author: lsq
* @date: 2015年10月14日 下午5:25:32
*/
public class OfficeToPdf {
public static void main(String[] args) {
String inputFilePath = "F://officeToPdf/WordToPdf测试.docx";
//String inputFilePath = "F://officeToPdf/pptToPdf测试.pptx";
//String inputFilePath = "F://officeToPdf/xlsxToPdf测试.xlsx";
String outputFilePath = getOutputFilePath(inputFilePath);
//Office转换成Pdf
OfficeToPdf.office2pdf(inputFilePath,outputFilePath);
//添加水印、页眉、页脚
addFooterAndWater("F://officeToPdf//WordToPdf测试.pdf", "F://officeToPdf//WordToPdf测试2.pdf", "WordToPdf水印严禁复制", "WordToPdf页眉", "WordToPdf页脚");
}
/**
* 将Office文档转换为PDF. 需要安装OpenOffice
*
* @param inputFilePath
* 源文件,绝对路径. 可以是Office2003-2007全部格式的文档, 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等.
*
* @param outputFilePath
* 目标文件.绝对路径.
*/
public static void office2pdf(String inputFilePath,String outputFilePath) {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
String officeHome = getOfficeHome();
//设置OpenOffice.org安装目录
config.setOfficeHome(officeHome);
//设置转换端口,默认为8100
//config.setPortNumbers(8100);
//设置任务执行超时为60分钟
config.setTaskExecutionTimeout(1000 * 60 * 60L);
//设置任务队列超时为24小时
config.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
System.out.println("office转换服务启动成功!");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File inputFile = new File(inputFilePath);
if (inputFile.exists()) {// 找不到源文件, 则返回
File outputFile = new File(outputFilePath);
if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
}
if (null != officeManager){
officeManager.stop();
System.out.println("office转换服务完成。");
}
}
/**
* 根据源文件路径获取PDF文件路径
* @param inputFilePath
* @return
*/
public static String getOutputFilePath(String inputFilePath) {
String outputFilePath = "";
String temp = inputFilePath.substring(inputFilePath.lastIndexOf(".")) ;
outputFilePath = inputFilePath.replaceAll(temp, ".pdf");
return outputFilePath;
}
/**
* 获取OpenOffice安装目录
* @return
*/
public static String getOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice.org3";
} else if (Pattern.matches("Windows.*", osName)) {
return "E:/software/OpenOffice 4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}
/**
* 添加水印、页眉、页脚
* @param fileName 源文件路径
* @param savepath 目标文件路径
* @param waterMarkName 文字水印
* @param pageHeade 页眉
* @param foot 页脚
* @return
*/
public static int addFooterAndWater(String fileName, String savepath,
String waterMarkName, String pageHeade, String foot)
{
// 文档总页数
int num = 0;
Document document = new Document();
try
{
PdfReader reader = new PdfReader(fileName);
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
num = reader.getNumberOfPages();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
document.open();
for (int i = 0; i < num;)
{
PdfImportedPage page = copy.getImportedPage(reader, ++i);
PageStamp stamp = copy.createPageStamp(page);
Font f = new Font(base);
// 添加页脚,左侧文字,右侧页码
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_RIGHT,
new Phrase(String.format("第 %d 页/共 %d 页", i, num), f),
550f, 28, 0);
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_LEFT, new Phrase(foot, f), 50f, 28, 0);
// 添加页眉 (文字页眉,居中)
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_CENTER, new Phrase(pageHeade, f), 150f,
800, 0);
// 页眉添加logo (图片页眉,居右)
/*Image img = Image.getInstance("template/logo.png");// 选择图片
img.setAlignment(1);
img.scaleAbsolute(436 / 5, 96 / 5);// 控制图片大小
img.setAbsolutePosition(450f, 800);// 控制图片位置
stamp.getUnderContent().addImage(img);*/
// 添加水印
PdfContentByte under = stamp.getUnderContent();
under.beginText();
under.setColorFill(Color.LIGHT_GRAY);
// 字符越长,字体越小,设置字体
int fontSize = getFontSize(waterMarkName);
under.setFontAndSize(base, fontSize);
// 设置水印文字字体倾斜 开始
float pageWidth = reader.getPageSize(i).getWidth();
float pageHeight = reader.getPageSize(i).getHeight();
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName,
pageWidth / 2, pageHeight / 2, 60);// 水印文字成60度角倾斜,且页面居中展示
// 字体设置结束
under.endText();
stamp.alterContents();
copy.addPage(page);
}
}
catch (Exception e)
{
e.printStackTrace();
return -1;
}
finally
{
if (null != document)
{
document.close();
}
}
System.out.println("pdf totalpages:" + num);
return num;
}
/**
* 根据水印文字长度计算获取字体大小
* @param waterMarkName
* @return
*/
private static int getFontSize(String waterMarkName){
int fontSize = 80;
if(null != waterMarkName && !"".equals(waterMarkName)){
int length = waterMarkName.length();
if(length <=26 && length >= 18){
fontSize = 26;
}else if(length <18 && length >= 8){
fontSize = 40;
}else if(length <8 && length >= 1){
fontSize = 80;
}else {
fontSize = 16;
}
}
return fontSize;
}
}
jodconverter水印java,OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚)相关推荐
- Java 处理PDF文档(一):页眉页脚、水印、背景、附件
前言 本文将介绍通过Java编程来处理PDF文档的一些方法,因为一篇文档的处理可能包括很多内容,比如文档安全性设置(水印.加密/解密).文本/图片/图形操作.注释.附件.域.文档转换(其他文件格式转为 ...
- html pdf支持css%写法吗,flying-saucer-pdf终于完美解决了(中文问题,换行问题,分页,页眉页脚,水印),html+css控制pdf样式...
集成freemarker+flying-saucer-pdf+itext,通过html模板生成PDF 折腾了很久,flying-saucer-pdf终于完美解决了(中文问题,换行问题,页眉页脚,水印) ...
- SpringBoot html转pdf 支持中文、图片水印+文字水印、页眉页脚 flying-saucer-pdf-itext5 + freemarker
使用 flying-saucer-pdf-itext5加freemarker生成pdf,支持中文.图片水印+文字水印.页眉页脚. 引入jar包 <!-- freemarker --> &l ...
- itext Pdf页眉/页脚/水印
package com.company.common.core.utils.itext;import com.itextpdf.text.*; import com.itextpdf.text.pdf ...
- Java生成pdf设置两行页脚_Itextpdf 5 html生成pdf 页眉页脚
效果图 QQ图片20190117140345.png 继承PdfPageEventHelper重写onEndPage方法 package com.xuqiang.itext.test; import ...
- Spire pdf 操作pdf,页眉 页脚 水印 二维码
1.创建页眉页脚 SizeF pageSize = pdf.Pages[0].Size == null ? PdfPageSize.A4 : pdf.Pages[0].Size;//画笔PdfPen ...
- java pdf 页眉_itext生成PDF设置页眉页脚的实例详解
itext生成PDF设置页眉页脚的实例详解 实例代码: /** * ITextTest * iText生成PDF加入列表,注释等内容,同时设置页眉和页脚及页码等. */ package com.lab ...
- c# .net生成pdf创建pdf,pdf签名pdf合并pdf增删页面页眉页脚批注旋转提取图片文本加水印等的类库SharpPDF
SharpPDF是一款在.net平台实现PDF生成和编辑的解决方案级产品.可以在Winform,WPF,WebAPI,WebService,MVC,WebForm等多种类型项目中,轻松实现一行代码生成 ...
- java导出pdf页码设置_itext生成PDF设置页眉页脚的实例详解
itext生成pdf设置页眉页脚的实例详解 实例代码: /** * itexttest * itext生成pdf加入列表,注释等内容,同时设置页眉和页脚及页码等. */ package com.lab ...
最新文章
- 华为交换机的一些OID
- 利用CSIDL打开特殊文件夹
- 动态规划 dynamic programming
- java 获取oracle表结构_获取Oracle中所有表的列表?
- MongoDB索引类型
- 区块链入门与去中心化应用实战 之一 第3章 区块链技术核心原理实现
- 华云网际:虚拟机+廉价服务器 一体机就这么简单
- 怎样设计访谈提纲_服务设计简史
- 证明的思路 —— 数形结合
- python中的turtle如何运行_python中turtle库的简单使用教程
- leetcode之奇偶链表
- 锋利的jQuery总结(三)
- 防止linux子系统关闭,Linux关闭selinux安全子系统的方法
- 黑马Java学科资料
- 数据建模 - 概念模型,逻辑模型,物理模型 的区别以及建模方式
- 聪明的猴子 黑暗爆炸 - 2429
- c语言_kbhit函数怎么用,kbhit再c语言中怎么用请教
- 计算机应用培训心得,计算机培训心得体会范文500字
- 住院病历的病历打印纸要求多大?
- 苹果手机sim卡无效怎么办_苹果手机解锁密码忘了怎么办