Use libreoffice to convert ppt and Doc to PDF

Source: Internet
Author: User
Use libreoffice to convert ppt and Doc to PDF

 

Libreoffice: http://www.libreoffice.org/download/libreoffice-fresh/

Jodconverter jar package download: http://sourceforge.net/projects/jodconverter/files/JODConverter/

 

 

 

Import Java. io. file; import Java. io. filenotfoundexception; import Java. io. ioexception; import java.net. connectexception; import Java. util. date; import COM. artofsolving. jodconverter. documentconverter; import COM. artofsolving. jodconverter. openOffice. connection. openofficeconnection; import COM. artofsolving. jodconverter. openOffice. connection. socketopenofficeconnection; import COM. artofsolving. jodconver Ter. openOffice. converter. openofficedocumentconverter; public class office2pdf {public static int office2pdf (string sourcefile, string destfile) {openofficeconnection connection = NULL; try {file inputfile = new file (sourcefile); If (! Inputfile. exists () {return-1; // file does not exist} // Folder does not exist create directory file outputfile = new file (destfile); If (! Outputfile. getparentfile (). exists () {outputfile. getparentfile (). mkdirs ();} // connect to an OpenOffice.org instance running on port 8100 connection = new socketopenofficeconnection ("127.0.0.1", 8100); connection. connect (); // convert documentconverter converter = new openofficedocumentconverter (connection); converter. convert (inputfile, outputfile); Return 0;} catch (connectexception e) {e. PRI Ntstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {If (connection! = NULL) {// close the connection. disconnect () ;}} return 1 ;}public static void main (string [] ARGs) {date startdate = new date (); string sourcefile = "D: \ code \ alipay.ppt "; string destfile =" d :\\ Code \ change2.pdf "; system. out. println (office2pdf. office2pdf (sourcefile, destfile); Date enddate = new date (); system. out. println ("the cost time is" + (enddate. gettime ()-startdate. gettime ()));}}

 

Run

Soffice -- headless -- accept = "socket, host = 127.0.0.1, Port = 8100; URP ;"

Soffice is in the libreoffice installation directory

 

If the following exception occurs:

Com. artofsolving. jodconverter. OpenOffice. Connection. openofficeexception: Conversion failed: cocould not load input document

Use the latest libreoffice whenever possible

 

Use libreoffice to convert ppt and Doc to PDF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.