日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

jodconverter水印java,OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚)

發(fā)布時(shí)間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jodconverter水印java,OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java OpenOffice officetopdf

最近項(xiàng)目需要實(shí)現(xiàn)下載Office文檔時(shí)自動(dòng)轉(zhuǎn)成PDF文檔,以下代碼支持2003及2007版的Word,PPT,Excel轉(zhuǎn)換,并支持自定義添加水印、頁(yè)眉、頁(yè)腳

實(shí)現(xiàn)需要事先安裝OpenOffice(我這里安裝的是OpenOffice 4)

OpenOffice 下載: http://www.openoffice.org/

JodConverter 下載地址 http://sourceforge.net/projects/jodconverter/files/JODConverter

需要代碼正常運(yùn)行還需要其他Jar包(見(jiàn)附件)

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測(cè)試.docx";

//String inputFilePath = "F://officeToPdf/pptToPdf測(cè)試.pptx";

//String inputFilePath = "F://officeToPdf/xlsxToPdf測(cè)試.xlsx";

String outputFilePath = getOutputFilePath(inputFilePath);

//Office轉(zhuǎn)換成Pdf

OfficeToPdf.office2pdf(inputFilePath,outputFilePath);

//添加水印、頁(yè)眉、頁(yè)腳

addFooterAndWater("F://officeToPdf//WordToPdf測(cè)試.pdf", "F://officeToPdf//WordToPdf測(cè)試2.pdf", "WordToPdf水印嚴(yán)禁復(fù)制", "WordToPdf頁(yè)眉", "WordToPdf頁(yè)腳");

}

/**

* 將Office文檔轉(zhuǎn)換為PDF. 需要安裝OpenOffice

*

* @param inputFilePath

* 源文件,絕對(duì)路徑. 可以是Office2003-2007全部格式的文檔, 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等.

*

* @param outputFilePath

* 目標(biāo)文件.絕對(duì)路徑.

*/

public static void office2pdf(String inputFilePath,String outputFilePath) {

DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();

String officeHome = getOfficeHome();

//設(shè)置OpenOffice.org安裝目錄

config.setOfficeHome(officeHome);

//設(shè)置轉(zhuǎn)換端口,默認(rèn)為8100

//config.setPortNumbers(8100);

//設(shè)置任務(wù)執(zhí)行超時(shí)為60分鐘

config.setTaskExecutionTimeout(1000 * 60 * 60L);

//設(shè)置任務(wù)隊(duì)列超時(shí)為24小時(shí)

config.setTaskQueueTimeout(1000 * 60 * 60 * 24L);

OfficeManager officeManager = config.buildOfficeManager();

officeManager.start();

System.out.println("office轉(zhuǎn)換服務(wù)啟動(dòng)成功!");

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);

File inputFile = new File(inputFilePath);

if (inputFile.exists()) {// 找不到源文件, 則返回

File outputFile = new File(outputFilePath);

if (!outputFile.getParentFile().exists()) { // 假如目標(biāo)路徑不存在, 則新建該路徑

outputFile.getParentFile().mkdirs();

}

converter.convert(inputFile, outputFile);

}

if (null != officeManager){

officeManager.stop();

System.out.println("office轉(zhuǎn)換服務(wù)完成。");

}

}

/**

* 根據(jù)源文件路徑獲取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;

}

/**

* 添加水印、頁(yè)眉、頁(yè)腳

* @param fileName 源文件路徑

* @param savepath 目標(biāo)文件路徑

* @param waterMarkName 文字水印

* @param pageHeade 頁(yè)眉

* @param foot 頁(yè)腳

* @return

*/

public static int addFooterAndWater(String fileName, String savepath,

String waterMarkName, String pageHeade, String foot)

{

// 文檔總頁(yè)數(shù)

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

// 添加頁(yè)腳,左側(cè)文字,右側(cè)頁(yè)碼

ColumnText.showTextAligned(stamp.getUnderContent(),

Element.ALIGN_RIGHT,

new Phrase(String.format("第 %d 頁(yè)/共 %d 頁(yè)", i, num), f),

550f, 28, 0);

ColumnText.showTextAligned(stamp.getUnderContent(),

Element.ALIGN_LEFT, new Phrase(foot, f), 50f, 28, 0);

// 添加頁(yè)眉 (文字頁(yè)眉,居中)

ColumnText.showTextAligned(stamp.getUnderContent(),

Element.ALIGN_CENTER, new Phrase(pageHeade, f), 150f,

800, 0);

// 頁(yè)眉添加logo (圖片頁(yè)眉,居右)

/*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);

// 字符越長(zhǎng),字體越小,設(shè)置字體

int fontSize = getFontSize(waterMarkName);

under.setFontAndSize(base, fontSize);

// 設(shè)置水印文字字體傾斜 開(kāi)始

float pageWidth = reader.getPageSize(i).getWidth();

float pageHeight = reader.getPageSize(i).getHeight();

under.showTextAligned(Element.ALIGN_CENTER, waterMarkName,

pageWidth / 2, pageHeight / 2, 60);// 水印文字成60度角傾斜,且頁(yè)面居中展示

// 字體設(shè)置結(jié)束

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;

}

/**

* 根據(jù)水印文字長(zhǎng)度計(jì)算獲取字體大小

* @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;

}

}

總結(jié)

以上是生活随笔為你收集整理的jodconverter水印java,OpenOffice实现Office转Pdf(支持自定义添加水印、页眉、页脚)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。