To generate a Word document from code using an API - without using a template - use WordApplication, the main class for pure code-based document generation. This class is an engine used to open, create, and write (save or stream to a browser) documents. A single instance of WordApplication can create multiple documents.
The WordApplication class is in the SoftArtisans.OfficeWriter.WordWriter namespace. The class can be referenced as SoftArtisans.OfficeWriter.WordWriter. To minimize typing and errors, import the namespace to the aspx page, and reference the class as WordApplication, without the namespace prefix. If you are coding directly in the .aspx page, following the Page directive, include:
If you are coding in a code-behind page (.aspx.vb or .aspx.cs), include an Imports or using statement at the top of the code-behind page:
To create an instance of WordApplication, use:
A Document object represents a Word document. To return a Document object:
- Call WordApplication.Open to open an existing Word file, for example:
- Call WordApplication.Create to create a new Word file, for example:
Each of the following classes represents a distinct section of content in a document. For information on how WordWriter organizes the structure of a Word document, see Word Application Object Tree
|Document||A Document object represents the whole Word document.|
|Section||A Section object represents a major section in a document, like a chapter in a book. Many documents will only contain one section. In more complex documents, content can be divided into multiple sections.|
|Paragraph||A Paragraph object represents a paragraph in the Word document - a block of text that ends with a carriage return.|
|CharacterRun||A CharacterRun object represents a contiguous run of characters in the document, all of which have the same formatting.|
|Table||A Table object represents a table in the document.|
|TableCell||Each cell in a table is represented by a TableCell object.|
|List||A List object represents a numbered or bulleted list.|
|ListEntry||Each entry in a list is represented by a ListEntry object.|
To insert a run of characters at the beginning of your document, call Document.InsertTextBefore:
The second parameter of InsertTextBefore specifies whether the inserted text is its own character run with default formatting (true), or if it should assume the formatting of the first character in the Element (in this case, the Document) and become a part of it (false).
You can use the formatting properties of the CharacterRun class to format the text you inserted:
When the document is complete, you can save it to the server's hard disk, return it in memory, stream it to the browser, or pass it to WordTemplate (for more information, see Output Options). For example, to stream the document to the browser, use:
When you pass an HttpResponse object to Save, OfficeWriter will stream the generated Word file to the client. The browser will display a download dialog asking the user to open or save the file. The method's third parameter specifies a file name to display in the download dialog. If a user opens the file, and the method's fourth parameter - openInBrowser - is true, the file opens in Internet Explorer. If openInBrowser is false, the file opens in the Word application.
|Word files cannot be embedded in browsers other than IE. For files to appear in a browser, you must enable the "Browse in same window" option. For more information, see Save (Document doc, System.Web.HttpResponse response, String fileName, boolean openInBrowser) and Save (Document doc, System.Web.HttpResponse response, String fileName, boolean openInBrowser, String contentType).|