4/09/2012

Set up Word Styles with C#, VB.NET

When we decorate our new house, we exert our utmost effort to choose furnishings to match house style. Besides, we also design to place every piece of furniture in the right position. Only both the house furnishings and their positions are suitably designed can the interior be gracefully decorated. By the same token, our word document is just like a house, only when we append the useful content and set up compatible content styles can our document look more distinctive and outstanding. Various word styles are available in the word processing program. Word styles allow you set up the format to combine the document and its information in the best way. Now let us learn how to set up word styles with C#, VB.NET in Spire. Doc when using Visual Studio. Of course, we should know what is Spire. Doc first.
Spire.Doc, as one important product of e-iceblue, is an MS word component. But Spire. Doc enables its users to perform a wide range of processing tasks directly without Microsoft Office Word installed in the computer. It can effectively generate, read, write and modify Word document for .NET and Silverlight. Besides, it supports C#, VB.NET, ASP.NET ASP.NET MVC and Silverlight as well as Word 97, Word 2003, Word 2007 and Word 2010.
In my prrocedure, I need to use Spire.Doc and Visual Studio to help me realize the task.

First, create the Word Styles project.
Open Visual Studio (2008 or later)—File—New Project—Visual C#—Windows Forms Application. Name Windows Forms Application Word Styles (if you need to create a Visual Basic project, Windows Forms Application, you can name the project Word Styles)—OK.

Second, add a button.
After building project Word Styles in Form 1, you need to add a button in Form1.

Third, add references.
Open Solution ExplorerClick project Add ReferenceFind "C:\Program Files\e-iceblue\Spire.Doc"the default folder which you installed the Spire Doc in )— Double-click the folder Bin.
If the target framework of the project
       Net 2.0, double-click folder NET2.0  
      or Net 3.5, double-click folder NET3.5
         or Net 4.0, double-click folder NET4.0
 Select assembly Spire Doc dll Click OK

Fourth, view the method.
After you create the project Word Styles, double-click “button1”, you can see the cold view and the following method has been added automatically:
C#
private void button1_Click(object sender, EventArgs e)

VB.NET    
Private Sub button1_Click(ByVal sender As Object,  ByVal e As EventArgs)  Handles button1.Click



Fifth, add the following codes to the top of the file:
C#
using Spire.Doc;
using Spire.Doc.Documents;
   
VB.NET    
Imports Spire.Doc
Imports
Spire.Doc.Documents



Sixth, add the following codes to the method.
C#
private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    //Create a new secition
    Section section = document.AddSection();

    //Create a new paragraph
    Paragraph paragraph = section.AddParagraph();

    //Append Text
    paragraph.AppendText("Builtin Style:");

    foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
    {
        paragraph = section.AddParagraph();
        //Append Text
        paragraph.AppendText(builtinStyle.ToString());
        //Apply Style
        paragraph.ApplyStyle(builtinStyle);
    }

    //Save doc file.
    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");


}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}





VB.NET    
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
        'Create word document
        Dim document_Renamed As New Document()

        'Create a new secition
        Dim section_Renamed As Section = document_Renamed.AddSection()

        'Create a new paragraph
        Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()

        'Append Text
        paragraph_Renamed.AppendText("Builtin Style:")

        For Each builtinStyle_Renamed As BuiltinStyle In System.Enum.GetValues(GetType(BuiltinStyle))
               paragraph_Renamed = section_Renamed.AddParagraph()
               'Append Text
               paragraph_Renamed.AppendText(builtinStyle_Renamed.ToString())
               'Apply Style
               paragraph_Renamed.ApplyStyle(builtinStyle_Renamed)
        Next builtinStyle_Renamed

        'Save doc file.
        document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

        'Launching the MS Word file.
        WordDocViewer("Sample.doc")


End Sub

Private Sub WordDocViewer(ByVal fileName As String)
        Try
               Process.Start(fileName)
        Catch
        End Try
End Sub



Finally, operate the project Word Styles.
In Solution Explorer, right-click the project Word StylesClick DebugStart new instance, you can see the opened window Form1Click button 1, you can see picture 7.1 shows itself:






Word Styles offer people diverse palette options which work with the main design. Through the setup of word styles, you can quickly and easily apply a set of formatting choices consistently in your document. Word Styles contains four big types: paragraph, character, list, and table. Paragraph styles are mostly used in people’s daily work. If we can make good use of paragraph styles, we may feel amazing about how much stressful work we escape when we work in a long document. Furthermore, our document will look more professional in a whole sight. In fact, the less work you do, the better your document will be! This is what word styles bring to us. But if you cannot use the word styles well, you will have a lot of troubles. So if you want to know more about the function of word styles in Spire. Doc, you can visite the Word Forum.


Source

No comments:

Post a Comment