지도자:
배포 과정에서 Java는 종종 Office Series 문서를 PDF로 변환하는 데 사용되며 일반적으로 Microsoft가 제공하는 OpenOffice+JodConverter를 사용하여 문서를 변환합니다.
OpenOffice에는 Windows 버전과 Linux 버전이 모두 있습니다. 생산 환경이 Linux 시스템이라는 것에 대해 걱정하지 마십시오.
1. OpenOffice는 Jar에 의존합니다. Maven을 예로 들어 보겠습니다.
<pectionency> <groupId> com.artofSolving </groupId> <artifactid> jodconverter </artifactid> <bersion> 2.2.1 </version> </fectionency> <pectionement> <groupId> org.openOffice </groupId> <atifactid> jurt </artifactId> </version> </dependent> 3.0.1> <groupid> org.openoffice </groupid> <artifactid> ridl </artifactid> <bersion> 3.0.1 </version> </fectionency> <pectionency> <groupid> org.openOffice </groupId> <artifactid> juh </artifactid> <버전> 3.0.1 </delation> <artifactid> unoil </artifactid> <bersion> 3.0.1 </version> </fectionency> <!-JodConverter2.2.1은 SLF4J-JDK14에 의존해야합니다. 그렇지 않으면 로그가 소스 코드에 오류를보고합니다. 매우 낮은 문제입니다-> <pectomency> <groupid> org.slf4j </groupid> <artifactid> slf4j-jdk14 </artifactid> <bersion> 1.4.3 </version> </dependency>
2. 코드를 직접 업로드하려면 OpenOffice 응용 프로그램의 8100 포트를 들어야합니다.
public void convert (파일 소스 파일, 파일 targetfile) {try {// 1 : 연결 열기 OpenOfficeConnection Connection = New SocketOpenOfficeCeConception (8100); Connection.connect (); DocumentConverter Converter = New OpenOfficedOcumentConverter (Connection); // 2 : 형식 GET DOCODIONFORMATREGISTRY FACTORY = NEW BASICDOCUMENTOMPORMATREGISTRY (); DocumentFormat inputDocumentOrmat = factory .getFormatByFileExtension (getExtensionName (sourceFile.getAbsolutePath ()); DocumentFormat outputDocumentOrmat = factory .getFormatByFileExtension (getExtensionName (targetFile.GetAbSolutePath ()); // 3 : 변환 변환기를 실행합니다 .convert (sourceFile, inputDocumentFormat, targetFile, outputDocumentOrmat); } catch (connectexception e) {log.info ( "문서 변환 실패"); }}3. 참고 : JodConverter는 2007 년 버전 이후 XXX.docx 문서를 변환 한 후 오류를보고합니다. 모두 03 접미사가 xxx.doc라는 것을 알고 있습니다. 07 버전은 xxx.docx입니다.
jodconverter 소스 코드를 확인하면 DocumentFormat이 xxx.docx 형식을 지원하지 않는 것으로 나타났습니다. basicdocumentformatregistry public documentformat getformatbyfileextension (String Extension) 기본 지원은 doc 형식을 사용하는 것입니다.
BasicDocumentOrmatregistry 클래스 소스 코드
// JodConverter -Java OpenDocument Converter // Copyright (C) 2004-2007 -Mirko Nasato <[email protected]> // //이 라이브러리는 무료 소프트웨어입니다. Free Software Foundation에서 게시 한대로 GNU Lesser General Public // 라이센스의 조건에 따라이를 재분배 및 // 수정할 수 있습니다. 라이센스의 // 버전 2.1 또는 이후 버전의 버전 2.1. // //이 라이브러리는 유용하기를 희망하여 배포되지만 // 보증이 없습니다. // 상인성 또는 특정 목적에 대한 적합성에 대한 묵시적 보증조차 없습니다. 자세한 내용은 gnu // Lesser General Public License를 참조하십시오. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; java.util.arraylist 가져 오기; import java.util.iterator; Java.util.list 가져 오기; public class basicdocumentormatregistry는 d public void addDocumentOrmat (DocumentFormat DocumentFormat) {documentformats.add (documentformat); } 보호 된 목록/*<DocumentFormat>*/ getDocumentOrmats () {return DocumentFormats; } / ** * @param 확장자 파일 확장자 * @ @return이 확장자의 문서 별을 return 또는 확장자가 매핑되지 않은 경우 null * / public documentformat getFormatByFileExtension (String Extension) {if (extension == null) {return null; } string lowerextension = extension.tolowerCase (); for (iterator it = documb if (format.getFileExtension (). Equals (lowEREXTENSION)) {return format; }} return null; } public d if (format.getMimeType (). Equals (Mimetype)) {return format; }} return null; }}
기본 구현 클래스에서 지원되는 파일 형식 DefaultDocumentFormatRegistry는 다음과 같습니다.
// JodConverter -Java OpenDocument Converter // Copyright (C) 2004-2007 -Mirko Nasato <[email protected]> // //이 라이브러리는 무료 소프트웨어입니다. Free Software Foundation에서 게시 한대로 GNU Lesser General Public // 라이센스의 조건에 따라이를 재분배 및 // 수정할 수 있습니다. 라이센스의 // 버전 2.1 또는 이후 버전의 버전 2.1. // //이 라이브러리는 유용하기를 희망하여 배포되지만 // 보증이 없습니다. // 상인성 또는 특정 목적에 대한 적합성에 대한 묵시적 보증조차 없습니다. 자세한 내용은 gnu // Lesser General Public License를 참조하십시오. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; 공개 클래스 기본 도문 소포를 확장합니다. 기본 도문 소식 argregistry {public defaultDocumentOrmatregistry () {Final DocumentFormat pdf = new DocumentFormat ( "Portable Document Format", "Application/PDF", "PDF"); pdf.setexportfilter (documentfamily.rawing, "draw_pdf_export"); pdf.setexportfilter (documentfamily.presentation, "inffess_pdf_export"); pdf.setexportfilter (documentfamily.spreadsheet, "calc_pdf_export"); pdf.setexportfilter (documentfamily.text, "writer_pdf_export"); addDocumentOrmat (PDF); 최종 DocumentFormat SWF = New DocumentFormat ( "Macromedia Flash", "Application/X-Shockwave-Flash", "SWF"); swf.setexportfilter (documentfamily.rawing, "draw_flash_export"); swf.setexportfilter (documentfamily.presentation, "inffess_flash_export"); addDocumentOrmat (SWF); 최종 DocumentFormat XHTML = New Docum xhtml.setexportfilter (documentfamily.presentation, "xhtml inffress file"); xhtml.setexportfilter (documentfamily.spreadsheet, "xhtml calc 파일"); xhtml.setexportfilter (documentfamily.text, "xhtml Writer File"); addDocumentOrmat (xhtml); // html은 입력으로 제공 될 때 텍스트로 취급되지만 출력으로서 // 스프레드 시트를 내보낼 수 있습니다. 최종 DocumentFormat html = new DocumentFormat ( "html", documentfamily.text, "text/html", "html"); html.setexportfilter (documentfamily.presentation, "inffess_html_export"); html.setexportfilter (documentfamily.spreadsheet, "html (starcalc)"); html.setexportfilter (documentfamily.text, "html (starwriter)"); addDocumentOrmat (html); 최종 DocumentFormat ODT = New DocumentFormat ( "OpenDocument Text", DocumentFamily.Text, "application/vnd.oasis.opendocument.text", "Odt"); odt.setexportfilter (documentfamily.text, "writer8"); AddDocumentFormat (ODT); 최종 DocumentFormat SXW = New DocumentFormat ( "OpenOffice.org 1.0 텍스트 문서", DocumentFamily.Text, "application/vnd.sun.xml.writer", "SXW"); sxw.setexportfilter (DocumentFamily.Text, "StarOffice XML (Writer)"); addDocumentFormat (sxw); 최종 DocumentFormat doc = new DocumentFormat ( "Microsoft Word", DocumentFamily.Text, "Application/Msword", "Doc"); Doc.SetexportFilter (DocumentFamily.Text, "MS Word 97"); AddDocumentOrmat (Doc); 최종 DocumentFormat rtf = new DocumentFormat ( "Rich Text Format", DocumentFamily.Text, "Text/RTF", "RTF"); rtf.setexportfilter (DocumentFamily.Text, "Rich Text Format"); addDocumentOrmat (RTF); 최종 DocumentFormat wpd = 새로운 docum addDocumentOrmat (WPD); 최종 DocumentFormat txt = new DocumentFormat ( "일반 텍스트", DocumentFamily.Text, "Text/Plain", "TXT"); // OOO가 "ASCII 필터 옵션"을 표시하는 것을 방지하기 위해 Filtername을 "Text"로 설정하여 "Text (Encoded)"가 될 수 있으며 필요한 경우 인코딩을 설정하는 데 사용되는 필터 로프가 필요할 수 있습니다. txt.setexportfilter (documentfamily.text, "text"); addDocumentFormat (txt); 최종 DocumentFormat wikitext = 새로운 docum wikitext.setexportfilter (documentfamily.text, "mediawiki"); addDocumentOrmat (wikitext); 최종 DocumentFormat ODS = 새로운 DocumentFormat ( "OpenDocument Spreadsheet", DocumentFamily.SpreadSheet, "Application/VND.OASIS.OPENDOCUMENT.SPREADSHEET", "ODS"); ods.setexportfilter (documentfamily.spreadsheet, "calc8"); addDocumentOrmat (ODS); 최종 DocumentFormat SXC = 새로운 Docum sxc.setexportfilter (documentfamily.spreadsheet, "Staroffice xml (calc)"); addDocumentOrmat (sxc); 최종 DocumentFormat XLS = New DocumentFormat ( "Microsoft Excel", DocumentFamily.SpreadSheet, "Application/VND.MS-EXCEL", "XLS"); xls.setexportfilter (DocumentFamily.spreadsheet, "Ms Excel 97"); addDocumentFormat (XLS); 최종 DocumentFormat CSV = New DocumentFormat ( "CSV", DocumentFamily.SpreadSheet, "Text/CSV", "CSV"); csv.setimportoption ( "filtername", "text -txt -csv (starcalc)"); csv.setimportoption ( "Filteroptions", "44,34,0"); // 필드 분리기 : ','; 텍스트 Delimiter : ''CSV.SetexportFilter (DocumentFamily.SpreadSheet, "Text -TXT -CSV (StarCALC)"; CSV.SetexportOption (DocumentFamily.spreadSheet, "FilterOptions", "44,34,0"); 최종 DocumentFormat (CSV); DocumentFARMAT (TAB-SEPARATED-SPREADSHET), "TSV Delimiter : ' "'TSV.SetexportFilter (DocumentFamily.SpreadSheet,"Text -TXT -CSV (StarCalc) "); tsv.setexportoption (documentfamily.spreadsheet, "filteroptions", "9,34,0"); addDocumentFormat (TSV); 최종 DocumentFormat ODP = New DocumentFormat ( "OpenDocument Presentation", DocumentFamily.Presentation, "application/vnd.oasis.opendocument.presentation", "ODP"); ODP.SETEXPORTFILTER (DocumentFamily.Presentation, "inffress8"); AddDocumentOrmat (ODP); 최종 DocumentFormat SXI = New DocumentFormat ( "OpenOffice.org 1.0 프리젠 테이션", DocumentFamily.Presentation, "application/vnd.sun.xml.impress", "sxi"); sxi.setexportfilter (DocumentFamily.Presentation, "StarOffice XML (인상)"); addDocumentFormat (sxi); 최종 DocumentFormat ppt = 새로운 docum ppt.setexportfilter (documentfamily.presentation, "ms powerpoint 97"); AddDocumentFormat (ppt); 최종 DocumentFormat ODG = 새로운 docum odg.setexportfilter (documentfamily.rawing, "draw8"); AddDocumentFormat (ODG); 최종 DocumentFormat SVG = New DocumentFormat ( "확장 가능한 벡터 그래픽", "이미지/SVG+XML", "SVG"); svg.setexportfilter (documentfamily.rawing, "draw_svg_export"); addDocumentFormat (SVG); }}
솔루션 : BasicDocumentOrmatregistry 클래스에서 공개 DocumentFormat 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 또는 이후 버전의 버전 2.1. // //이 라이브러리는 유용하기를 희망하여 배포되지만 // 보증이 없습니다. // 상인성 또는 특정 목적에 대한 적합성에 대한 묵시적 보증조차 없습니다. 자세한 내용은 gnu // Lesser General Public License를 참조하십시오. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; java.util.arraylist 가져 오기; import java.util.iterator; Java.util.list 가져 오기; / *** Rewrite rewrite rewrite rewrite rewrite rewrite rewrite rewrite basicdocumentormatregistry document format* @author huguangjun*/ public class basicdocumentormatregistry emputobleformatregistry {private list/* <cumentformat>*/ documentformats = new arraylist (); public void addDocumentOrmat (DocumentFormat DocumentFormat) {documentformats.add (documentformat); } 보호 된 목록/ * <DocumentFormat> */ getDocumentOrmats () {return DocumentFormats; } / ** * @param eptension * 파일 확장자 * @ @return이 확장자에 대한 documentformat, 또는 확장자 * / public documentformat getFormatByFileExtension (String Extension) {if (extension == null) {return null; } // Unified 파일 이름 접미사 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 = documb if (format.getFileExtension (). Equals (lowEREXTENSION)) {return format; }} return null; } public d if (format.getMimeType (). Equals (Mimetype)) {return format; }} return null; }}
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.