OpenOffice.org Open Source Office suite to convert various documents to PDF

Source: Internet
Author: User
Tags object documentation return

Recently, a requirement was encountered in the project to convert various documents to PDF. This should be a very common job, and I just need to support common document formats such as Ms Word,excel,powerpoint. So a friend suggested, you can use MS Office to turn. Of course, you can also use other methods, such as installing some PDF printers, and printing documents into PDF files. But these practices seem to have some problems with the "mandate". Of course, I also found a number of business solutions (such as aspose) guaranteed, I look at the way it is not expensive to authorize. But now it seems that OpenOffice.org has been able to meet my needs. Please let me know if you have a better practice.

OpenOffice.org is an open source Office suite that provides multiple software corresponding to Ms Word,excel,powerpoint, which is often enough to use. More importantly, it supports a variety of formats, including Ms Office 2007, and is able to export it as a PDF file, plus the way it is licensed is LGPL, and there is no obvious limit to the use of nature in a production environment. Besides, OOO has quite a lot of development documentation, and I'm confident that I can do it--but I didn't think it would be as smooth as it thought.

Compiling through is also not easy

First, I installed the OpenOffice.org main program and the SDK. The SDK comes with some sample code, where the Documenthandling section contains exactly one of the documentconverter features I need. So I turned on eclipse and poured it into the file, and it was clear that there were countless error tips: No proper class libraries were introduced. So which jar packages should I refer to? According to some other bits and pieces of information, I should introduce a few jar packages under the ~\basis\program\classes, such as Unoil.jar, Juh.jar ... Wait, where is this bag? In fact, I only find Unoil.jar this only one in this catalogue. Inexplicable, I have a brain to the directory of more than 30 jar packages all introduced, but the error remains.

I was in the search engine constantly using Juh.jar related keywords to query, hoping to find some hints, nothing. Then I use the system of file search, in the ~/basis directory to find *.jar, or no trace of Juh.jar found. So I am frustrated, how the first step is so not smooth. It wasn't until about one hours later that I accidentally found those key jar packs in the ~\ure\java directory. I breathed a sigh of relief after the introduction: The sample code was finally compiled. In summary, if you need to get Documentconverter.java compiled, you need to introduce three jar packages:

~\ure\java\juh.jar

~\ure\java\jurt.jar

~\basis\program\classes\unoil.jar

Really hate the document and the actual phenomenon does not match the situation, consumption of time does not say, the mood has become worse.

Organizing Sample Code

I have to say, Documentconverter.java is really not a good example code. First of all, it does not play a good role in demonstration. My ideal sample code should be able to clearly illustrate the way and steps of work without adding too much extra content. The effect of this example code is to "convert all files in a specified directory" and use recursion. Plus it has no import of any type, and each type drags a long "Com.sun.star" when it is used, which makes the Java code that is already very redundant becomes more difficult to understand. Not to mention the conflicts between annotations and the code itself, as well as the redundant type casts, and so on. To do this, I rewrote the sample code and split the whole process into several steps, based on the documentation description.

First, we open and connect an OOO program, which requires creating a Xcomponentcontext object:


private static Xcomponentcontext Createcontext () throws Exception {
Get the remote office component context
return Bootstrap.bootstrap ();
}

Then create a Xcomponentloader object:


private static Xcomponentloader Createloader (Xcomponentcontext context) throws Exception {
Get the remote Office service Manager
Xmulticomponentfactory mcf = Context.getservicemanager ();
Object desktop = Mcf.createinstancewithcontext ("Com.sun.star.frame.Desktop", context);
Return Unoruntime.queryinterface (xcomponentloader.class, desktop);
}



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.