Spire.Doc

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

This program guide focus on helping you start with a simple demo, which shows how to edit Word document, by using Spire.Doc for Silverlight. At the beginning, make sure that Spire.Doc for Silverlight, Visual Studio and Microsoft Silverlight are installed on your computer.

1. Create Silverlight Project - Click File to Add New Project in Visual Studio. Then, select Visual C# or Visual Basic. Choose Silverlight. Name this project. In this example, we name it Editing-Doc-SL. After Clicking OK, a dialog box pops up, set if host Silverlight application in a new Web site and select your Silverlight Version. Click OK to finish creating.

2. Add Reference - Right click reference to add reference. Choose appropriate Spire.Doc dll version from your computer. If your Silverlight Version is Silverlight 4, please choose dll file in Silverlight4 folder from Spire.Doc Bin folder.

3. Design MainPage - Double click MainPage.xaml and then design UserControl. Firstly, add a label in UserControl and change its contents. Also, set label contents format, including font style and color. Next, add a button for running demo.

[MainPage.xaml]
<UserControl x:Class="Editing_Doc_SL.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="239" d:DesignWidth="479">

    <Grid x:Name="LayoutRoot" Background="#B7A6B5B4" Height="243" Width="484" Loaded="LayoutRoot_Loaded">
        <Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="369,189,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBlock Height="41" HorizontalAlignment="Left" Margin="18,82,0,0" Name="textBlock1" Text="This sample demonstrates how to use Spire.Doc for Silverlight" VerticalAlignment="Top" Width="454" TextWrapping="NoWrap" FontSize="14" FontStyle="Italic" Foreground="#FFFC0000" />
    </Grid>
</UserControl>    

4. Declare Document and SaveFileDialog - Declare a new Word document and a saveFileDialog to save Word document. In this saveFileDialog, you also need to set a filter for selecting Word document version. In this document, I set Word document extension as .doc only.

[MainPage.xaml.cs]
        private SaveFileDialog saveFileDg = null;
        private Document document = new Document();

        public MainPage()
        {
            InitializeComponent();
            this.saveFileDg = new SaveFileDialog();
            this.saveFileDg.Filter = "Word Document(*.doc)|*.doc";
        }
[MainPage.xaml.vb]
        Private saveFileDg As SaveFileDialog = Nothing
        Private document As New Document()

        Public Sub New()
            InitializeComponent()
            Me.saveFileDg = New SaveFileDialog()
            Me.saveFileDg.Filter = "Word Document(*.doc)|*.doc"
        End Sub

5. Load Document - Because we cannot use document.LoadFromFile() method directly in Silverlight, so you need to add document in project which you want to load. Right click project name and add existed item from computer. Then, click this document to set its Build Action as Embedded Resource as following picture.

Double click Gird (LayoutRoot) and then find Loaded from the right corner Properties window. Double click it and then load the embedded document. Firstly, get document name from assembly. If the name is the embedded document name, save this document to stream and load document from stream.

[MainPage.xaml.cs]
        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //Load Document
            Assembly assembly = this.GetType().Assembly;
            foreach (string name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith("Editing.doc"))
                {
                    using (Stream stream = assembly.GetManifestResourceStream(name))
                    {
                        this.document.LoadFromStream(stream, FileFormat.Doc);
                    }
                }
            }
        }
[MainPage.xaml.vb]
        Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Load Document
            Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
            For Each name As String In [assembly].GetManifestResourceNames()
                If name.EndsWith("Editing.doc") Then
                    Using stream As Stream = [assembly].GetManifestResourceStream(name)
                        Me.document.LoadFromStream(stream, FileFormat.Doc)
                    End Using
                End If
            Next name
        End Sub

6. Edit Document - Double click button and write code. Firstly, add a new paragraph in section one of this loaded document. Then, append text for this document. Text I add in paragraph is "Editing Sample".

[MainPage.xaml.cs]
            Paragraph paragraph = this.document.Sections[0].AddParagraph();
            paragraph.AppendText("Editing sample");
[MainPage.xaml.vb]
            Dim paragraph As Paragraph = Me.document.Sections(0).AddParagraph()
            paragraph.AppendText("Editing sample")

7. Save Document - Judge if the saveFileDialog which I declare in step 4 can pop up after clicking Run button. If yes, save document by using save file dialog.

[MainPage.xaml.cs]
            bool? result = this.saveFileDg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDg.OpenFile())
                {
                    this.document.SaveToStream(stream, FileFormat.Doc);
                }
            }
[MainPage.xaml.vb]
            Dim result? As Boolean = Me.saveFileDg.ShowDialog()
            If result.HasValue AndAlso result.Value Then
                Using stream As Stream = Me.saveFileDg.OpenFile()
                    Me.document.SaveToStream(stream, FileFormat.Doc)
                End Using
            End If
