java將office文檔pdf文檔轉換成swf文件在線預覽
第一步,安裝openoffice.org
openoffice.org是一套sun的開源office辦公套件,能在widows,linux,solaris等操作系統上執行。
主要模塊有writer(文本文檔),impress(演示文稿),Calc(電子表格),Draw(繪圖),Math(公式),base(數據庫)
筆者下載的是openoffice.org 3.3.0。下載完直接安裝即可。
但是,我們還需要啟動openoffice server。有兩種做法:
1.以命令行方式啟動openoffice server,缺點是每次系統重啟,都需要手動去把openoffice server啟動。
2.將openoffice server作為操作系統的服務啟動,既然成為了系統服務,就可以設定開機自動啟動了。
我們先來看第一種方式,
1.以命令行方式啟動openoffice server
在cmd命令下,cd opeonofiice的安裝路徑/program 如:cd c:\program files\openoffice.org 3\program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.以系統服務的方式啟動
這里我們還需要Windows Resource Kit tools ,將openoffice server設為系統服務。
Windows Resource Kit tools 是微軟專為管理人員、開發人員和高級用戶開發的,包括管理活動目錄、組策略、TCP/IP網絡、注冊表、系統安全、監測等涉及Windows Server 2003 操作系統的其它很多方面的非常規安裝的工具組件。Resource Kit Tools for XP的發布使得XP用戶也能使用Resource Kit Tools對這些問題進行處理。
下載windows resource kit tools,我們進行默認安裝。
1.打開Windows Resource Kit Tools
在Command Shell執行以下命令:
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
打開 管理工具->服務 可以找到以 OpenOfficeUnoServer 命名的服務
2.打開注冊表尋找以下路徑
HKEY_LOCAL_MACHINE -> SYSTEM ->ControlSet001 ->Services ->OpenOfficeUnoServer
新建項 Parameters,在該項下添加兩個字符串值:
key:Application
value:C:\Program Files\OpenOffice.org 3\program\soffice.exe
key:AppParameters
value:-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard
3.在服務控制台,啟動 openoffice 服務
4.在CMD中用以下命令查看8100是否已被監聽:netstat -anop tcp
這樣OpenOffice3.0就以服務方式運行在Windows系統上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)
然後可以通過socket方式連接openOffice,以使用openoffice提供的某些服務,如文件轉換服務,ms office轉pdf等等。
開源項目 JODConverter 就是結合openoffice來進行文檔轉換的java組件。
另外有一個命令行工具swftools,該工具可以將pdf轉換為swf格式的文檔,提供給ie客戶端流覽。
另外,我們可以將該配置用bat文件來快速實現,運行前請先修改相應目錄參數:
openoffice service.bat文件
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /ve /d
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /v Application /t REG_SZ /d "C:\Program Files\OpenOffice.org 3\program\soffice.exe"
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /v AppParameters /t REG_SZ /d "-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard"
第二步,使用JODConverter將office文檔轉換為pdf
JODConverter是一個java的OpenDucument文件轉換器,可以進行許多文件格式的轉換,它利用
OpenOffice來進行轉換工作,它能進行以下的轉換工作:
1.Microsoft Office格式轉換為OpenDucument,以及OpenDucument轉換為Microsoft Office
2.OpenDucument轉換為PDF,Word、Excel、PowerPoint轉換為PDF,RTF轉換為PDF等。
它是一個開源項目。
我的項目是在MyEclipse下開發的。
下載最新版的jodconverter-2.2.2,把lib文件夾的包導入到你的DocConverter項目的lib文件夾內。
(假設你的項目是DocConverter)
新建DOC2PDFUtil.java
package com.iori.webapp.util;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Date;
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;
public class DOC2PDFUtil extends java.lang.Thread {
private File inputFile;// 需要轉換的文件
private File outputFile;// 輸出的文件
public DOC2PDFUtil(File inputFile, File outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
}
public void docToPdf() {
Date start = new Date();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
} catch (ConnectException cex) {
cex.printStackTrace();
} finally {
// close the connection
if (connection != null) {
connection.disconnect();
connection = null;
}
}
}
/**
* 由於服務是線程不安全的,所以……需要啟動線程
*/
public void run() {
this.docToPdf();
}
public File getInputFile() {
return inputFile;
}
public void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
public File getOutputFile() {
return outputFile;
}
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
/**
* 測試main方法
* @param args
*/
public static void main(String[] args) {
File inputFile = new File("c://temp//333.xls");
File outputFile = new File("c://temp//333.pdf");
DOC2PDFUtil dp=new DOC2PDFUtil(inputFile,outputFile);
dp.start();
}
}
在DOC2PDFUtil.java,右鍵屬性 - >Run as - >Java Application ,輸出main的測試結果。
在jsp中執行
新建MyDOC2PDFTest.jsp
<%@ page import="java.io.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.converter.*"%>
<%@ page import="com.artofsolving.jodconverter.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.iori.webapp.util.*"%>
<%
File inputFile = new File("c://temp//333.xls");
File outputFile = new File("c://temp//333.pdf");
DOC2PDFUtil dp=new DOC2PDFUtil(inputFile,outputFile);
dp.start();
%>
<!-- 下面這些html可以去掉 -->
<html>
<head><title>Simple jsp page</title></head>
<body>Place your content here</body>
</html>
在項目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application
發布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪問:Http://localhost:8080/DocConverter/MyDOC2PDFTest.jsp 進行測試。
JODConverter將office文檔轉換pdf,用到的代碼如下:
File inputFile = new File("c://temp//333.xls");
File outputFile = new File("c://temp//333.pdf");
// 鏈接 一個運行在8100端口的OpenOffice.org 實例
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// 創建一個converter對象並轉換格式
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// 關閉連接
connection.disconnect();
第三步,使用swftools將pdf轉換為swf
建議下載swftools-0.9.1,筆者起先下載的是最新版的swftools-1.0版。貌似轉換時出錯,缺少什么組件。
繼續筆者的DocConverter項目。筆者使用的開發環境是MyEclipse 9.0。
新建PDF2SWFUtil.java
package com.iori.webapp.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class PDF2SWFUtil {
/**
* 利用SWFTools工具將pdf轉換成swf,轉換完后的swf文件與pdf同名
* @author iori
* @param fileDir PDF文件存放路徑(包括文件名)
* @param exePath 轉換器安裝路徑
* @throws IOException
*/
public static synchronized void pdf2swf(String fileDir, String exePath) throws IOException {
//文件路徑
String filePath = fileDir.substring(0, fileDir.lastIndexOf("/"));
//文件名,不帶后綴
String fileName = fileDir.substring((filePath.length() + 1), fileDir.lastIndexOf("."));
Process pro = null;
if (isWindowsSystem()) {
//如果是windows系統
//命令行命令
String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\"";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
} else {
//如果是linux系統,路徑不能有空格,而且一定不能用雙引號,否則無法創建進程
String[] cmd = new String[3];
cmd[0] = exePath;
cmd[1] = fileDir;
cmd[2] = filePath + "/" + fileName + ".swf";
//Runtime執行后返回創建的進程對象
pro = Runtime.getRuntime().exec(cmd);
}
//非要讀取一遍cmd的輸出,要不不會flush生成文件(多線程)
new DoOutput(pro.getInputStream()).start();
new DoOutput(pro.getErrorStream()).start();
try {
//調用waitFor方法,是為了阻塞當前進程,直到cmd執行完
pro.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 判斷是否是windows操作系統
* @author iori
* @return
*/
private static boolean isWindowsSystem() {
String p = System.getProperty("os.name");
return p.toLowerCase().indexOf("windows") >= 0 ? true : false;
}
/**
* 多線程內部類
* 讀取轉換時cmd進程的標准輸出流和錯誤輸出流,這樣做是因為如果不讀取流,進程將死鎖
* @author iori
*/
private static class DoOutput extends Thread {
public InputStream is;
//構造方法
public DoOutput(InputStream is) {
this.is = is;
}
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
String str = null;
try {
//這里並沒有對流的內容進行處理,只是讀了一遍
while ((str = br.readLine()) != null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 測試main方法
* @param args
*/
public static void main(String[] args) {
//轉換器安裝路徑
String exePath = "c:/Program Files/SWFTools/pdf2swf.exe";
try {
PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath);
} catch (IOException e) {
System.err.println("轉換出錯!");
e.printStackTrace();
}
}
}
在PDF2SWFUtil.java,右鍵屬性 - >Run as - >Java Application ,輸出main的測試結果。
在jsp中執行
新建MyPDF2SWFTest.jsp
<%@ page import="java.io.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.converter.*"%>
<%@ page import="com.artofsolving.jodconverter.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.iori.webapp.util.*"%>
<%
//轉換器安裝路徑
String exePath = "c:/Program Files/SWFTools/pdf2swf.exe";
try {
PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath);
} catch (IOException e) {
System.err.println("轉換出錯!");
e.printStackTrace();
}
%>
<!-- 下面這些html可以去掉 -->
<html>
<head>
<title>Simple jsp page</title>
</head>
<body>Place your content here</body>
</html>
在項目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application
發布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪問:Http://localhost:8080/DocConverter/MyPDF2SWFTest.jsp 進行測試。
第四步,office文檔轉為pdf,同時進一步轉為swf
網上資料有很多office文檔轉為pdf,pdf轉為swf,但都是單步轉換。關於一起轉換的資料比較少。
一起轉換有個問題就是轉為pdf時,這個轉換過程將花費一段時間才能成功,如何控制在pdf轉換成功后,才進行swf的轉換。
以及多個文檔批量轉換又該怎么辦。
有幸筆者還是找到了一篇同時轉換的代碼:
新建DocConverter.java
package com.iori.webapp.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
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;
/*
* doc docx格式轉換
* @author Administrator
*/
public class DocConverter {
private static final int environment=1;//環境1:windows 2:linux(涉及pdf2swf路徑問題)
private String fileString;
private String outputPath="";//輸入路徑,如果不設置就輸出在默認位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;
public DocConverter(String fileString)
{
ini(fileString);
}
/*
* 重新設置 file
* @param fileString
*/
public void setFile(String fileString)
{
ini(fileString);
}
/*
* 初始化
* @param fileString
*/
private void ini(String fileString)
{
this.fileString=fileString;
fileName=fileString.substring(0,fileString.lastIndexOf("."));
docFile=new File(fileString);
pdfFile=new File(fileName+".pdf");
swfFile=new File(fileName+".swf");
}
/*
* 轉為PDF
* @param file
*/
private void doc2pdf() throws Exception
{
if(docFile.exists())
{
if(!pdfFile.exists())
{
OpenOfficeConnection connection=new SocketOpenOfficeConnection(8100);
try
{
connection.connect();
DocumentConverter converter=new OpenOfficeDocumentConverter(connection);
converter.convert(docFile,pdfFile);
//close the connection
connection.disconnect();
System.out.println("****pdf轉換成功,PDF輸出:"+pdfFile.getPath()+"****");
}
catch(java.net.ConnectException e)
{
//ToDo Auto-generated catch block
e.printStackTrace();
System.out.println("****swf轉換異常,openoffice服務未啟動!****");
throw e;
}
catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e)
{
e.printStackTrace();
System.out.println("****swf轉換器異常,讀取轉換文件失敗****");
throw e;
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
else
{
System.out.println("****已經轉換為pdf,不需要再進行轉化****");
}
}
else
{
System.out.println("****swf轉換器異常,需要轉換的文檔不存在,無法轉換****");
}
}
/*
* 轉換成swf
*/
private void pdf2swf() throws Exception
{
Runtime r=Runtime.getRuntime();
if(!swfFile.exists())
{
if(pdfFile.exists())
{
if(environment==1)//windows環境處理
{
try {
Process p=r.exec("C:/Program Files/SWFTools/pdf2swf.exe "+pdfFile.getPath()+" -o "+swfFile.getPath()+" -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf轉換成功,文件輸出:"+swfFile.getPath()+"****");
if(pdfFile.exists())
{
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
else if(environment==2)//linux環境處理
{
try {
Process p=r.exec("pdf2swf "+pdfFile.getPath()+" -o "+swfFile.getPath()+" -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf轉換成功,文件輸出:"+swfFile.getPath()+"****");
if(pdfFile.exists())
{
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
else {
System.out.println("****pdf不存在,無法轉換****");
}
}
else {
System.out.println("****swf已存在不需要轉換****");
}
}
static String loadStream(InputStream in) throws IOException
{
int ptr=0;
in=new BufferedInputStream(in);
StringBuffer buffer=new StringBuffer();
while((ptr=in.read())!=-1)
{
buffer.append((char)ptr);
}
return buffer.toString();
}
/*
* 轉換主方法
*/
public boolean conver()
{
if(swfFile.exists())
{
System.out.println("****swf轉換器開始工作,該文件已經轉換為swf****");
return true;
}
if(environment==1)
{
System.out.println("****swf轉換器開始工作,當前設置運行環境windows****");
}
else {
System.out.println("****swf轉換器開始工作,當前設置運行環境linux****");
}
try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
// TODO: Auto-generated catch block
e.printStackTrace();
return false;
}
if(swfFile.exists())
{
return true;
}
else {
return false;
}
}
/*
* 返回文件路徑
* @param s
*/
public String getswfPath()
{
if(swfFile.exists())
{
String tempString =swfFile.getPath();
tempString=tempString.replaceAll("\\\\", "/");
return tempString;
}
else{
return "";
}
}
/*
* 設置輸出路徑
*/
public void setOutputPath(String outputPath)
{
this.outputPath=outputPath;
if(!outputPath.equals(""))
{
String realName=fileName.substring(fileName.lastIndexOf("/"),fileName.lastIndexOf("."));
if(outputPath.charAt(outputPath.length())=='/')
{
swfFile=new File(outputPath+realName+".swf");
}
else
{
swfFile=new File(outputPath+realName+".swf");
}
}
}
public static void main(String s[])
{
DocConverter d=new DocConverter("c:/temp/111.ppt");
d.conver();
}
}
在DocConverter.java,右鍵屬性 - >Run as - >Java Application ,輸出main的測試結果。筆者分別進行單個轉換,及批量轉換,都測試可行。
至於為什么能成功進行pdf及swf的完整轉換,在代碼中沒有看到和上述問題相關的控制。筆者在得到預期的結果,偶爾也會裝糊塗,不去繼續深究。
第五步,flexpaper在線瀏覽swf文檔
FlexPaper是一個開源輕量級的在瀏覽器上顯示各種文檔的組件,被設計用來與PDF2SWF一起使用,
使在Flex中顯示PDF成為可能,而這個過程並無需PDF軟件環境的支持。它可以被當做Flex的庫來使用。
另外你也可以通過將一些例如Word、PPT等文檔轉成PDF,然后實現在線瀏覽。
FlexPaper_1.2.4_flash:無打印功能
FlexPaper_1.4.7_flash:打印功能,右鍵打印
這里我們不需要讓用戶打印,所以筆者選擇FlexPaper_1.2.4_flash。
FlexPaper項目中有演示demo,這里筆者不多述。
綜上,一個完整的在線文檔瀏覽方案。
附一:使用iText將jpg、jpeg、png轉換為pdf
其他,使用iText將jpg/jpeg/png轉換為pdf
iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成 PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。1.在企業的信息系統中,報表處理一直占比較重要的作用,iText--一種生 成PDF報表的Java組件,通過在服務器端使用Jsp或JavaBean生成PDF報表,客戶端采用超級連接顯示或下載得到生成的報表,這樣就很好的解 決了B/S系統的報表處理問題。2.支持文本,表格,圖形的操作,可以方便的跟 Servlet 進行結合。
繼續筆者的DocConverter項目。開發環境是MyEclipse 9.0。筆者下載的是iText5.0.4。
新建JPG2PDFUtil.java
package com.iori.webapp.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class JPG2PDFUtil {
private String inputFileString;
private String outputFileString;
public JPG2PDFUtil(String inputFile, String outputFile) {
this.inputFileString = inputFile;
this.outputFileString = outputFile;
}
public void imgtopdf()
{
//創建一個文檔對象
Document doc = new Document();
try {
//定義輸出文件的位置
PdfWriter.getInstance(doc, new FileOutputStream(outputFileString));
//開啟文檔
doc.open();
//設定字體 為的是支持中文
//BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
//向文檔中加入圖片
/*//以下是多圖合成一個pdf,暫時用不到
for(int i=1;i<32;i++)
{
//取得圖片~~~圖片格式:
Image jpg1 = Image.getInstance("c:/"+i+".jpg"); //原來的圖片的路徑
//獲得圖片的高度
float heigth=jpg1.height();
float width=jpg1.width();
System.out.println("heigth"+i+"----"+heigth);
System.out.println("width"+i+"-----"+width);
//合理壓縮,h>w,按w壓縮,否則按w壓縮
//int percent=getPercent(heigth, width);
//統一按照寬度壓縮
int percent=getPercent2(heigth, width);
//設置圖片居中顯示
jpg1.setAlignment(Image.MIDDLE);
//直接設置圖片的大小~~~~~~~第三種解決方案,按固定比例壓縮
//jpg1.scaleAbsolute(210.0f, 297.0f);
//按百分比顯示圖片的比例
jpg1.scalePercent(percent);//表示是原來圖像的比例;
//可設置圖像高和寬的比例
//jpg1.scalePercent(50, 100);
doc.add(jpg1);
}
*/
//向文檔中加入圖片
//取得圖片~~~圖片格式:
Image jpg1 = Image.getInstance(inputFileString); //原來的圖片的路徑
//獲得圖片的高度
float heigth=jpg1.height();
float width=jpg1.width();
System.out.println("heigth----"+heigth);
System.out.println("width-----"+width);
//合理壓縮,h>w,按w壓縮,否則按w壓縮
//int percent=getPercent(heigth, width);
//統一按照寬度壓縮
int percent=getPercent2(heigth, width);
//設置圖片居中顯示
jpg1.setAlignment(Image.MIDDLE);
//直接設置圖片的大小~~~~~~~第三種解決方案,按固定比例壓縮
//jpg1.scaleAbsolute(210.0f, 297.0f);
//按百分比顯示圖片的比例
jpg1.scalePercent(percent);//表示是原來圖像的比例;
//可設置圖像高和寬的比例
//jpg1.scalePercent(50, 100);
doc.add(jpg1);
//關閉文檔並釋放資源
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 第一種解決方案
* 在不改變圖片形狀的同時,判斷,如果h>w,則按h壓縮,否則在w>h或w=h的情況下,按寬度壓縮
* @param h
* @param w
* @return
*/
public int getPercent(float h,float w)
{
int p=0;
float p2=0.0f;
if(h>w)
{
p2=297/h*100;
}
else
{
p2=210/w*100;
}
p=Math.round(p2);
return p;
}
/**
* 第二種解決方案,統一按照寬度壓縮
* 這樣來的效果是,所有圖片的寬度是相等的,自我認為給客戶的效果是最好的
* @param args
*/
public int getPercent2(float h,float w)
{
int p=0;
float p2=0.0f;
p2=530/w*100;
p=Math.round(p2);
return p;
}
/**
* 第三種解決方案,就是直接壓縮,不安像素比例,全部壓縮到固定值,如210*297
*
* @param args
*/
public static void main(String[] args) {
JPG2PDFUtil pt=new JPG2PDFUtil("c:/temp/ddd.jpg","c:/temp/ddd.pdf");
pt.imgtopdf();
}
}
在JPG2PDFUtil.java,右鍵屬性 - >Run as - >Java Application ,輸出main的測試結果。
在jsp中執行
新建MyJPG2PDFTest.jsp
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.iori.webapp.util.*"%>
<%
JPG2PDFUtil pt=new JPG2PDFUtil("c:/temp/333.jpg", "c:/temp/333.pdf");
pt.imgtopdf();
%>
<!-- 下面這些html可以去掉 -->
<html>
<head>
<title>Simple jsp page</title>
</head>
<body>Place your content here</body>
</html>
在項目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application
發布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪問:Http://localhost:8080/DocConverter/MyDOC2PDFTest.jsp 進行測試。
附二:常見問題集FAQ
1.txt轉換swf,發生中文亂碼。
txt轉換為utf-8編碼,或txt格式手動改為odt,上傳就不會發生亂碼。從根源上解決,暫時就算了...暫時不想去糾結這些雞毛。
2.加密的pdf可能導致轉換為swf失敗。
3.Microsoft Excel在公式運算中支持文本型的數值,而OpenOffice.org Calc不支持
此問題暫無解,請手動將Excel中文本型的數值修改為數值型的數值。
4.部分Excel存在過於豐富的樣式(大部分指沒有數據的單元格也填充了各種樣式),即使用專業Adobe Acrobat 7(或9) Pro來進行轉換,
本來可能預計將產生20-30分頁的pdf,結果卻產生800-900分頁的pdf。此類文檔在線轉換,難以避免的將導致轉換死鎖。
請在你的Excel文檔中刪除多余的,毫無必要的樣式,或者你有更靈活的做法。
5.有些中文PDF文件轉換為SWF后,出現亂碼(特別一些專業期刊)
1.下載XPDF:xpdf-chinese-simplified.tar.gz
2.下載字體:gkai00mp.rar
3.修改xpdf-chinese-simplified目錄下的add-to-xpdfrc文件。將里面的路徑設為自己的路徑:
#----- begin Chinese Simplified support package (2011-sep-02)
cidToUnicode Adobe-GB1 C:\xpdf-chinese-simplified\Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN C:\xpdf-chinese-simplified\ISO-2022-CN.unicodeMap
unicodeMap EUC-CN C:\xpdf-chinese-simplified\EUC-CN.unicodeMap
unicodeMap GBK C:\xpdf-chinese-simplified\GBK.unicodeMap
cMapDir Adobe-GB1 C:\xpdf-chinese-simplified\CMap
toUnicodeDir C:\xpdf-chinese-simplified\CMap
fontDir C:\WINDOWS\Fonts
displayCIDFontTT Adobe-GB1 C:\xpdf-chinese-simplified\CMap\gkai00mp.ttf
#fontFileCC Adobe-GB1 /usr/..../gkai00mp.ttf
#----- end Chinese Simplified support package
4.參照上面的代碼,在調用pdf2swf命令中加入“ -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ”參數。
PDF2SWFUtil.java
String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\" -T 9 -s languagedir=c:\\xpdf-chinese-simplified";
這樣亂碼的問題就解決了。