国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
java將office文檔pdf文檔轉(zhuǎn)換成swf文件在線(xiàn)預(yù)覽

第一步,安裝openoffice.org

  openoffice.org是一套sun的開(kāi)源office辦公套件,能在widows,linux,solaris等操作系統(tǒng)上執(zhí)行。

主要模塊有writer(文本文檔),impress(演示文稿),Calc(電子表格),Draw(繪圖),Math(公式),base(數(shù)據(jù)庫(kù))

筆者下載的是openoffice.org 3.3.0。下載完直接安裝即可。

 

     但是,我們還需要啟動(dòng)openoffice server。有兩種做法:

    1.以命令行方式啟動(dòng)openoffice server,缺點(diǎn)是每次系統(tǒng)重啟,都需要手動(dòng)去把openoffice server啟動(dòng)。

    2.將openoffice server作為操作系統(tǒng)的服務(wù)啟動(dòng),既然成為了系統(tǒng)服務(wù),就可以設(shè)定開(kāi)機(jī)自動(dòng)啟動(dòng)了。

    我們先來(lái)看第一種方式,

1.以命令行方式啟動(dòng)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.以系統(tǒng)服務(wù)的方式啟動(dòng)

    這里我們還需要Windows Resource Kit tools ,將openoffice server設(shè)為系統(tǒng)服務(wù)。

Windows Resource Kit tools 是微軟專(zhuān)為管理人員、開(kāi)發(fā)人員和高級(jí)用戶(hù)開(kāi)發(fā)的,包括管理活動(dòng)目錄、組策略、TCP/IP網(wǎng)絡(luò)、注冊(cè)表、系統(tǒng)安全、監(jiān)測(cè)等涉及Windows Server 2003 操作系統(tǒng)的其它很多方面的非常規(guī)安裝的工具組件。Resource Kit Tools for XP的發(fā)布使得XP用戶(hù)也能使用Resource Kit Tools對(duì)這些問(wèn)題進(jìn)行處理。

    下載windows resource kit tools,我們進(jìn)行默認(rèn)安裝。

     1.打開(kāi)Windows Resource Kit Tools

     在Command Shell執(zhí)行以下命令:

 "C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"

打開(kāi) 管理工具->服務(wù) 可以找到以 OpenOfficeUnoServer 命名的服務(wù)

    2.打開(kāi)注冊(cè)表尋找以下路徑

    HKEY_LOCAL_MACHINE -> SYSTEM ->ControlSet001 ->Services ->OpenOfficeUnoServer

  新建項(xiàng) Parameters,在該項(xiàng)下添加兩個(gè)字符串值:

  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.在服務(wù)控制臺(tái),啟動(dòng) openoffice 服務(wù)

     4.在CMD中用以下命令查看8100是否已被監(jiān)聽(tīng):netstat -anop tcp

這樣OpenOffice3.0就以服務(wù)方式運(yùn)行在Windows系統(tǒng)上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)

然後可以通過(guò)socket方式連接openOffice,以使用openoffice提供的某些服務(wù),如文件轉(zhuǎn)換服務(wù),ms office轉(zhuǎn)pdf等等。

開(kāi)源項(xiàng)目 JODConverter 就是結(jié)合openoffice來(lái)進(jìn)行文檔轉(zhuǎn)換的java組件。

另外有一個(gè)命令行工具swftools,該工具可以將pdf轉(zhuǎn)換為swf格式的文檔,提供給ie客戶(hù)端流覽。    

  另外,我們可以將該配置用bat文件來(lái)快速實(shí)現(xiàn),運(yùn)行前請(qǐng)先修改相應(yīng)目錄參數(shù):

     openoffice service.bat文件

    "C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
    reg addHKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters/ve /d
    reg addHKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters/v Application /t REG_SZ /d "C:\Program Files\OpenOffice.org3\program\soffice.exe"
    reg addHKEY_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文檔轉(zhuǎn)換為pdf

