リーダー:
配布プロセス中、JavaはオフィスシリーズのドキュメントをPDFに変換するためによく使用され、通常、Microsoftが提供するOpenOffice+JodConverterを使用してドキュメントを変換します。
OpenOfficeには、WindowsバージョンとLinuxバージョンの両方があります。生産環境がLinuxシステムであることを心配しないでください。
1。OpenOfficeはJARに依存します。例としてMavenを取ります。
<Dependency> GroupId> com.ArtofSolving </groupId> <artifactid> jodconverter </artifactid> <version> 2.2.1 </version> </dependency> <dependency> openoffice </groupid> <artifactid> jurt </artifactid> <groupid> org.openoffice </groupid> <artifactid> ridl </artifactid> <version> 3.0.1 </version> </dependency> <dependency> org.openoffice </groupid> <artifactid> juh </artifactid> <バージョン> <artifactid> unoil </artifactid> <バージョン> 3.0.1 </version> </dependency> <! - jodconverter2.2.1はslf4j-jdk14に依存する必要があります。それは非常に低い問題です - > <依存関係> groupId> org.slf4j </groupid> <artifactid> slf4j-jdk14 </artifactid> <バージョン> <バージョン> </version> </dependency>
2。コードを直接アップロードするには、OpenOfficeアプリケーションの8100ポートを聞く必要があります。
public void convert(file sourcefile、file targetfile){try {// 1:接続を開きますopenofficeconnection connection = new socketopenofficeconnection(8100); connection.connect(); documentConverter converter = new OpenOfficeDocumentConverter(connection); // 2:Format documentFormatregistry Factory = new BasicDocumentFormatregistry()を取得します。 documentFormat inputDocumentFormat = factory .getFormatbyFileExtension(getExtensionName(sourcefile.getabsolutepath())); documentFormat outputDocumentFormat = factory .getFormatbyFileExtension(getExtensionName(targetFile.getAbsolutepath())); // 3:変換converter.convertを実行します(sourcefile、inputdocumentformat、targetfile、outputdocumentformat); } catch(connectexception e){log.info( "ドキュメント変換障害"); }}3.注:JodConverterは、2007年バージョンの後にXXX.DOCXドキュメントを変換した後にエラーを報告します。 03サフィックスがxxx.docであることを誰もが知っています。 07バージョンはxxx.docxになります。
JodConverterソースコードを確認すると、documentFormatはXXX.DOCX形式をサポートしていないことがわかります。
BasicDocumentFormatregistryクラスソースコード
// JodConverter -Java OpenDocument Converter // Copyright(c)2004-2007 -Mirko nasato <[email protected]> //このライブラリはフリーソフトウェアです。 Free Software Foundationによって公開されているGNU Lesser general Public //ライセンスの条件の下で、それを再配布し、// //変更できます。 //ライセンスのバージョン2.1、または(オプションで)後のバージョンのいずれか。 // //このライブラリは、それが有用であることを期待して配布されます。 //商品性または特定の目的に対するフィットネスの暗黙の保証さえありません。詳細については、GNU //より少ない一般公開ライセンスを参照してください。 // http://www.gnu.org/copyleft/lesser.html //パッケージcom.artofsolving.jodconverter; java.util.arraylistをインポートします。 java.util.iteratorをインポートします。 java.util.listをインポートします。パブリッククラスBasicDocumentFormatregistryはdocumentformatregistry {private list/*<documentformat>*/ documentformats = new ArrayList(); public void adddocumentformat(documentformat documentformat){documentformats.add(documentformat); } protected list/*<documentformat>*/ getDocumentFormats(){return documentformats; } / ** * @param拡張機能ファイル拡張機能 * @ @returnこの拡張子のdocumentformat、または拡張機能がマッピングされていない場合 * / public documentformat getFormatbyFileExtension(string extension){if(extension == null){return null; } string lowerextension = extension.tolowercase(); for(iterator it = documentformats.iterator(); it.hasnext();){documentFormat =(documentFormat)it.next(); if(format.getFileExtension()。equals(lowerextension)){return format; }} nullを返します。 } public documentFormat getFormatByMimeType(String MimeType){for(iterator it = documentformats.iterator(); it.hasnext();){documentformat format =(documentformat)it.next(); if(format.getMimeType()。equals(mimetype)){return format; }} nullを返します。 }}
デフォルトの実装クラスでサポートされているファイル形式は、次のとおりです
// JodConverter -Java OpenDocument Converter // Copyright(c)2004-2007 -Mirko nasato <[email protected]> //このライブラリはフリーソフトウェアです。 Free Software Foundationによって公開されているGNU Lesser general Public //ライセンスの条件の下で、それを再配布し、// //変更できます。 //ライセンスのバージョン2.1、または(オプションで)後のバージョンのいずれか。 // //このライブラリは、それが有用であることを期待して配布されます。 //商品性または特定の目的に対するフィットネスの暗黙の保証さえありません。詳細については、GNU //より少ない一般公開ライセンスを参照してください。 // http://www.gnu.org/copyleft/lesser.html //パッケージcom.artofsolving.jodconverter; public class defaultdocumentformatregistry extends basicdocumentformatregistry {public defaultdocumentformatregistry(){final documentformat pdf = new DocumentFormat( "Portable Document Format"、 "Application/PDF"、 "PDF"); pdf.setexportfilter(documentfamily.drawing、 "draw_pdf_export"); pdf.setexportfilter(documentfamily.presentation、 "impress_pdf_export"); pdf.setexportfilter(documentfamily.spreadsheet、 "calc_pdf_export"); pdf.setexportfilter(documentfamily.text、 "writer_pdf_export"); adddocumentformat(pdf); final documentformat swf = new DocumentFormat( "Macromedia Flash"、 "Application/X-Shockwave-flash"、 "SWF"); swf.setexportfilter(documentfamily.drawing、 "draw_flash_export"); swf.setexportfilter(documentfamily.presentation、 "impress_flash_export"); adddocumentformat(swf); final documentformat xhtml = new DocumentFormat( "xhtml"、 "application/xhtml+xml"、 "xhtml"); xhtml.setexportfilter(documentfamily.presentation、 "xhtml Impress file"); xhtml.setexportfilter(documentfamily.spreadsheet、 "xhtml calc file"); xhtml.setexportfilter(documentfamily.text、 "xhtml writer file"); adddocumentformat(xhtml); // htmlは入力として提供されるとテキストとして扱われますが、出力として//スプレッドシートとプレゼンテーションの形式のエクスポートにも利用できますfinal documentformat html = new documentformat( "html"、documentfamily.text、 "text/html"、 "html"); html.setexportfilter(documentfamily.presentation、 "impress_html_export"); html.setexportfilter(documentfamily.spreadsheet、 "html(starcalc)"); html.setexportfilter(documentfamily.text、 "html(starwriter)"); adddocumentformat(html); final documentformat odt = new DocumentFormat( "opendocument Text"、documentfamily.text、 "application/vnd.oasis.opendocument.text"、 "odt"); odt.setexportfilter(documentfamily.text、 "writer8"); adddocumentformat(odt); final documentformat sxw = new DocumentFormat( "OpenOffice.org 1.0 Text Document"、DocumentFamily.Text、 "Application/vnd.sun.xml.writer"、 "sxw"); sxw.setexportfilter(documentfamily.text、 "staroffice xml(writer)"); adddocumentformat(sxw); final documentformat doc = new DocumentFormat( "Microsoft Word"、DocumentFamily.Text、 "Application/MSWORD"、 "doc"); doc.setexportfilter(documentfamily.text、 "ms word 97"); adddocumentformat(doc); final documentformat rtf = new DocumentFormat( "Rich Text Format"、DocumentFamily.Text、 "Text/RTF"、 "RTF"); rtf.setexportfilter(documentfamily.text、 "Rich Text Format"); adddocumentformat(rtf); final documentformat wpd = new DocumentFormat( "wordperfect"、documentfamily.text、 "application/wordperfect"、 "wpd"); adddocumentformat(wpd); final documentformat txt = new DocumentFormat( "Plain Text"、documentfamily.text、 "text/plain"、 "txt"); //「テキスト」に「テキスト」に設定して、OOOが「ASCIIフィルターオプション」ダイアログを表示しないようにしないようにします。 txt.setexportfilter(documentfamily.text、 "text"); adddocumentformat(txt); final documentformat wikitext = new DocumentFormat( "mediawiki wikitext"、 "text/x-wiki"、 "wiki"); wikitext.setexportfilter(documentfamily.text、 "mediawiki"); adddocumentformat(wikitext); final documentformat ods = new DocumentFormat( "OpenDocument Spreadsheet"、documentFamily.spreadsheet、 "application/vnd.oasis.opendocument.spreadsheet"、 "ods"); ods.setexportfilter(documentfamily.spreadsheet、 "calc8"); addDocumentFormat(ODS); final documentformat sxc = new DocumentFormat( "openoffice.org 1.0スプレッドシート"、documentfamily.spreadsheet、 "application/vnd.sun.xml.calc"、 "sxc"); sxc.setexportfilter(documentfamily.spreadsheet、 "Staroffice XML(calc)"); adddocumentformat(sxc); final documentformat xls = new DocumentFormat( "Microsoft Excel"、documentFamily.spreadsheet、 "application/vnd.ms-excel"、 "xls"); xls.setexportfilter(documentfamily.spreadsheet、 "ms excel 97"); adddocumentformat(xls); final documentformat csv = new DocumentFormat( "csv"、documentfamily.spreadsheet、 "text/csv"、 "csv"); csv.setimportoption( "filtername"、 "text -txt -csv(starcalc)"); csv.setimportoption( "filteroptions"、 "44,34,0"); //フィールドセパレーター: '、';テキストデリミター: '"' csv.setexportfilter(documentfamily.spreadsheet、" text -txt -csv(starcalc) "); csv.setexportoption(documentfamily.spreadsheet、" filteroptions "、" 44,34,0 "); documentformat(documentfamily.spreadsheet、「table-spected-values」、「tsv」);テキストデリミター: '"' tsv.setexportfilter(documentfamily.spreadsheet、" text -txt -csv(starcalc) "); tsv.setexportoption(documentfamily.spreadsheet、 "filteroptions"、 "9,34,0"); adddocumentformat(TSV); final documentformat odp = new DocumentFormat( "OpenDocument Presention"、documentfamily.presentation、 "application/vnd.oasis.opendocument.presentation"、 "odp"); odp.setexportfilter(documentfamily.presentation、 "Impress8"); addDocumentFormat(ODP); final documentformat sxi = new DocumentFormat( "openoffice.org 1.0プレゼンテーション"、documentfamily.presentation、 "application/vnd.sun.xml.impress"、 "sxi"); sxi.setexportfilter(documentfamily.presentation、 "Staroffice XML(Impress)"); adddocumentformat(sxi); final documentformat ppt = new DocumentFormat( "Microsoft PowerPoint"、documentFamily.presentation、 "application/vnd.ms-powerpoint"、 "ppt"); ppt.setexportfilter(documentfamily.presentation、 "ms powerpoint 97"); adddocumentformat(ppt); final documentformat odg = new DocumentFormat( "OpenDocument Drawing"、documentFamily.drawing、 "application/vnd.oasis.opendocument.graphics"、 "odg"); odg.setexportfilter(documentfamily.drawing、 "draw8"); addDocumentFormat(ODG); final documentformat svg = new DocumentFormat( "Scalable Vector Graphics"、 "image/svg+xml"、 "svg"); svg.setexportfilter(documentfamily.drawing、 "draw_svg_export"); adddocumentformat(svg); }}
解決策:basicdocumentformatregistryクラスで公開文書Format getFormatByFileExtension(String Extension)メソッドを書き換えます。接尾辞名にdocが含まれている限り、docのdocumentformat形式を使用します
// JodConverter -Java OpenDocument Converter // Copyright(c)2004-2007 -Mirko nasato <[email protected]> //このライブラリはフリーソフトウェアです。 Free Software Foundationによって公開されているGNU Lesser general Public //ライセンスの条件の下で、それを再配布し、// //変更できます。 //ライセンスのバージョン2.1、または(オプションで)後のバージョンのいずれか。 // //このライブラリは、それが有用であることを期待して配布されます。 //商品性または特定の目的に対するフィットネスの暗黙の保証さえありません。詳細については、GNU //より少ない一般公開ライセンスを参照してください。 // http://www.gnu.org/copyleft/lesser.html //パッケージcom.artofsolving.jodconverter; java.util.arraylistをインポートします。 java.util.iteratorをインポートします。 java.util.listをインポートします。 / *** basicdocumentformatregistry document format* @author guguangjun*/ public class basicdocumentformatregistry documentformatregistry {private list/* <documentformat>*/ documentformats = new arrayList(); public void adddocumentformat(documentformat documentformat){documentformats.add(documentformat); } protected list/ * <documentformat> */ getDocumentFormats(){return documentformats; } / ** * @param拡張機能 *ファイル拡張機能 * @ @returnこの拡張子のdocumentformat、または拡張機能 *がマッピングされていない場合 * / public documentformat getFormatbyFileExtension(String拡張子){if(extension == null){return null; } // unifiedファイル名接尾辞if(endix.indexof( "doc")> = 0){extension = "doc"; } if(extension.indexof( "ppt")> = 0){extension = "ppt"; } if(extension.indexof( "xls")> = 0){extension = "xls"; } string lowerextension = extension.tolowercase(); for(iterator it = documentformats.iterator(); it.hasnext();){documentFormat =(documentFormat)it.next(); if(format.getFileExtension()。equals(lowerextension)){return format; }} nullを返します。 } public documentFormat getFormatByMimeType(String MimeType){for(iterator it = documentformats.iterator(); it.hasnext();){documentformat format =(documentformat)it.next(); if(format.getMimeType()。equals(mimetype)){return format; }} nullを返します。 }}
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。