openOffice實現word轉pdf和新增水印
阿新 • • 發佈:2019-01-30
ipenOffice的demo,實現word轉成pdf同時向pdf新增水印
package html2pdf;
import java.awt.Color;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class D2P {
public static void main(String[] argv) {
//String pdfPath = argv[0];
String pdfPath = "C:\\Users\\qianql\\Desktop\\測試\\16BSB16";
BufferedOutputStream bos;
try {
office2PDF(pdfPath+".doc", pdfPath+"temp.pdf");
bos = new BufferedOutputStream(new FileOutputStream(new File(pdfPath+".pdf")));
setWatermark(bos,pdfPath+"temp.pdf");
(new File(pdfPath+"temp.pdf")).delete();
System.out.println("轉換成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int office2PDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到原始檔, 則返回-1
}
// 如果目標路徑不存在, 則新建該路徑
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
String OpenOffice_HOME = "D:\\Program Files\\MyDrivers\\OpenOffice4.1.3\\OpenOffice 4";//這裡是OpenOffice的安裝目錄, 在我的專案中,為了便於拓展介面,沒有直接寫成這個樣子,但是這樣是絕對沒問題的
// 如果從檔案中讀取的URL地址最後一個字元不是 '\',則新增'\'
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
OpenOffice_HOME += "\\";
}
// 啟動OpenOffice的服務
String command = OpenOffice_HOME
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process pro = Runtime.getRuntime().exec(command);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
// OpenOfficeConnection connection = new SocketOpenOfficeConnection("yourIP",8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
// 關閉OpenOffice服務的程序
pro.destroy();
return 0;
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
public static void setWatermark(BufferedOutputStream bos, String input) throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//BaseFont base = BaseFont.createFont("/data/tmis/uploads/file/font/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
// content = stamper.getOverContent(i);// 在內容上方加水印
content = stamper.getUnderContent(i);// 在內容下方加水印
gs.setFillOpacity(0.2f);
// content.setGState(gs);
content.beginText();
content.setRGBColorFill(192, 192, 192);
content.setFontAndSize(base, 50);
content.setTextMatrix(100, 250);
content.showTextAligned(Element.ALIGN_CENTER, "安徽省建築工程質量第二監督檢測站", 250, 400, 55);
// content.showTextAligned(Element.ALIGN_CENTER,
// "檢測管理資訊系統!",400,250, 55);
content.setRGBColorFill(0,0,0);
content.setFontAndSize(base, 8);
content.endText();
}
stamper.close();
}
}