|
WordTemplate in Depth
> Output Options
Output Options
After populating your WordWriter template from a data source, call
Save to either save the new Word file on the server, or stream it to the
browser. WordWriter can:
| When WordWriter streams a file to the client, the output result
will depend on the browser as well as the server-side script. For
example, when WordWriter streams a file directly to the browser or
to Microsoft Word, the browser may append a number to the name of
the file (because it caches a copy of the file and files must have
unique names). The best way to control download behavior on the client
is use
OfficeWriter Assistant, which is available with
WordWriterEE. |
Save to Disk

To save the generated file on the server:
Pass the Save method a file path, for example:
// [C#]
WordTemplate wt = new WordTemplate();
...
wt.Process();
wt.Save(@"c:\Documents\File.doc");
' [VB.NET]
Dim wt As New WordTemplate()
...
wt.Process()
wt.Save("c:\Documents\File.doc")
OR
Pass the Save method a System.IO.FileStream
object, for example:
// [C#]
using System.IO; // For FileStream object
...
WordTemplate wt = new WordTemplate();
...
wt.Process();
using(FileStream fstream = new FileStream(@"c:\Documents\File.doc",
FileMode.Create))
{
wt.Save(fstream);
fstream.Close();
}
' [VB.NET]
Imports System.IO ' For FileStream object
...
Dim wt As New WordTemplate()
...
wt.Process()
Dim fstream As new FileStream("c:\Documents\File.doc",
FileMode.Create)
Try
wt.Save(fstream)
fstream.Close()
Finally
If Not fstream Is Nothing Then
fstream.Dispose()
End If
End Try
Open in the Browser

When you pass an HttpResponse object to Save, WordWriter will
stream the generated Word file to the client. The browser will display a File
Download dialog asking the user to open or save the file. The method's second
parameter specifies a file name to display in the File Download dialog.
If the method's third parameter - OpenInBrowser - is True and the user
chooses to open the Word file, the file will open in the browser window.
// [C#]
wt.Save(Page.Response, "StringVarOutput.doc", true);
' [VB.NET]
wt.Save(Page.Response, "StringVarOutput.doc, True)
Alternatively, stream the file to the browser by passing
Response.Stream to the Save method. In this case,
to open the generated file in the browser window:
- Set the response's content-type header to
"application/vnd.ms-word":
Response.ContentType = "application/vnd.ms-word"
- Set the response's content-disposition to
"inline":
Response.AddHeader("Content-Disposition", "inline;filename=""WordWriter.doc""")
- Pass the
Save method
Response.Stream as a parameter:
oWW.Save(Response.OutputStream)
Open in the Default Application for .doc Files

When you pass an HttpResponse object to Save, WordWriter will stream the
generated Word file to the client. The browser will display a File Download
dialog asking the user to open or save the file. The method's second parameter
specifies a file name to display in the File Download dialog. If the method's
third parameter - OpenInBrowser - is False and the user chooses to open the
Word file, the file will open in the default application for .doc files
(such as, Microsoft Word, StarOffice, or OpenOffice).
// [C#]
bool bOpenInBrowser = false;
wt.Save(Page.Response, "StringVarOutput.doc", bOpenInBrowser);
' [VB.NET]
Dim bOpenInBrowser As Boolean = False
wt.Save(Page.Response, "StringVarOutput.doc, bOpenInBrowser)
Alternatively, stream the file to the browser by passing
Response.Stream to the Save method. In this case,
to open the generated file in the default application for Word files:
- Set the response's content-type header to
"application/vnd.ms-word":
// [C#]
Response.ContentType = "application/vnd.ms-word";
' [VB.NET]
Response.ContentType = "application/vnd.ms-word"
- Set the response's content-disposition to
"attachment":
// [C#]
Response.AddHeader("Content-Disposition", attachment;filename=WordWriter.doc");
' [VB.NET]
Response.AddHeader("Content-Disposition",_ "attachment;filename=""WordWriter.doc""")
- Pass the
Save method
Response.Stream as a parameter:
// [C#] wt.Save(Response.OutputStream);
' [VB.NET] wt.Save(Response.OutputStream)
Save and Stream to Browser

You can call Save more than once for a single instance of
WordTemplate. In the following examples Save is
called twice:
- oWW.Save(Server.MapPath("./SaveMultipleOutput.doc"))
Saves the generated Word file on the server.
- oWW.Save(Page.Response, "SaveMultipleOutput.doc", False)
Streams the generated Word file to the client. Since the parameter
OpenInBrowser is False, the file will open in the default application
for .doc files.

Copyright 2005 © SoftArtisans, Inc. All Rights Reserved.
|