Output Options


WordWriter's Save method generates a new Word file and either saves it on the server, or streams 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.

Save to diskTo save the generated file on the server:
  1. Pass the Save method a file path, for example:
    oWW.Save("c:\Documents\File.doc")
    OR

    Pass the Save method a System.IO.FileStream object. For example:
    oFileStream = New FileStream(Server.MapPath("./StreamOutput.doc"), FileMode.Create)
    ...
    oWW.Save(oFileStream)
    ...
    If Not oFileStream Is Nothing Then
    	oFileStream.Close()
    End If
VB.NET Example
Basic/SaveToDisk/SaveToDisk-vb.aspx

[View Source]
C# Example
Basic/SaveToDisk/SaveToDisk-cs.aspx

[View Source]
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. For example:

	oWW.Save(Page.Response, "StringVarOutput.doc", True)
VB.NET Example
Basic/OpenInline/OpenInline-vb.aspx

[View Source]
C# Example
Basic/OpenInline/OpenInline-cs.aspx

[View Source]

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:

  1. Set the response's content-type header to "application/vnd.ms-word":
    Response.ContentType = "application/vnd.ms-word"

  2. Set the response's content-disposition to "inline":
    Response.AddHeader("Content-Disposition", "inline;filename=""WordWriter.doc""")

  3. Pass the Save method Response.Stream as a parameter:
    oWW.Save(Response.OutputStream)
Open in Microsoft Word

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 Microsoft Word. For Example:

	oWW.Save(Page.Response, "StringVarOutput.doc", False)
VB.NET Example
Basic/StringVar/StringVar-vb.aspx

[View Source]
C# Example
Basic/StringVar/StringVar-cs.aspx

[View Source]

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 Microsoft Word:

  1. Set the response's content-type header to "application/vnd.ms-word":
    Response.ContentType = "application/vnd.ms-word"

  2. Set the response's content-disposition to "attachment":
    Response.AddHeader("Content-Disposition", "attachment;filename=""WordWriter.doc""")

  3. Pass the Save method Response.Stream as a parameter:
    oWW.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:

  1. oWW.Save(Server.MapPath("./SaveMultipleOutput.doc"))
    Saves the generated Word file on the server.

  2. 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 Microsoft Word.
VB.NET Example
Basic/SaveMultiple/SaveMultiple-vb.aspx

[View Source]
C# Example
Basic/SaveMultiple/SaveMultiple-cs.aspx

[View Source]


Copyright © 2003, SoftArtisans, Inc.