激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java調用openoffice將office系列文檔轉換為PDF的示例方法

java調用openoffice將office系列文檔轉換為PDF的示例方法

2021-02-23 11:27make_a_difference Java教程

本篇文章主要介紹了java使用openoffice將office系列文檔轉換為PDF的示例方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

前導:

發過程中經常會使用javaoffice系列文檔轉換為PDF, 一般都使用微軟提供的openoffice+jodconverter 實現轉換文檔。

openoffice既有windows版本也有linux版。不用擔心生產環境是linux系統。

1、openoffice依賴jar,以maven為例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<dependency>
      <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>jurt</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>ridl</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>juh</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>unoil</artifactId>
      <version>3.0.1</version>
    </dependency>
 
    <!--jodconverter2.2.1必須依賴slf4j-jdk14必須這個版本,不然源碼中日志會報錯,很low的一個問題-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.4.3</version>
    </dependency>

2、直接上轉換代碼,需要監聽openoffice應用程序8100端口即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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("文檔轉換PDF失敗");
  }
}

3、需注意:jodconverter 在轉換2007版本以后的xxx.docx文檔會報錯,原因大家都明03后綴名xxx.doc  07以后版本xxx.docx

查看jodconverter源碼發現documentFormat不支持xxx.docx格式BasicDocumentFormatRegistry中public DocumentFormat getFormatByFileExtension(String extension)默認支持是使用doc格式

BasicDocumentFormatRegistry類源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// http://www.gnu.org/copyleft/lesser.html
//
package com.artofsolving.jodconverter;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
 
  private List/*<DocumentFormat>*/ documentFormats = new ArrayList();
 
  public void addDocumentFormat(DocumentFormat documentFormat) {
    documentFormats.add(documentFormat);
  }
 
  protected List/*<DocumentFormat>*/ getDocumentFormats() {
    return documentFormats;
  }
 
  /**
   * @param extension the file extension
   * @return the DocumentFormat for this extension, or null if the extension is not mapped
   */
  public DocumentFormat getFormatByFileExtension(String extension) {
    if (extension == null) {
      return null;
    }
    String lowerExtension = extension.toLowerCase();
    for (Iterator it = documentFormats.iterator(); it.hasNext();) {
      DocumentFormat format = (DocumentFormat) it.next();   
      if (format.getFileExtension().equals(lowerExtension)) {
        return format;
      }
    }
    return 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;
      }
    }
    return null;
  }
}

BasicDocumentFormatRegistry的默認實現類DefaultDocumentFormatRegistry  中支持的文件格式如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// http://www.gnu.org/copyleft/lesser.html
//
package 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 is treated as Text when supplied as input, but as an output it is also
    // available for exporting Spreadsheet and Presentation formats
    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");
    // set FilterName to "Text" to prevent OOo from tryign to display the "ASCII Filter Options" dialog
    // alternatively FilterName could be "Text (encoded)" and FilterOptions used to set encoding if needed
    txt.setImportOption("FilterName", "Text");
    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 Spreadsheet", 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"); // Field Separator: ','; Text Delimiter: '"' 
    csv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
    csv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "44,34,0");
    addDocumentFormat(csv);
 
    final DocumentFormat tsv = new DocumentFormat("Tab-separated Values", DocumentFamily.SPREADSHEET, "text/tab-separated-values", "tsv");
    tsv.setImportOption("FilterName", "Text - txt - csv (StarCalc)");
    tsv.setImportOption("FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"'
    tsv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
    tsv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "9,34,0");
    addDocumentFormat(tsv);
 
    final DocumentFormat odp = new DocumentFormat("OpenDocument Presentation", DocumentFamily.PRESENTATION, "application/vnd.oasis.opendocument.presentation", "odp");
    odp.setExportFilter(DocumentFamily.PRESENTATION, "impress8");
    addDocumentFormat(odp);
 
    final DocumentFormat sxi = new DocumentFormat("OpenOffice.org 1.0 Presentation", 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類中public DocumentFormat getFormatByFileExtension(String extension)方法,只要是后綴名包含doc則使用doc的documentFormat文檔格式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// http://www.gnu.org/copyleft/lesser.html
//
package com.artofsolving.jodconverter;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/**
 * 重寫 BasicDocumentFormatRegistry 文檔格式
 * @author HuGuangJun
 */
public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
 
  private List/* <DocumentFormat> */ documentFormats = new ArrayList();
 
  public void addDocumentFormat(DocumentFormat documentFormat) {
    documentFormats.add(documentFormat);
  }
 
  protected List/* <DocumentFormat> */ getDocumentFormats() {
    return documentFormats;
  }
 
  /**
   * @param extension
   *      the file extension
   * @return the DocumentFormat for this extension, or null if the extension
   *     is not mapped
   */
  public DocumentFormat getFormatByFileExtension(String extension) {
    if (extension == null) {
      return null;
    }
    //將文件名后綴統一轉化
    if (extension.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 format = (DocumentFormat) it.next();
      if (format.getFileExtension().equals(lowerExtension)) {
        return format;
      }
    }
    return 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;
      }
    }
    return null;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/make_a_difference/article/details/53771136

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品日产高清版的功能介绍 | 久久久久久久网站 | 国产一级一国产一级毛片 | 久久精品一区二区三区不卡牛牛 | 成人毛片一区二区三区 | 欧美日韩精品一区二区三区蜜桃 | 国产一区二区三区视频在线观看 | 性片久久| av免费在线网站 | 欧美成人免费在线视频 | 主人在调教室性调教女仆游戏 | av日韩一区二区三区 | 国产亚洲精品久久777777 | 国产精品hd免费观看 | 亚洲va久久久噜噜噜久久男同 | 久久久国产精品免费观看 | 伊人亚洲精品 | 成年免费视频黄网站在线观看 | 无码专区aaaaaa免费视频 | 国产精品18久久久久久久 | 久久3| 国产女厕所| 99ri在线| 欧美日韩一区三区 | 亚洲精品欧美二区三区中文字幕 | 激情大乳女做爰办公室韩国 | 亚洲精品午夜国产va久久成人 | 国产一级毛片视频在线! | 91中文在线| 黄色免费av| 欧美日本91精品久久久久 | 超级av在线 | 亚洲电影免费观看国语版 | 欧美福利视频一区二区三区 | 精品国产一区二区在线观看 | 成人毛片视频免费看 | 国产精品成aⅴ人片在线观看 | 99在线在线视频免费视频观看 | 国产成人精品一区二区视频免费 | 97人操| 中文字幕在线第二页 |