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.

Advertisements

5 comments

  1. Anonymous · July 13, 2010

    great post!!

    Like

  2. Anonymous · November 22, 2010

    This is also what i do when i was in front of this problem , but the real question : there is any way to manipulate a word document with a library in flex ??

    Like

  3. Tony Georgiev · November 22, 2010

    Well I don't think there is such a library or there will be. The solution I think would be to convert the doc back and forth, from doc to desired by you format to edit in flex, and then to export it back to doc… something like what Acrobat.com is doing.

    Like

  4. nitin · March 3, 2012

    Can I have the code for implementing flex paper in asp.net

    Like

  5. Anonymous · July 5, 2012

    Thanks Tony, this helped me a lot.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s