Converting doc/docx to pdf in java

  • Turgay Can
  • Tarih

    26 Mar, 2012
  • Yorum

  • Görüntüleme

  • İndirme


Converting doc/docx to pdf in java

For this we will use following technology:
Core java
Open office Universal Network Objects (UNO)

Open office provides functionality to convert doc/docx files to pdf files. We will use open office API from java to achieve this.
For this we will use open office UNO (Universal Network Objects) API to communicate with open office.

Overview of UNO:

UNO is a component model that offers inter-operability between different programming languages, different objects models, different machine architectures, and different processes; either in a LAN or via the Internet.

The StarOffice and Sun ONE Webtop products have proven the usability of UNO in complex real world applications. Developers that want to use, extend, or modify the functionality of one of the products will do this using UNO.

UNO is not limited to the above applications. The base libraries of UNO are independent of StarOffice and can be used as a framework for other applications.

UNO is freely available (it is distributed under the LGPL license) and currently supports Java, C and C++ (on windows, Linux, and Solaris). A bridge for COM OLE Automation already exists.

UNO is interface based, as are COM and CORBA. Components implement interfaces compliant to their interface specification. Multiple components communicate only via their interfaces. This allows implementing one component in a different language or to move an implementation to another machine, without modifying the other's components. This gives you huge flexibility and preserves earlier invested efforts.
Each component lives in a Uno Runtime Environment (URE). A URE is identified by the implementation language (e.g., C++, Java, Perl, ...) and the current process. There is no performance overhead for components, that are instantiated within the same URE, e.g., in C++, a call from component A to B is just a virtual call. The calls between components from different UREs are bridged by UNO.

Converting doc/docx to pdf in java

Java source code to convert from doc/docx to pdf:

First we have to create XComponentContext, XMultiComponentFactory and url resolver.

    XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);

    XMultiComponentFactory xmulticomponentfactory = xcomponentcontext.getServiceManager();

    Object objectUrlResolver = 
xmulticomponentfactory.createInstanceWithContext("", xcomponentcontext);

    Then we have to create xconnector, xconnection and other initialization code:

    XConnector xconnector = (XConnector) UnoRuntime.queryInterface(

                                          , objectUrlResolver);

    String connectString = "uno:socket,host=localhost,port=8100;urp,Negotiate=0,forceSynchronous=1;
StarOffice.ServiceManager"; xconnection = xconnector

    XBridgeFactory xbridgefactory = (XBridgeFactory) UnoRuntime

    XBridge xbridge = xbridgefactory.createBridge("", as[1], xconnection, null);

    XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(, xbridge);

    objectUrlResolver = xbridge.getInstance("StarOffice.ServiceManager");

    xmulticomponentfactory = (XMultiComponentFactory) 
    XPropertySet xpropertyset = (XPropertySet) UnoRuntime.queryInterface(, xmulticomponentfactory);

    Object obj1 = xpropertyset.getPropertyValue("DefaultContext"); xcomponentcontext = (XComponentContext) 
UnoRuntime.queryInterface(, obj1); xcomponentloader = (XComponentLoader)

    xmulticomponentfactory.createInstanceWithContext("", xcomponentcontext));

    Object obj2 = xmulticomponentfactory.createInstanceWithContext("", xcomponentcontext); xTransformer = (XURLTransformer) UnoRuntime.queryInterface(, obj2);

    Object obj3 = xmulticomponentfactory.createInstanceWithContext(

    xcomponentcontext); mServiceFactory =
 (XMultiServiceFactory) UnoRuntime.queryInterface(, obj3);

    Then we have to open the document in invisible mode:
    PropertyValue apropertyvalue[] = new PropertyValue[2];
    apropertyvalue[0] = new PropertyValue();
    apropertyvalue[1] = new PropertyValue();
    apropertyvalue[0].Name = "Hidden";
    apropertyvalue[0].Value = new Boolean(false);

    apropertyvalue[1].Name = "ShowTrackedChanges";
    apropertyvalue[1].Value = new Boolean(false);
    xcomponentloader.loadComponentFromURL(s1, "_blank", 0, apropertyvalue);

After this we have to convert to SXW

    XStorable xstorable = (XStorable)UnoRuntime.queryInterface(,
    PropertyValue apropertyvalue[] = new PropertyValue[3];
    apropertyvalue[0] = new PropertyValue();
    apropertyvalue[1] = new PropertyValue();
    apropertyvalue[2] = new PropertyValue();
    apropertyvalue[0].Name = "Overwrite";
    apropertyvalue[0].Value = new Boolean(true);                     
    apropertyvalue[1].Name = "ShowTrackedChanges";
    apropertyvalue[1].Value = new Boolean(false);

    xstorable.storeToURL(s1, apropertyvalue);

Then finally we have to convert to pdf

    XStorable xstorable = (XStorable)UnoRuntime.queryInterface(,
    PropertyValue apropertyvalue[] = new PropertyValue[2];
    apropertyvalue[0] = new PropertyValue();
    apropertyvalue[0].Name = "FilterName";
    apropertyvalue[0].Value = "writer_pdf_Export";
    apropertyvalue[1] = new PropertyValue();
    apropertyvalue[1].Name = "CompressionMode";
    apropertyvalue[1].Value = "1";

    xstorable.storeToURL(s1, apropertyvalue);

0 Yorum..

Yorum yapmak için "Giriş yapın" yada "Misafir üye" olarak yorum yapabilirsiniz.

Yorum Yap