Thursday, May 13, 2010

Viewing Word Documents with Flex

Recently I have started a very interesting project and part of the project is to be able to display .pdf and .doc files in a flex application.

[Source: www.sudafrica.it/documenti%20calcio%202010/Zakumi.doc ]


[Source: http://www.primussoccer.com/Zakumi.pdf]


For the pdf I had luck finding this great tool FlexPaper. The idea behind it was to convert the pdf to swf using PDF2SWF from SWFTools so for every page of the PDF document you had a frame in the swf.
FlexPaper does a great job for viewing .pdf documents, but what about .doc?
Well I guess the solution had to be pretty much the same - convert the doc to swf and display it with FlexPaper. Unfortunately I could not find any good doc to swf converter.

So I asked myself why not just convert the Word document to Adobe Reader document and then do the same what I am doing for .pdf documents (convert them to .swf and view them with FlexPaper). That seemed like the straight-forward solution. Converting .doc to .pdf (and other various formats) is really easy with OpenOffice and it comes for Windows, Linux and Mac OS. When you open your document you just have to click File and Export as PDF.


After that we start PDF2SWF from the command-line with the corresponding options


However if you want to make this process programmatically in a headless mode here is the solution. There is this Java library called JODConverter that relies on OpenOffice that comes here for the help.
So here is how to convert Word documents to Adobe Reader documents programmatically with Java

public static File convertToPDF(File file){
  String oldFilePath = file.getAbsolutePath();
  //change the extension to .pdf
  String newFilePath = oldFilePath.substring(0, oldFilePath.lastIndexOf(".")) + ".pdf";
  
  OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();
    officeManager.start();

    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
    File newFile = new File(newFilePath);
    converter.convert(file, newFile);
    
    officeManager.stop();
    
    return newFile;
}

After we are done with this, we have to convert the pdf to swf

private static File convertToSWF(File file) throws IOException{
    String oldFilePath = file.getAbsolutePath();
    //change the extension to .swf
    String newFilePath = oldFilePath.substring(0, oldFilePath.lastIndexOf(".")) + ".swf";
    
    //calling pdf2swf to convert our file.pdf to file.swf

    String[] command = { "pdf2swf", file.getAbsolutePath(), "-o", newFilePath, "-f",
            "-T 10", "-t", "-G"};
    
    Process p = Runtime.getRuntime().exec(command);
    
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
    String line = null;  
    while ((line = in.readLine()) != null) {  
        log.info(line);  
    } 
    
    return new File(newFilePath);
}

This is my workaround for viewing Word Documents with Flex, maybe you have a better solution? I'd love to hear it.