JODConverter是一個(gè)java的OpenDucument文件轉(zhuǎn)換器,可以進(jìn)行許多文件格式的轉(zhuǎn)換,它利用

OpenOffice來(lái)進(jìn)行轉(zhuǎn)換工作,它能進(jìn)行以下的轉(zhuǎn)換工作:

     1.Microsoft Office格式轉(zhuǎn)換為OpenDucument,以及OpenDucument轉(zhuǎn)換為Microsoft Office

     2.OpenDucument轉(zhuǎn)換為PDF,Word、Excel、PowerPoint轉(zhuǎn)換為PDF,RTF轉(zhuǎn)換為PDF等。

它是一個(gè)開(kāi)源項(xiàng)目。

 

我的項(xiàng)目是在MyEclipse下開(kāi)發(fā)的。

下載最新版的jodconverter-2.2.2,把lib文件夾的包導(dǎo)入到你的DocConverter項(xiàng)目的lib文件夾內(nèi)。

(假設(shè)你的項(xiàng)目是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;// 需要轉(zhuǎn)換的文件         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;                }            }        }              /**         * 由于服務(wù)是線(xiàn)程不安全的,所以……需要啟動(dòng)線(xiàn)程          */       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;        }             /**       * 測(cè)試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的測(cè)試結(jié)果。

在jsp中執(zhí)行

新建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>

 

在項(xiàng)目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application

發(fā)布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪(fǎng)問(wèn):Http://localhost:8080/DocConverter/MyDOC2PDFTest.jsp 進(jìn)行測(cè)試。

JODConverter將office文檔轉(zhuǎn)換pdf,用到的代碼如下:

File inputFile = new File("c://temp//333.xls");File outputFile = new File("c://temp//333.pdf"); // 鏈接 一個(gè)運(yùn)行在8100端口的OpenOffice.org 實(shí)例OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);connection.connect(); // 創(chuàng)建一個(gè)converter對(duì)象并轉(zhuǎn)換格式DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile); // 關(guān)閉連接connection.disconnect();

第三步,使用swftools將pdf轉(zhuǎn)換為swf

建議下載swftools-0.9.1,筆者起先下載的是最新版的swftools-1.0版。貌似轉(zhuǎn)換時(shí)出錯(cuò),缺少什么組件。

     繼續(xù)筆者的DocConverter項(xiàng)目。筆者使用的開(kāi)發(fā)環(huán)境是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轉(zhuǎn)換成swf,轉(zhuǎn)換完后的swf文件與pdf同名       * @author iori      * @param fileDir PDF文件存放路徑(包括文件名)       * @param exePath 轉(zhuǎn)換器安裝路徑       * @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系統(tǒng)              //命令行命令              String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\"";            //Runtime執(zhí)行后返回創(chuàng)建的進(jìn)程對(duì)象              pro = Runtime.getRuntime().exec(cmd);        } else {            //如果是linux系統(tǒng),路徑不能有空格,而且一定不能用雙引號(hào),否則無(wú)法創(chuàng)建進(jìn)程              String[] cmd = new String[3];            cmd[0] = exePath;            cmd[1] = fileDir;            cmd[2] = filePath + "/" + fileName + ".swf";            //Runtime執(zhí)行后返回創(chuàng)建的進(jìn)程對(duì)象              pro = Runtime.getRuntime().exec(cmd);        }        //非要讀取一遍cmd的輸出,要不不會(huì)flush生成文件(多線(xiàn)程)         new DoOutput(pro.getInputStream()).start();        new DoOutput(pro.getErrorStream()).start();        try {            //調(diào)用waitFor方法,是為了阻塞當(dāng)前進(jìn)程,直到cmd執(zhí)行完             pro.waitFor();        } catch (InterruptedException e) {           e.printStackTrace();        }    }         /**      * 判斷是否是windows操作系統(tǒng)       * @author iori      * @return      */    private static boolean isWindowsSystem() {        String p = System.getProperty("os.name");        return p.toLowerCase().indexOf("windows") >= 0 ? true : false;    }         /**      * 多線(xiàn)程內(nèi)部類(lèi)       * 讀取轉(zhuǎn)換時(shí)cmd進(jìn)程的標(biāo)準(zhǔn)輸出流和錯(cuò)誤輸出流,這樣做是因?yàn)槿绻蛔x取流,進(jìn)程將死鎖       * @author iori      */    private static class DoOutput extends Thread {        public InputStream is;              //構(gòu)造方法         public DoOutput(InputStream is) {            this.is = is;        }              public void run() {            BufferedReader br = new BufferedReader(new InputStreamReader(this.is));            String str = null;            try {                //這里并沒(méi)有對(duì)流的內(nèi)容進(jìn)行處理,只是讀了一遍                  while ((str = br.readLine()) != null);            } catch (IOException e) {                e.printStackTrace();            } finally {                if (br != null) {                    try {                        br.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }        }    }         /**      * 測(cè)試main方法       * @param args      */    public static void main(String[] args) {        //轉(zhuǎn)換器安裝路徑         String exePath = "c:/Program Files/SWFTools/pdf2swf.exe";        try {            PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath);        } catch (IOException e) {            System.err.println("轉(zhuǎn)換出錯(cuò)!");            e.printStackTrace();        }    }}

在PDF2SWFUtil.java,右鍵屬性 - >Run as - >Java Application ,輸出main的測(cè)試結(jié)果。

 

在jsp中執(zhí)行

新建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.*"%>
<%
//轉(zhuǎn)換器安裝路徑
String exePath = "c:/Program Files/SWFTools/pdf2swf.exe";
try {
    PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath);
} catch (IOException e) {
    System.err.println("轉(zhuǎn)換出錯(cuò)!");
    e.printStackTrace();
}
%>
<!-- 下面這些html可以去掉 -->
<html>
<head>
<title>Simple jsp page</title>
</head>
<body>Place your content here</body>
</html>

 

