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;
}
}

原文地址:https://www.cnblogs.com/7231485/p/11577777.html

时间: 2024-08-29 18:16:02

OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚)的相关文章

iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片

本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉图片 原来是决定采用wkhtmlToPdf将html页面转换为pdf,而且html样式保存的还挺好,但是最后尝试下来发现,他转换不了我们框架中的html页面,将框架中的html页面地址进行转换总是会转换成首页的图片,多次查询无果,最终放弃了,改换成itext工具. 由于我们的需求中要求有页眉和页脚,

itext 生成pdf文件添加页眉页脚

原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpdf是itext的升级版本,并且itextpdf-5.5.jar以上版本算总页数不需要-1,之前版本必须-1: jfinal.jar只能用2.0版本,用3.0的会报版本过高:希望能帮到有需要的人 itextpdf-5.5.13.jar 下载地址:http://central.maven.org/mav

PDF文件怎么添加页眉页脚,有什么简单的方法吗?

PDF文件怎么添加页眉页脚呢?我们现在对PDF文件也算是比较熟悉了,但是对于PDF文件怎么编辑还不是那么了解,其实我们想要给PDF文件添加页眉页脚也需要使用到迅捷PDF编辑器,下面小编就为大家操作一下PDF编辑器给PDF文件添加页眉页脚的操作步骤, 操作软件:迅捷PDF编辑器 1:我们将PDF编辑器安装到自己的电脑中,双击将PDF编辑器打开,然后将需要编辑的PDF文件添加到软件中来. 2:在软件的顶部找到文档,点击文档可以找到页眉页脚,点击页眉页脚就可以找到添加了. 3:点击添加就会出现一个弹窗

c# iText 生成PDF 有文字,图片,表格,文字样式,对齐方式,页眉页脚,等等等,

#region 下载说明书PDF protected void lbtnDownPDF_Click(object sender, EventArgs e) { int pid = ConvertHelper.GetInteger(Request["PID"]); pds = p.GetModel(pid); #region 第一种方法 DataTable datatable = new DataTable(dt.ToString()); try { string Url1 = &quo

【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类,实现第几页/共几页,方便各位刚进入itext的童鞋调用.2013年9月16日 14:51:01 背景:网上流传的都是一些简单的demo,直接扔个A4文档就说实现了,其实很坑的,换个A4横版打印,那些代码就不行了,所以自己搞了这个出来. 如果页面看着排版不好,请移步我的百度空间 :http://hi

PDF页眉页脚删除用什么方法

PDF页眉页脚删除用什么方法.相信每一个小伙伴都在网络上下载过PDF文件,当我们打开PDF文件后,发现文件里有着我们不需要的页眉与页脚,我们应该如何将其删除掉呢?如果屏幕前的你不清楚的话,那就和小编一起学习学习吧. 操作软件:迅捷PDF编辑器 工具下载地址:https://www.xunjiepdf.com/editor 1.首先我们需要下载一款能够编辑PDF文件的工具来使用,这里小编使用的是迅捷PDF编辑器. 2.利用迅捷PDF编辑器将我们需要的PDF文件打开,打开文件后我们可以看到页面中的页

如何使用PDF编辑工具添加PDF页眉页脚

不管是在学习中还是在工作中,都会使用到PDF文件,在很多的时候,大家都会使用到PDF文件,对于PDF文件的修改,则是需要使用到PDF编辑软件的,那么,如何使用PDF编辑工具添加PDF页眉页脚呢,是不是有很多的小伙伴都想知道应该怎么做呢,那就可以看看下面的文章,说不定就知道了哦. 1.打开运行迅捷PDF编辑器,在编辑器中打开需要修改的PDF文件. 2.打开文件后,选择编辑器中菜单栏里的文档,然后选择文档中的页眉页脚,在页眉页脚工具中有添加,全部删除以及管理,点击添加. 3.点击添加后,在添加的页面

如何给PDF文件添加页眉页脚?你知道哪种方法简单吗?

如何给PDF文件添加页眉页脚呢?我们在使用PDF文件的时候,想要给PDF文件添加页眉页脚,但是不知道怎么操作,其实想要给PDF文件添加页眉页脚还是比较简单的,下面小编就为大家操作一下PDF编辑器给PDF文件添加页眉页脚的方法. 操作软件:迅捷PDF编辑器 具体操作方法: 1:首先将迅捷PDF编辑器安装到电脑中,双击打开PDF编辑器,将PDF文件添加到软件中. 2:在软件顶部找到文档,点击文档找到页眉页脚,将鼠标移动到页眉页脚的位置,在右侧就可以找到添加. 3:点击添加在软件中就会出现一个弹窗,在

PDF如何添加页眉页脚?PDF添加页眉页脚的小技巧

PDF文件如何添加页眉页脚呢?很多实用PDF文件的人都知道PDF文件无法直接进行编辑,将要直接给PDF添加页眉页脚也是比较难得,想要给PDF添加页眉页脚就可以使用专业的PDF编辑器来进行操作.下面就为大家分享一下PDF添加页眉页脚的操作方法. 1:首先将迅捷PDF编辑器安装到自己电脑中,打开PDF编辑器将PDF文件添加到软件中. 2:在软件顶部找到文档,点击文档在下面可以找到页眉页脚,将鼠标移动到页眉页脚的位置,在右侧可以找到添加. 3:点击添加,在软件中会出现一个选择框,在选择框中可以直接填写