Forums     Knowledge Base     OfficeWriter Online     
 
 
This documentation is for
OfficeWriter v3.8.1
.NET Platform

View Docs for Another
Version or Platform

Quick Start > Create a Document with WordApplication

Create a Document with WordApplication

To create 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.

Step 1: Import WordApplication

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:

<%@ Import Namespace="SoftArtisans.OfficeWriter.WordWriter" %>

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:

In C#: using SoftArtisans.OfficeWriter.WordWriter;
In VB.NET: Imports SoftArtisans.OfficeWriter.WordWriter

Step 2: Create an Instance of WordApplication

To create an instance of WordApplication, use:

In C#: WordApplication wordApp = new WordApplication();
In VB.NET: Dim wordApp As New WordApplication()

Step 3: Create a Document

A Document object represents a Word document. To return a Document object:

  • Call WordApplication.Open to open an existing Word file, for example:

    // [C#]
    Document doc = wordApp.Open(@"C:\Reports\Report.doc");

    ' [VB.NET]
    Dim doc As Document = wordApp.Open("C:\Reports\Report.doc")


    Or,

  • Call WordApplication.Create to create a new Word file, for example:

    // [C#]
    Document doc = wordApp.Create();

    ' [VB.NET]
    Dim doc As Document = wordApp.Create()

Step 5: Add Content to the Document

Each of the following classes represents a distinct section of content in a document:

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:

// [C#]
CharacterRun charRun = 
	doc.InsertTextBefore("This file was created with OfficeWriter", true);
	
' [VB.NET]
Dim charRun As CharacterRun = _
	doc.InsertTextBefore("This file was created with OfficeWriter", True)

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:


// [C#]
charRun.Font.Bold = true;
charRun.Font.FontName = "Times New Roman";

' [VB.NET]
charRun.Font.Bold = True
charRun.Font.FontName = "Times New Roman"

Step 6: Generate a New Word File

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:


// [C#]
bool bPreserve = true;
bool bOpenInBrowser = false;
wordApp.Save(doc, // Document object to save
	bPreserve,  
	Page.Response,
	"OfficeWriter.doc",
	bOpenInBrowser);
	
' [VB.NET]
Dim bPreserve As Boolean = True
Dim bOpenInBrowser As Boolean = False
wordApp.Save(doc, _
	bPreserve, _
	"OfficeWriter.doc", _
	bOpenInBrowser)	 

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 fourth parameter specifies a file name to display in the download dialog. If the method's fifth parameter - openInBrowser - is true, the browser is Internet Explorer, and the user chooses to open the Word file, the file will open in the browser window. Note: Word files cannot be embedded in browsers other than IE.



Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.