Page 1 of 1

how to open a generated docx file

PostPosted: Wed Apr 24, 2013 3:06 pm
by lazywednesday
I am currently doing a program that generates docx files. and with docx4j i can successfully save it without any problems. but now i need to make an option where the user can view the docx file instead of saving it. how do i do this using java? here's my sample program

Code: Select all
ObjectFactory factory = null;
factory = Context.getWmlObjectFactory();
WordprocessingMLPackage wordMLPackage;
wordMLPackage = WordprocessingMLPackage.createPackage();
String fileName = "Sample.docx";
String filePath = SettingsConfig.getTempFolderPath(fileName);

P pSample = factory.createP();
R rSample = factory.createR();
Text txtSample = factory.createText();
txtSample .setValue("SAMPLE TEXT");
rSample .getContent().add(txtSample );
pSample .getContent().add(rSample );

wordMLPackage.getMainDocumentPart().addObject(pSample );

wordMLPackage.save(new java.io.File(filePath));


this code saves my generated docx file in my workspace. how do i open the generated docx file via java?

Re: how to open a generated docx file

PostPosted: Wed Apr 24, 2013 5:42 pm
by jason
view it / open it, with what application?

Re: how to open a generated docx file

PostPosted: Wed Apr 24, 2013 5:54 pm
by lazywednesday
with Microsoft office Word.

Re: how to open a generated docx file

PostPosted: Wed Apr 24, 2013 7:13 pm
by jason
Will your code be running on a web server, with users interacting with it via their web browser, or is it a desktop application?

Re: how to open a generated docx file

PostPosted: Wed Apr 24, 2013 7:24 pm
by lazywednesday
yes it will be running on a server with users on their web browser particularly internet explorer 7

Re: how to open a generated docx file

PostPosted: Wed Apr 24, 2013 7:43 pm
by jason
See docx-java-f6/urgent-output-stream-rendering-to-browser-t320.html

or, if using JAX-RS, something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                // Now stream the docx
                final SaveToZipFile saver = new SaveToZipFile(output);         
                ResponseBuilder builder = Response.ok(
                               
                        new StreamingOutput() {                        
                                public void write(OutputStream output) throws IOException, WebApplicationException {                                   
                                 try {
                                                saver.save(output);
                                        } catch (Docx4JException e) {
                                                throw new WebApplicationException(e);
                                        }                                                      
                                }
                        }
                );
                builder.header("Content-Disposition", "attachment; filename=foo.docx");
                builder.type("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                return builder.build();
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Whether the user is prompted to save the docx or it opens within their browser is a client side config thing. There is a Microsoft KB article somewhere which describes what to do for Internet Explorer.

hope this helps .. Jason

Re: how to open a generated docx file

PostPosted: Thu Apr 25, 2013 8:43 pm
by lazywednesday
ok i tried to copy and paste your code but i got lots of errors including some with no available imports. so i looked at the thread that you posted and pasted your code that was written there. the problem is how do i make the "res"? the httpservlet. i tried making a HttpServletResponse res = new HttpServletResponse. But then lots of handlers poped out. how do i declare the res?

Re: how to open a generated docx file

PostPosted: Thu Apr 25, 2013 11:28 pm
by jason
In a plain old servlet you typically implement method:

public void doPost(HttpServletRequest req, HttpServletResponse res )

or

public void doGet(HttpServletRequest req, HttpServletResponse res )

Notice res is HttpServletResponse in the method signature.