1. 需要用的軟體
OpenOffice 下載地址http://www.openoffice.org/
JodConverter 下載地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接從附件裡面下載
2.啟動OpenOffice的服務
安裝完openoffice,安裝服務
cd C:\Program Files (x86)\OpenOffice 4\program
執行
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
查看是否安裝成功,查看埠對應的pid
netstat -ano|findstr "8100"
查看pid對應的服務程序名
tasklist|findstr "pid值"
3.將JodConverter相關的jar包添加到項目中
4. 下面是實現代碼
附件裡面有現成的可以用的項目示例,直接導入eclipse就可以運行
[java] view plain
/**
* 將Office文檔轉換為PDF. 運行該函數需要用到OpenOffice, OpenOffice下載地址為
* http://www.openoffice.org/
*
* <pre>
* 方法示例:
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @param sourceFile
* 源文件, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc,
* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
* @param destFile
* 目標文件. 絕對路徑. 示例: F:\\pdf\\dest.pdf
* @return 操作成功與否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置錯誤; 如果返回 0,
* 則表示操作成功; 返回1, 則表示轉換失敗
*/
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();
}
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
return 0;
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
2. 怎麼將word文檔用java讀進資料庫,並且把指定內容分開存入各個欄位中
使用 - T卡!!!可讀取world 、excel、pdf等文件格式!只是取出其中文本
3. 如何用純java代碼實現word轉pdf
幾種方案:
方法一:用apache pio 讀取doc文件,然後轉成html文件用Jsoup格式化html文件,最後用itext將html文件轉成pdf。
方法2:使用jdoctopdf來實現,這是一個封裝好的包,可以把doc轉換成pdf,html,xml等格式,調用很方便
地址:http://www.maxstocker.com/jdoctopdf/downloads.php
需要注意中文字體的寫入問題。
方法3:使用jodconverter來調用openOffice的服務來轉換,openOffice有個各個平台的版本,所以這種方法跟方法1一樣都是跨平台的。
jodconverter的下載地址:http://www.artofsolving.com/opensource/jodconverter
首先要安裝openOffice,下載地址:http://www.openoffice.org/download/index.html
安裝完後要啟動openOffice的服務,具體啟動方法請自行google
方法4:效果最好的一種方法,但是需要window環境,而且速度是最慢的需要安裝msofficeWord以及SaveAsPDFandXPS.exe(word的一個插件,用來把word轉化為pdf)
Office版本是2007,因為SaveAsPDFandXPS是微軟為office2007及以上版本開發的插件
SaveAsPDFandXPS下載地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=7
jacob 包下載地址:http://sourceforge.net/projects/jacob-project/
4. Java 生成Word文檔,是跨平台的,如何實現
跨平台文檔,不如生成HTML文件更好,word也可以打開,其他平台也能打開。
如果切實需要二進制文件,其它平台要打開的話,目前有永中office,openoffice.org,libreoffice。
永中office本身就是JAVA編寫的,在java中可以調用生成word文件。
openoffice.org,libreoffice本身也與java有密切的淵源,跨平台與生成word都沒問題。
你可以使用這些軟體實現你的需求,不用你自己編寫了。
當然,附帶的問題就是你在相關的電腦里必須安裝這些軟體。
5. 用java怎麼將word文檔轉成圖片格式
使用 java 操作 openoffice 實現格式轉換 辛苦了幾天。 不敢獨享成果
首先,要安裝 openoffice (廢話- -)
創建JAVA項目的時候 需要至少以下4個包
juh.jar,jurt.jar,ridl.jar,unoil.jar
以下是路徑
..\OpenOffice.org 2.3\program\classes
我只實現了我要的功能,就是把一種文件格式轉成另一種
Java代碼
package testh;
import java.io.*;
import com.sun.star.uno.UnoRuntime;
public class testcls {
public static void readdoc(String paths, String savepaths)
{
File d = new File(paths);
//取得當前文件夾下所有文件和目錄的列表
File lists[] = d.listFiles();
String pathss = new String("");
//對當前目錄下面所有文件進行檢索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//取得文件類型
filetype = filename.substring((filename.length() - 3), filename.length());
//判斷是否為doc文件
if(filetype.equals("doc"))
{
System.out.println("當前正在檢索....");
//列印當前目錄路徑
System.out.println(paths);
//列印doc文件名
String fname=filename.substring(0, (filename.length() - 4));
System.out.println("檢索到文件"+fname);
try
{
//指定文件路徑和名稱
String path = savepaths+fname+".html";
File outfilename = new File(path);
/** *//**
* 檢查文件是否存在.
* @throws IOException
*
*/
if (!outfilename.exists()) {
System.err.println("目標路徑無同名文件,開始轉換");
System.out.print("正在轉換文件:"+fname);
Dump(paths,fname,savepaths);
}
else
{
System.out.print("文件已存在,放棄創建,請處理存在文件後再運行...\n");
continue;
}
RandomAccessFile mm = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static void Dump(String FilePath,String FileName,String OutPath)
{
com.sun.star.uno.XComponentContext xContext = null;
try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");
// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
com.sun.star.frame.XComponentLoader xCompLoader =
(com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class, oDesktop);
java.io.File sourceFile = new java.io.File(FilePath+FileName+".doc");//讀取的文件
StringBuffer sLoadUrl = new StringBuffer("file:///");
sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
sourceFile = new java.io.File(OutPath+FileName+".html");//輸出的文件
System.out.print(OutPath+" "+FileName);
StringBuffer sSaveUrl = new StringBuffer("file:///");
sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
com.sun.star.beans.PropertyValue[] propertyValue =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Hidden";
propertyValue[0].Value = new Boolean(true);
Object oDocToStore = xCompLoader.loadComponentFromURL(
sLoadUrl.toString(), "_blank", 0, propertyValue );
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );
propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = new Boolean(true);
propertyValue[1] = new com.sun.star.beans.PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "HTML (StarWriter)";//你一定發現了,把這里改成其他參數,可以保存為不同的文件 MS Word 97,writer_pdf_Export
xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
sSaveUrl + "\"\n");
com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
oDocToStore );
if (xCloseable != null ) {
xCloseable.close(false);
} else
{
com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, oDocToStore );
xComp.dispose();
}
System.out.println("document closed!");
}
catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
}
public static void main(String args[]) throws Exception
{
String paths = new String("c:\\a\\");
String savepaths = new String ("f:\\");
readdoc(paths,savepaths);
}
}
6. 如何用純java代碼實現word轉pdf
1:用apache pio 讀取doc文件,然後轉成html文件用Jsoup格式化html文件,最後用itext將html文件轉成pdf。
2:使用jdoctopdf來實現,這是一個封裝好的包,可以把doc轉換成pdf,html,xml等格式,調用很方便。
3:地址http://www.maxstocker.com/jdoctopdf/downloads.php
需要注意中文字體的寫入問題。
4:使用jodconverter來調用openOffice的服務來轉換,openOffice有個各個平台的版本,所以這種方法跟方法1一樣都是跨平台的。
jodconverter的下載地址:http://www.artofsolving.com/opensource/jodconverter
首先要安裝openOffice,下載地址:office.org/download/index.html" target="_blank">http://www.openoffice.org/download/index.html
5:安裝完後要啟動openOffice的服務,具體啟動方法請自行google。
6:效果最好的一種方法,但是需要window環境,而且速度是最慢的需要安裝msofficeWord以及SaveAsPDFandXPS.exe(word的一個插件,用來把word轉化為pdf)
7:Office版本是2007,因為SaveAsPDFandXPS是微軟為office2007及以上版本開發的插件。
8:SaveAsPDFandXPS下載地址:microsoft.com/zh-cn/download/details.aspx?id=7" target="_blank">http://www.microsoft.com/zh-cn/download/details.aspx?id=7。
9:需要轉換的工具 ,看你是linux還是word 。word還好不需要安裝。linux就麻煩了。
7. Java怎麼操作OpenOffice創建word文檔並向其設置內容
OpenOffice java api:
簡單的說就是利用java程序可以操作OpenOffice的所有功能,比如創建doc文檔,插入文字,設置文字格式等等。
1. OpenOffice 給程序員提供了一個叫UNO (UniversalNetwork Objects)的組件技術.我理解的UNO: OpenOffice 類似於web程序中的伺服器,程序員寫的代碼類似於客戶端,利用UNO提供的介面和服務去完成對OpenOffice文檔的操作。所以寫程序首先要搭建 UNO環境:
1. 下載 OpenOffice
2.復制UNO提供的jar包: unoil.jar, java_uno.jar, juh.jar, jurt.jar, ridl.jar, unoloader.jar. (ps: 安裝了SDK之後在文件夾找)到自己的工程中,引入它們。
3. 下載文檔:DevelopersGuide.pdf.
4. 安裝了SDK後,重新啟動一下機器,然後就可以按照 DevelopersGuide 來學習 UNO 編程了。
5. 需要ava 環境。
補充: 安裝了SDK後, java, c++幫助文檔,樣常式序,其他關於sdk的信息 都放在本地openOffice安裝路徑一個叫sdk目錄下面,enjoy it !
總結一下已經實現的功能和碰到的問題匯總:
1. 首先要得到遠程office組件的上下文.通過:
com.sun.star.uno.XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
得到,如果OpenOffice安裝路徑不是在工程的路徑下面(我自己猜的), 就會報:
com.sun.star.comp.helper.BootstrapException: no office executable found!
解決辦法: 黑其源代碼, 看了源代碼就會發現其實OpenOffice是在尋找本地的soffice的shell文件,所以弄個變數來保存soffice在系統中的路徑,重新寫一 個Bootstrap就可以了。詳細請參照:論壇 。
2. 得到 XMultiComponentFactory (ComponentFactory 工廠)
com.sun.star.lang.XMultiComponentFactory xMCF = xContext.getServiceManager();
3. 得到各種組件可以通過下面代碼:
// docType 是 與 soffice 同目錄下面的OpenOffice的其他shell文件,swrite等等
protected XComponent newDocComponent(String docType)
throws java.lang.Exception {
String loadUrl = "private:factory/" + docType;
mxRemoteServiceManager = this.getRemoteServiceManager();
Object desktop = mxRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", mxRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime
.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] loadProps = new PropertyValue[0];
return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0,
loadProps);
}
4.得到 XTextDocument
XComponent xEmptyWriterComponent = newDocComponent("swriter");
XTextDocument mxDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,
xEmptyWriterComponent);
5. 得到一個文檔的引用
XText mxDocText = mxDoc.getText();
6. 得到文檔的屬性列表
XPropertySet mxDocProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, mxDoc);
7. 建立游標,用來插入新的內容。
XTextCursor mxDocCursor = mxDocText.createTextCursor();
XSentenceCursor xSentenceCursor = (XSentenceCursor) UnoRuntime
.queryInterface(XSentenceCursor.class, mxDocCursor);
XWordCursor xWordCursor = (XWordCursor) UnoRuntime.queryInterface(
XWordCursor.class, mxDocCursor);
8.得到游標屬性列表
XPropertySet xCursorProps = (XPropertySet) UnoRuntime .queryInterface(XPropertySet.class, mxDocCursor);
9.設置插入文字格式
xCursorProps.setPropertyValue("CharFontName", "宋體");
xCursorProps.setPropertyValue("CharWeight", new Float(FontWeight.BOLD));
xCursorProps.setPropertyValue("CharHeight", new Float(10.5));
// 居中顯示
xCursorProps.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER);
10.在該游標處插入信息
mxDocText.insertString(xSentenceCursor, 「Hello World", true);
11. 保存的關鍵代碼
protected void storeDocComponent(XComponent xDoc, String storeUrl)
throws java.lang.Exception {
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
XStorable.class, xDoc);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "MS Word 97";
openOfficeJavaLogger.debug("... store \"" + xDoc.toString() + "\" to \"" + storeUrl
+ "\".");
xStorable.storeAsURL(storeUrl, storeProps);
8. 如何利用Java操作Word,Excel,PDF文檔
office類的文檔可以參考appache的poi,java一般都使用這個包來操作office文檔。office文檔與office文檔的轉化參考openoffice的UNO對象,功能相當強大,可惜的是openoffice沒有64位的,2016這種office格式的文檔支持不行。想要直接操作pdf,上adobe的網上找去,我有個icepdf可以把pdf轉圖片,這個包我記得也可以操作pdf文檔,包括生成pdf,修改pdf內容等等。