在項(xiàng)目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application

發(fā)布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪(fǎng)問(wèn):Http://localhost:8080/DocConverter/MyPDF2SWFTest.jsp 進(jìn)行測(cè)試。

***第四步,office文檔轉(zhuǎn)為pdf,同時(shí)進(jìn)一步轉(zhuǎn)為swf

網(wǎng)上資料有很多office文檔轉(zhuǎn)為pdf,pdf轉(zhuǎn)為swf,但都是單步轉(zhuǎn)換。關(guān)于一起轉(zhuǎn)換的資料比較少。

一起轉(zhuǎn)換有個(gè)問(wèn)題就是轉(zhuǎn)為pdf時(shí),這個(gè)轉(zhuǎn)換過(guò)程將花費(fèi)一段時(shí)間才能成功,如何控制在pdf轉(zhuǎn)換成功后,才進(jìn)行swf的轉(zhuǎn)換。

以及多個(gè)文檔批量轉(zhuǎn)換又該怎么辦。

 

     有幸筆者還是找到了一篇同時(shí)轉(zhuǎn)換的代碼:

新建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格式轉(zhuǎn)換 * @author Administrator */public class DocConverter {    private static final int environment=1;//環(huán)境1:windows 2:linux(涉及pdf2swf路徑問(wèn)題)    private String fileString;    private String outputPath="";//輸入路徑,如果不設(shè)置就輸出在默認(rèn)位置    private String fileName;    private File pdfFile;    private File swfFile;    private File docFile;        public DocConverter(String fileString)    {        ini(fileString);    }        /*     * 重新設(shè)置 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");    }        /*     * 轉(zhuǎn)為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轉(zhuǎn)換成功,PDF輸出:"+pdfFile.getPath()+"****");                }                catch(java.net.ConnectException e)                {                    //ToDo Auto-generated catch block                    e.printStackTrace();                    System.out.println("****swf轉(zhuǎn)換異常,openoffice服務(wù)未啟動(dòng)!****");                    throw e;                }                catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e)                {                    e.printStackTrace();                    System.out.println("****swf轉(zhuǎn)換器異常,讀取轉(zhuǎn)換文件失敗****");                    throw e;                }                catch(Exception e)                {                    e.printStackTrace();                    throw e;                }            }            else            {                System.out.println("****已經(jīng)轉(zhuǎn)換為pdf,不需要再進(jìn)行轉(zhuǎn)化****");            }        }        else        {            System.out.println("****swf轉(zhuǎn)換器異常,需要轉(zhuǎn)換的文檔不存在,無(wú)法轉(zhuǎn)換****");        }    }        /*     * 轉(zhuǎn)換成swf     */    private void pdf2swf() throws Exception    {        Runtime r=Runtime.getRuntime();        if(!swfFile.exists())        {            if(pdfFile.exists())            {                if(environment==1)//windows環(huán)境處理                {                    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轉(zhuǎn)換成功,文件輸出:"+swfFile.getPath()+"****");                        if(pdfFile.exists())                        {                            pdfFile.delete();                        }                    } catch (Exception e) {                        e.printStackTrace();                        throw e;                    }                }                else if(environment==2)//linux環(huán)境處理                {                    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轉(zhuǎn)換成功,文件輸出:"+swfFile.getPath()+"****");                        if(pdfFile.exists())                        {                            pdfFile.delete();                        }                    } catch (Exception e) {                        e.printStackTrace();                        throw e;                    }                }            }            else {                System.out.println("****pdf不存在,無(wú)法轉(zhuǎn)換****");            }        }        else {            System.out.println("****swf已存在不需要轉(zhuǎn)換****");        }    }        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();    }        /*     * 轉(zhuǎn)換主方法     */    public boolean conver()    {        if(swfFile.exists())        {            System.out.println("****swf轉(zhuǎn)換器開(kāi)始工作,該文件已經(jīng)轉(zhuǎn)換為swf****");            return true;        }                if(environment==1)        {            System.out.println("****swf轉(zhuǎn)換器開(kāi)始工作,當(dāng)前設(shè)置運(yùn)行環(huán)境windows****");        }        else {            System.out.println("****swf轉(zhuǎn)換器開(kāi)始工作,當(dāng)前設(shè)置運(yùn)行環(huán)境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 "";        }    }        /*     * 設(shè)置輸出路徑     */    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的測(cè)試結(jié)果。筆者分別進(jìn)行單個(gè)轉(zhuǎn)換,及批量轉(zhuǎn)換,都測(cè)試可行。

至于為什么能成功進(jìn)行pdf及swf的完整轉(zhuǎn)換,在代碼中沒(méi)有看到和上述問(wèn)題相關(guān)的控制。筆者在得到預(yù)期的結(jié)果,偶爾也會(huì)裝糊涂,不去繼續(xù)深究。

第五步,flexpaper在線(xiàn)瀏覽swf文檔

FlexPaper是一個(gè)開(kāi)源輕量級(jí)的在瀏覽器上顯示各種文檔的組件,被設(shè)計(jì)用來(lái)與PDF2SWF一起使用,

使在Flex中顯示PDF成為可能,而這個(gè)過(guò)程并無(wú)需PDF軟件環(huán)境的支持。它可以被當(dāng)做Flex的庫(kù)來(lái)使用。

另外你也可以通過(guò)將一些例如Word、PPT等文檔轉(zhuǎn)成PDF,然后實(shí)現(xiàn)在線(xiàn)瀏覽。

FlexPaper_1.2.4_flash:無(wú)打印功能

FlexPaper_1.4.7_flash:打印功能,右鍵打印

這里我們不需要讓用戶(hù)打印,所以筆者選擇FlexPaper_1.2.4_flash。

FlexPaper項(xiàng)目中有演示demo,這里筆者不多述。

綜上,一個(gè)完整的在線(xiàn)文檔瀏覽方案。

附一:使用iText將jpg、jpeg、png轉(zhuǎn)換為pdf

其他,使用iText將jpg/jpeg/png轉(zhuǎn)換為pdf

  iText是著名的開(kāi)放源碼的站點(diǎn)sourceforge一個(gè)項(xiàng)目,是用于生成PDF文檔的一個(gè)java類(lèi)庫(kù)。通過(guò)iText不僅可以生成 PDF或rtf的文檔,而且可以將XML、Html文件轉(zhuǎn)化為PDF文件。1.在企業(yè)的信息系統(tǒng)中,報(bào)表處理一直占比較重要的作用,iText--一種生 成PDF報(bào)表的Java組件,通過(guò)在服務(wù)器端使用Jsp或JavaBean生成PDF報(bào)表,客戶(hù)端采用超級(jí)連接顯示或下載得到生成的報(bào)表,這樣就很好的解 決了B/S系統(tǒng)的報(bào)表處理問(wèn)題。2.支持文本,表格,圖形的操作,可以方便的跟 Servlet 進(jìn)行結(jié)合。

     繼續(xù)筆者的DocConverter項(xiàng)目。開(kāi)發(fā)環(huán)境是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()    {        //創(chuàng)建一個(gè)文檔對(duì)象         Document doc = new Document();        try {               //定義輸出文件的位置               PdfWriter.getInstance(doc, new FileOutputStream(outputFileString));            //開(kāi)啟文檔               doc.open();               //設(shè)定字體 為的是支持中文               //BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);              // Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);               //向文檔中加入圖片              /*//以下是多圖合成一個(gè)pdf,暫時(shí)用不到            for(int i=1;i<32;i++)            {                //取得圖片~~~圖片格式:                Image jpg1 = Image.getInstance("c:/"+i+".jpg"); //原來(lái)的圖片的路徑                //獲得圖片的高度                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);                //統(tǒng)一按照寬度壓縮                int percent=getPercent2(heigth, width);                //設(shè)置圖片居中顯示                jpg1.setAlignment(Image.MIDDLE);                //直接設(shè)置圖片的大小~~~~~~~第三種解決方案,按固定比例壓縮                //jpg1.scaleAbsolute(210.0f, 297.0f);                //按百分比顯示圖片的比例                jpg1.scalePercent(percent);//表示是原來(lái)圖像的比例;                //可設(shè)置圖像高和寬的比例                //jpg1.scalePercent(50, 100);                doc.add(jpg1);            }            */                        //向文檔中加入圖片              //取得圖片~~~圖片格式:            Image jpg1 = Image.getInstance(inputFileString); //原來(lái)的圖片的路徑            //獲得圖片的高度            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);            //統(tǒng)一按照寬度壓縮            int percent=getPercent2(heigth, width);            //設(shè)置圖片居中顯示            jpg1.setAlignment(Image.MIDDLE);            //直接設(shè)置圖片的大小~~~~~~~第三種解決方案,按固定比例壓縮            //jpg1.scaleAbsolute(210.0f, 297.0f);            //按百分比顯示圖片的比例            jpg1.scalePercent(percent);//表示是原來(lái)圖像的比例;            //可設(shè)置圖像高和寬的比例            //jpg1.scalePercent(50, 100);            doc.add(jpg1);                                    //關(guān)閉文檔并釋放資源               doc.close();           } catch (FileNotFoundException e) {               e.printStackTrace();           } catch (DocumentException e) {               e.printStackTrace();           } catch (IOException e) {               e.printStackTrace();           }      }    /**     * 第一種解決方案     * 在不改變圖片形狀的同時(shí),判斷,如果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;    }    /**     * 第二種解決方案,統(tǒng)一按照寬度壓縮     * 這樣來(lái)的效果是,所有圖片的寬度是相等的,自我認(rèn)為給客戶(hù)的效果是最好的     * @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的測(cè)試結(jié)果。

在jsp中執(zhí)行

新建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>

 

在項(xiàng)目DocConverter根目錄,右鍵屬性 - >Run as - >MyEclipse Server Application

發(fā)布到之前安裝的Tomcat 6.0的根目錄,然后用url路徑訪(fǎng)問(wèn):Http://localhost:8080/DocConverter/MyDOC2PDFTest.jsp 進(jìn)行測(cè)試。

附二:常見(jiàn)問(wèn)題集FAQ

1.txt轉(zhuǎn)換swf,發(fā)生中文亂碼。

txt轉(zhuǎn)換為utf-8編碼,或txt格式手動(dòng)改為odt,上傳就不會(huì)發(fā)生亂碼。從根源上解決,暫時(shí)就算了...暫時(shí)不想去糾結(jié)這些雞毛。

2.加密的pdf可能導(dǎo)致轉(zhuǎn)換為swf失敗。

3.Microsoft Excel在公式運(yùn)算中支持文本型的數(shù)值,而OpenOffice.org Calc不支持

此問(wèn)題暫無(wú)解,請(qǐng)手動(dòng)將Excel中文本型的數(shù)值修改為數(shù)值型的數(shù)值。

4.部分Excel存在過(guò)于豐富的樣式(大部分指沒(méi)有數(shù)據(jù)的單元格也填充了各種樣式),即使用專(zhuān)業(yè)Adobe Acrobat 7(或9) Pro來(lái)進(jìn)行轉(zhuǎn)換,

本來(lái)可能預(yù)計(jì)將產(chǎn)生20-30分頁(yè)的pdf,結(jié)果卻產(chǎn)生800-900分頁(yè)的pdf。此類(lèi)文檔在線(xiàn)轉(zhuǎn)換,難以避免的將導(dǎo)致轉(zhuǎn)換死鎖。

請(qǐng)?jiān)谀愕腅xcel文檔中刪除多余的,毫無(wú)必要的樣式,或者你有更靈活的做法。

5.有些中文PDF文件轉(zhuǎn)換為SWF后,出現(xiàn)亂碼(特別一些專(zhuān)業(yè)期刊)

 1.下載XPDF:xpdf-chinese-simplified.tar.gz

 2.下載字體:gkai00mp.rar

 3.修改xpdf-chinese-simplified目錄下的add-to-xpdfrc文件。將里面的路徑設(shè)為自己的路徑:

#----- 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-GB1C:\xpdf-chinese-simplified\CMap\gkai00mp.ttf
#fontFileCC Adobe-GB1 /usr/..../gkai00mp.ttf
#----- end Chinese Simplified support package

 4.參照上面的代碼,在調(diào)用pdf2swf命令中加入“ -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ”參數(shù)。

PDF2SWFUtil.java

String cmd = exePath + " \""+ fileDir + "\" -o \"" + filePath + "/" +fileName + ".swf\" -T 9 -slanguagedir=c:\\xpdf-chinese-simplified";

這樣亂碼的問(wèn)題就解決了。

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java+FlexPaper+swfTools仿百度文庫(kù)文檔在線(xiàn)預(yù)覽系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
網(wǎng)頁(yè)文檔在線(xiàn)瀏覽(仿百度文庫(kù)設(shè)計(jì))
轉(zhuǎn)換office>>>>pdf>>>>>swf展示(百度文檔)
java實(shí)現(xiàn)doc向swf格式的轉(zhuǎn)換 轉(zhuǎn)
php仿百度文庫(kù),豆丁在線(xiàn)文檔(word,excel,ppt轉(zhuǎn)flash)
仿百度文庫(kù)解決方案(四)——利用JODConverter調(diào)用OpenOffice.org服務(wù)轉(zhuǎn)換文檔為PDF
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服