[Full MainPage.xaml.cs]
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Editing_Doc_SL
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFileDg = null;
        private Document document = new Document();

        public MainPage()
        {
            InitializeComponent();
            this.saveFileDg = new SaveFileDialog();
            this.saveFileDg.Filter = "Word Document(*.doc)|*.doc";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Add Paragraph
            Paragraph paragraph = this.document.Sections[0].AddParagraph();
            paragraph.AppendText("Editing sample");

            //Save Document
            bool? result = this.saveFileDg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFileDg.OpenFile())
                {
                    this.document.SaveToStream(stream, FileFormat.Doc);
                }
            }
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            //Load Document
            Assembly assembly = this.GetType().Assembly;
            foreach (string name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith("Editing.doc"))
                {
                    using (Stream stream = assembly.GetManifestResourceStream(name))
                    {
                        this.document.LoadFromStream(stream, FileFormat.Doc);
                    }
                }
            }
        }
    }
}
[Full MainPage.xaml.vb]
Imports System.Windows
Imports System.Windows.Controls
Imports System.IO
Imports System.Reflection
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Editing_Doc_SL
    Partial Public Class MainPage
        Inherits UserControl
        Private saveFileDg As SaveFileDialog = Nothing
        Private document As New Document()

        Public Sub New()
            InitializeComponent()
            Me.saveFileDg = New SaveFileDialog()
            Me.saveFileDg.Filter = "Word Document(*.doc)|*.doc"
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Add Paragraph
            Dim paragraph As Paragraph = Me.document.Sections(0).AddParagraph()
            paragraph.AppendText("Editing sample")

            'Save Document
            Dim result? As Boolean = Me.saveFileDg.ShowDialog()
            If result.HasValue AndAlso result.Value Then
                Using stream As Stream = Me.saveFileDg.OpenFile()
                    Me.document.SaveToStream(stream, FileFormat.Doc)
                End Using
            End If
        End Sub

        Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Load Document
            Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
            For Each name As String In [assembly].GetManifestResourceNames()
                If name.EndsWith("Editing.doc") Then
                    Using stream As Stream = [assembly].GetManifestResourceStream(name)
                        Me.document.LoadFromStream(stream, FileFormat.Doc)
                    End Using
                End If
            Next name
        End Sub
    End Class
End Namespace

Result Screenshot

Friday, 04 May 2012 02:16

Add Page Borders for Word in C#, VB.NET

Written by support iceblue

Word Page Border is one part of page background to beautify document appearance. Solution in this guide introduces how to insert and format Word page border in C# and VB.NET.

MS Word enables users to decorate Word pages to have a better layout or appearance. All the formats of page borders can be customized, including style, color, width, margin etc. This guide demonstrates how to add page borders for Word document and format them in C# and VB.NET via Spire.Doc for .NET. The following screenshot presents Word with page borders after programming.

Word Page Borders

Spire.Doc for .NET offers Section class to allow you to create new section instance or manipulate existing section. Borders is one property of PageSetup of Section class. You can directly set BorderType property of Borders to add page borders and set Color, Space of Left/Right properties to format borders. The border types Spire.Doc for .NET offers include None, Single, Thick, Double, Hairline, Dot, DashLargeGap, DotDash, DotDotDash, Triple, ThinkThickSmallGap, ThinThinSampllGap, ThinThickThinSamllGap, ThinThickMediumGap, ThickThinMediumGap, ThickThickThinMediumGap, ThinThickLargeGap, ThickThinLargeGap, ThinThickThinLargeGap, Wave, DoubleWave, DashSamllGap, DashDotStroker, Emboss3D, Engrave3D, Outset, Inset, TwistedLines1, Cleared. In this example, the border type is set as DoubleWave.

Download and install Spire.Doc for .NET and follow the code:

[C#]
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordBorder
{
    class PageBorders
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Microsoft Word 2013 Preview.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave;
            section.PageSetup.Borders.Color = Color.LightSeaGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;

            //Save and Launch
            document.SaveToFile("PageBorders.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("PageBorders.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBorder
    Friend Class PageBorders
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Microsoft Word 2013 Preview.docx")
            Dim section As Section = document.Sections(0)

            'Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave
            section.PageSetup.Borders.Color = Color.LightSeaGreen
            'Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50
            section.PageSetup.Borders.Right.Space = 50

            'Save and Launch
            document.SaveToFile("PageBorders.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("PageBorders.docx")
        End Sub
    End Class
End Namespace

Spire.Doc is a stand-alone component, enabling developers/programmers to generate, open, write, edit and save Word document in WPF, .NET and Silverlight without Microsoft Word installed on system.