How to Use Spire.Doc in a WPF Applicaiton

This document aims at clearly introducing a simple “HelloWorld” demo about Spire.Doc for WPF by using Visual Studio. The below procedure will help you realize this task step by step. Before getting started, please make sure that Spire.Doc for WPF and Visual Studio are correctly installed on system.

Step 1. Create a new project.

1. Create a new project by choosing WPF Application in Visual Studio and name the project "HelloWorld". If you want to create a C# project, select Visual C#, WPF Application, if you want to build a Visual Basic project, please choose Visual Basic, WPF Application. The detail process is:

Click File → NewProjectWPF ApplicationNameOK

2. Set the Target framework property of the HelloWorld project in Solution Explorer to be .NET Framework 4.The process is:

Click Solution ExplorerHelloWorld (The project that you already built)Properties(right click HelloWorld)Target framework.NET Framework 4

3. Add a button in MainWindow. The default button name is "button1". You can set button1 Content property to be "Run" in its properties by right clicking it. Thus, it shows "Run" in the MainWindow. You can perform as below:

Click ViewToolboxButtonProperties (right click button1)Contentset the Content to be "Run"

Step 2. Add reference and project namespaces.

1. Add Spire.Doc. Wpf.dll as reference in Project. The Default location of Spire.Doc for WPF is “C:\Program Files\e-iceblue\Spire.Doc for WPF”. Details are:

Click ProjectAdd ReferenceBrowseChoose the folder contains Spire.Doc for WPFBinWPF 4.0Spire.Doc.Wpf.dll

2. Double click the "Run" button, you can see the following method has been added automatically:

[C#]
namespace HelloWorld
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}
[VB.NET]
Namespace HelloWorld
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
                End Sub
	End Class
End Namespace

3. Add the below namespaces at the top of the method.

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Step 3. Write "Hello, World!” in the method.

Add the following code to the method button1_click

[C#]
            //create a new document using spire.Doc
            Document document = new Document();

            //add one paragraph 
            Paragraph paragraph = document.AddSection().AddParagraph();
            paragraph.AppendText("Hello, World!");

            //save the document
            document.SaveToFile(@"..\..\sample.doc", FileFormat.Doc);

            //launch the document
            System.Diagnostics.Process.Start(@"..\..\sample.doc");
[VB.NET]
	  'create a new document using spire.Doc
	   Dim document As New Document()

	  'add one paragraph 
	   Dim paragraph As Paragraph = document.AddSection().AddParagraph()
	   paragraph.AppendText("Hello, World!")

	  'save the document
	   document.SaveToFile("..\..\sample.doc", FileFormat.Doc)

	   'launch the document
	   System.Diagnostics.Process.Start("..\..\sample.doc")

Step 4. Debug the project

Right click the project HelloWorld in Solution ExplorerDebug-> Start new instanceClick Run in MainWindow, a Word Document will be created, edited and opened. The string “Hello, World!” is drawn in the first line of page1.

Preview