Führer:
Während des Verteilungsprozesses wird Java häufig verwendet, um Office -Serien -Dokumente in PDFs umzuwandeln und im Allgemeinen OpenOffice+JodConverter zu verwenden, das von Microsoft bereitgestellt wird, um Dokumente zu konvertieren.
OpenOffice verfügt über eine Windows -Version und Linux -Version. Machen Sie sich keine Sorgen, dass die Produktionsumgebung ein Linux -System ist.
1. OpenOffice hängt von Jar ab. Nehmen Sie Maven als Beispiel:
<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> <gruppeID> org.openOffice </GroupId> <artifactId> RIDL </artifactId> <version> 3.0.1 </Version> </abhängig> <depectIncy> <GroupId> org.openOffice </GroupId> <artifactid> juh </artifactId> <version> 3.0.1 </Version> </Version> </Version> </Version> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </fusion> </depectIn </fusion> </fILDUCE> </vl. <artifactId> unoil </artifactId> <version> 3.0.1 </Version> </abhängig> <!-JodConverter2.2.1 muss sich auf SLF4J-JDK14 verlassen, andernfalls meldet die Protokolle einen Fehler im Quellcode. Es handelt
2. Um den Code direkt hochzuladen, müssen Sie den 8100 -Port der OpenOffice -Anwendung anhören.
public void convert (Datei SourceFile, Datei targetFile) {try {// 1: Öffnen Sie die Verbindung openOfficeConnection Connection = new SocketopenofficeConnection (8100); Connection.Connect (); DocumentConverter Converter = new OpenOfficedocumentConverter (Verbindung); // 2: Format documentFormatRegistry Factory = new BasicDocumentFormatRegistry (); DocumentFormat inputDocumentFormat = factory .GetFormatByFileXT (getExtensionName (SourceFile.getabsolutepath ())); DocumentFormat outputDocumentFormat = factory .GetFormatByFileXT (getExtensionName (targetFile.getabsolutepath ())); // 3: Führen Sie den Conversion Converter.Convert (SourceFile, InputDocumentFormat, TargetFile, OutputDocumentFormat) aus; } catch (ConnectException e) {log.info ("Dokumentkonvertierung fehlgeschlagen"); }}3. HINWEIS: JodConverter meldet einen Fehler nach dem Konvertieren des xxx.docx -Dokuments nach der Version von 2007. Jeder weiß, dass das 03 -Suffix xxx.doc ist. Die 07 -Version ist xxx.docx.
Wenn Sie den JodConverter -Quellcode überprüfen, wird festgestellt, dass documentFormat das XXX.DOCX -Format nicht unterstützt. BasicDocumentFormatRegistry. Öffentliches documentFormat getFormatByFileXTension (String -Erweiterung) wird standardmäßig unterstützt. DOC -Format wird verwendet.
BasicDocumentFormatRegistry Class Quellcode
// // JodConverter - Java Opendocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]> // // Diese Bibliothek ist kostenlose Software; Sie können es umverteilen und/oder // es unter den Bestimmungen der GNU Lesser allgemeine Öffentlichkeit // Lizenz wie von der Free Software Foundation veröffentlicht. Entweder // Version 2.1 der Lizenz oder (nach Ihrer Option) jede spätere Version. // // Diese Bibliothek wird in der Hoffnung verteilt, dass sie nützlich sein wird, // jedoch ohne Garantie; Ohne die implizite Garantie von // Handelsfähigkeit oder Fitness für einen bestimmten Zweck. Weitere Informationen finden Sie in der GNU // weniger allgemeine öffentliche Lizenz. // http://www.gnu.org/copyleft/lesser.html // Paket com.artofsolving.jodconverter; Import Java.util.ArrayList; Import Java.util.iterator; importieren java.util.list; öffentliche Klasse BasicDocumentFormatRegistry implementiert documentFormatRegistry {private Liste/*<DocumentFormat>*/ documentFormats = new ArrayList (); public void adddocumentFormat (documentFormat documentFormat) {documentFormats.add (documentFormat); } geschützte Liste/*<DocumentFormat>*/ getDocumentFormats () {return documentFormats; } / ** * @Param -Erweiterung Die Dateierweiterung * @return das documentFormat für diese Erweiterung oder NULL, wenn die Erweiterung nicht zugeordnet ist } String lowerextsion = extension.tolowerCase (); für (iterator it = documentFormats.iterator (); it.hasNext ();) {documentFormat format = (documentFormat) it.Next (); if (format.getFileextesion (). Equals (niedrigextsion)) {return format; }} return null; } public documentFormat getFormatByMiMetype (String mimetype) {für (iterator it = documentFormats.iterator (); it.hasNext ();) {documentFormat format = (documentFormat) iT.Next (); if (format.getMimetype (). Equals (Mimetype)) {return format; }} return null; }}
Die in der Standardimplementierungsklasse unterstützten Dateiformate sind wie folgt wie folgt
// // JodConverter - Java Opendocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]> // // Diese Bibliothek ist kostenlose Software; Sie können es umverteilen und/oder // es unter den Bestimmungen der GNU Lesser allgemeine Öffentlichkeit // Lizenz wie von der Free Software Foundation veröffentlicht. Entweder // Version 2.1 der Lizenz oder (nach Ihrer Option) jede spätere Version. // // Diese Bibliothek wird in der Hoffnung verteilt, dass sie nützlich sein wird, // jedoch ohne Garantie; Ohne die implizite Garantie von // Handelsfähigkeit oder Fitness für einen bestimmten Zweck. Weitere Informationen finden Sie in der GNU // weniger allgemeine öffentliche Lizenz. // http://www.gnu.org/copyleft/lesser.html // Paket com.artofsolving.jodconverter; Public Class DefaultDocumentFormatRegistry erweitert BasicDocumentFormatRegistry {public defaultDocumentFormatRegistry () {endgültig documentFormat pdf = new DocumentFormat ("tragbares Dokumentformat", "Anwendung/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); endgültig 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); endgültig documentFormat xhtml = new DocumentFormat ("xhtml", "application/xhtml+xml", "xhtml"); xhtml.setExportFilter (DocumentFamily.Presentation, "XHTML Impress -Datei"); xhtml.setExportFilter (documentFamily.spreadsheet, "xhtml calc -Datei"); xhtml.setExportFilter (documentFamily.text, "xhtml writer file"); AddDocumentFormat (xhtml); // HTML wird als Text als Eingabe behandelt. html.setExportFilter (DocumentFamily.Presentation, "impress_html_export"); html.setExportFilter (documentFamily.spreadsheet, "html (starcalc)"); html.setExportFilter (documentFamily.text, "html (Starwriter)"); AddDocumentFormat (HTML); endgültig documentFormat odt = new DocumentFormat ("Opendocument text", documentFamily.text, "application/vnd.oasis.opendocument.text", "ODT"); odt.setExportFilter (documentFamily.text, "writer8"); AddDocumentFormat (ODT); endgültig documentFormat sxw = new DocumentFormat ("openOffice.org 1.0 textdokument", documentFamily.text, "application/vnd.sun.xml.writer", "sxw"); sxw.setexportFilter (DocumentFamily.text, "StarOffice XML (Writer)"); AddDocumentFormat (SXW); endgültig documentFormat doc = new DocumentFormat ("Microsoft Word", documentFamily.text, "application/msword", "doc"); doc.setExportFilter (documentFamily.text, "MS Word 97"); AddDocumentFormat (DOC); endgültig documentFormat rtf = new DocumentFormat ("Rich Text Format", documentFamily.text, "text/rtf", "rtf"); rtf.setExportFilter (DocumentFamily.text, "Rich Text Format"); AddDocumentFormat (RTF); endgültig documentFormat wpd = new DocumentFormat ("WordPerfect", documentFamily.text, "application/wordPerfect", "wpd"); AddDocumentFormat (WPD); endgültig documentFormat txt = new DocumentFormat ("Klartext", documentFamily.text, "text/plain", "txt"); // Filtername auf "text" festlegen, um zu verhindern, dass OOO den Dialog "ASCII -Filteroptionen" angezeigt wird. txt.setExportFilter (documentFamily.text, "text"); addDocumentFormat (txt); endgültig documentFormat wikitext = new DocumentFormat ("Mediawiki Wikitext", "Text/X-Wiki", "Wiki"); Wikitext.setExportFilter (DocumentFamily.text, "Mediawiki"); addDocumentFormat (Wikitext); endgültig documentFormat ods = new DocumentFormat ("Opendocument -Tabelle", documentFamily.spreadsheet, "application/vnd.oasis.opendocument.spreadsheet", "ODS"); ods.setExportFilter (documentFamily.spreadsheet, "Calc8"); AddDocumentFormat (ODS); endgültig documentFormat sxc = new DocumentFormat ("openOffice.org 1.0 treadsheet", documentfamily.spreadsheet, "application/vnd.sun.xml.calc", "SXC"); sxc.setExportFilter (DocumentFamily.Spreadsheet, "StarOffice XML (Calc)"); AddDocumentFormat (SXC); endgültig documentFormat xls = new DocumentFormat ("Microsoft Excel", documentFamily.spreadsheet, "application/vnd.ms-excel", "xls"); xls.setExportFilter (DocumentFamily.Spreadsheet, "MS Excel 97"); addDocumentFormat (xls); endgültig documentFormat csv = new DocumentFormat ("CSV", documentFamily.spreadsheet, "text/csv", "csv"); csv.setImportoption ("filtername", "text - txt - csv (starcalc)"); CSV.SetImportoption ("Filteroptionen", "44,34,0"); // Feldabscheider: ','; Textlimiter: '' 'csv.setExportFilter (Dokumentfamilie Werte ", documentFamily.spreadsheet," text/tab -separatierte Werte "," TSV "); tsv.setExportFilter (documentFamily.spreadsheet, "text - txt - csv (starcalc)"); tsv.setExportoption (DocumentFamily.Spreadsheet, "Filteroptions", "9,34,0"); AddDocumentFormat (TSV); endgültig documentFormat odp = new DocumentFormat ("Opendocument Presentation", DocumentFamily.Presentation, "Application/Vnd.Oasis.opendocument.Presentation", "ODP"); ODP.SetExportFilter (DocumentFamily.Presentation, "impress8"); AddDocumentFormat (ODP); endgültig documentFormat sxi = new DocumentFormat ("openOffice.org 1.0 Präsentation", documentFamily.presentation, "application/vnd.sun.xml.impress", "sxi"); sxi.setExportFilter (DocumentFamily.Presentation, "StarOffice XML (impress)"); AddDocumentFormat (SXI); endgültig documentFormat ppt = new DocumentFormat ("Microsoft PowerPoint", DocumentFamily.Presentation, "Application/VND.MS-Powerpoint", "PPT"); ppt.setExportFilter (DocumentFamily.Presentation, "MS PowerPoint 97"); AddDocumentFormat (PPT); endgültig documentFormat odg = new DocumentFormat ("Opendocument Drawing", DocumentFamily.Drawing, "Application/Vnd.oasis.opendocument.graphics", "ODG"); odg.setExportFilter (DocumentFamily.Drawing, "Draw8"); AddDocumentFormat (ODG); endgültig documentFormat svg = new DocumentFormat ("Skalierbare Vektorgrafiken", "Image/SVG+XML", "SVG"); svg.setexportFilter (DocumentFamily.Drawing, "Draw_Svg_export"); AddDocumentFormat (SVG); }}
Lösung: Schreiben Sie die öffentliche DocumentFormat -Methode GetFormatByFileXT (String -Erweiterung) in der BasicDocumentFormatRegistry -Klasse neu. Verwenden Sie das Dokumentformat -Format von DOC von DOC, solange der Suffix -Name DOC enthält
// // JodConverter - Java Opendocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]> // // Diese Bibliothek ist kostenlose Software; Sie können es umverteilen und/oder // es unter den Bestimmungen der GNU Lesser allgemeine Öffentlichkeit // Lizenz wie von der Free Software Foundation veröffentlicht. Entweder // Version 2.1 der Lizenz oder (nach Ihrer Option) jede spätere Version. // // Diese Bibliothek wird in der Hoffnung verteilt, dass sie nützlich sein wird, // jedoch ohne Garantie; Ohne die implizite Garantie von // Handelsfähigkeit oder Fitness für einen bestimmten Zweck. Weitere Informationen finden Sie in der GNU // weniger allgemeine öffentliche Lizenz. // http://www.gnu.org/copyleft/lesser.html // Paket com.artofsolving.jodconverter; Import Java.util.ArrayList; Import Java.util.iterator; importieren java.util.list; / *** Schreiben Sie BasicDocumentFormatRegistry -Dokumentformat* @Author Huguangjun*/ public class BasicDocumentFormatRegistry implementiert eine DocumentFormatRegistry {private Liste/* <DocumentFormat>*/ documentFormats = new ArrayList (); public void adddocumentFormat (documentFormat documentFormat) {documentFormats.add (documentFormat); } geschützte Liste/ * <DocumentFormat> */ getDocumentFormats () {return documentFormats; } / ** * @Param -Erweiterung * Die Dateierweiterung * @return das documentFormat für diese Erweiterung oder null, wenn die Erweiterung * nicht zugeordnet ist } // Unified Dateiname Suffix if (erweitert.indexof ("doc")> = 0) {tenweiterung = "doc"; } if (extension.indexof ("ppt")> = 0) {extension = "ppt"; } if (erweitert.indexof ("xls")> = 0) {extension = "xls"; } String lowerextsion = extension.tolowerCase (); für (iterator it = documentFormats.iterator (); it.hasNext ();) {documentFormat format = (documentFormat) it.Next (); if (format.getFileextesion (). Equals (niedrigextsion)) {return format; }} return null; } public documentFormat getFormatByMiMetype (String mimetype) {für (iterator it = documentFormats.iterator (); it.hasNext ();) {documentFormat format = (documentFormat) iT.Next (); if (format.getMimetype (). Equals (Mimetype)) {return format; }} return null; }}
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.