Converting doc/docx to pdf in java

java
  • Turgay Can
  • Tarih

    26 Mar, 2012
  • Yorum

    0
  • Görüntüleme

    2255
  • İndirme

    0

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("com.sun.star.connection.Connector", xcomponentcontext);

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

    XConnector xconnector = (XConnector) UnoRuntime.queryInterface(

                                                    com.sun.star.connection.XConnector.class, objectUrlResolver);

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

    com.sun.star.connection.XConnection xconnection = xconnector
                                                    .connect(url);

    XBridgeFactory xbridgefactory = (XBridgeFactory) UnoRuntime
    .queryInterface(com.sun.star.bridge.XBridgeFactory.class,
                            objectUrlResolver);

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

    XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(com.sun.star.lang.XComponent.class, xbridge);

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

    xmulticomponentfactory = (XMultiComponentFactory) 
UnoRuntime.queryInterface(com.sun.star.lang.XMultiComponentFactory.class,
                            objectUrlResolver);
    XPropertySet xpropertyset = (XPropertySet) UnoRuntime.queryInterface(

    com.sun.star.beans.XPropertySet.class, xmulticomponentfactory);

    Object obj1 = xpropertyset.getPropertyValue("DefaultContext");
    com.sun.star.uno.XComponentContext xcomponentcontext = (XComponentContext) 
UnoRuntime.queryInterface(com.sun.star.uno.XComponentContext.class, obj1);

    com.sun.star.frame.XComponentLoader xcomponentloader = (XComponentLoader)
 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,

    xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext));

    Object obj2 = xmulticomponentfactory.createInstanceWithContext("com.sun.star.util.URLTransformer", xcomponentcontext);

    com.sun.star.util.XURLTransformer xTransformer = (XURLTransformer) UnoRuntime.queryInterface(

    com.sun.star.util.XURLTransformer.class, obj2);

    Object obj3 = xmulticomponentfactory.createInstanceWithContext(

    "com.sun.star.configuration.ConfigurationProvider",
    xcomponentcontext);

    com.sun.star.lang.XMultiServiceFactory mServiceFactory =
 (XMultiServiceFactory) UnoRuntime.queryInterface(
    com.sun.star.lang.XMultiServiceFactory.class, 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(com.sun.star.frame.XStorable.class,
 xcomponent);
    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(com.sun.star.frame.XStorable.class,
 xComponent);
    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