Context (14)
The sample demonstrates how to Create Table in Word for Silverlight via Spire.Doc.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Table_doc.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace Table_doc
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Windows
Namespace Table_doc
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="Table_doc.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="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="306,253,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Drawing;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Table_doc
{
public partial class MainPage : UserControl
{
private SaveFileDialog saveFileDialog = null;
public MainPage()
{
InitializeComponent();
this.saveFileDialog = new SaveFileDialog();
this.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//create a blank word document
Document document = new Document();
//add one section
Section section = document.AddSection();
//add one paragraph
Paragraph paragraph = section.AddParagraph();
TextRange txtRange = paragraph.AppendText("This is a sample demonstrates how to create table using Spire.Doc\n\n");
txtRange.CharacterFormat.Font.Bold = true;
txtRange.CharacterFormat.Font.Italic = true;
txtRange.CharacterFormat.TextColor = Color.Red;
//add one table
AddTable(section);
//save the word document
bool? result = this.saveFileDialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFileDialog.OpenFile())
{
document.SaveToStream(stream, FileFormat.Doc);
}
}
}
private void AddTable(Section section)
{
//prepare the data of table
String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
String[][] data =
{
new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
};
//create the table and set the cells
Spire.Doc.Table table = section.AddTable();
table.ResetCells(data.Length + 1, header.Length);
//edit the header row
TableRow row = table.Rows[0];
row.IsHeader = true;
row.Height = 20;
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < header.Length; i++)
{
row.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
Paragraph p = row.Cells[i].AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange txtRange = p.AppendText(header[i]);
txtRange.CharacterFormat.Bold = true;
}
//edit the data rows
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Bisque;
for (int c = 0; c < data[r].Length; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
}
}
//add the borders
table.TableFormat.Borders.BorderType = BorderStyle.Thick;
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Drawing
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace Table_doc
Partial Public Class MainPage
Inherits UserControl
Private saveFileDialog As SaveFileDialog = Nothing
Public Sub New()
InitializeComponent()
Me.saveFileDialog = New SaveFileDialog()
Me.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'create a blank word document
Dim document As New Document()
'add one section
Dim section As Section = document.AddSection()
'add one paragraph
Dim paragraph As Paragraph = section.AddParagraph()
Dim txtRange As TextRange = paragraph.AppendText("This is a sample demonstrates how to create table using Spire.Doc" & vbLf & vbLf)
txtRange.CharacterFormat.Font.Bold = True
txtRange.CharacterFormat.Font.Italic = True
txtRange.CharacterFormat.TextColor = Color.Red
'add one table
AddTable(section)
'save the word document
Dim result? As Boolean = Me.saveFileDialog.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using stream As Stream = Me.saveFileDialog.OpenFile()
document.SaveToStream(stream, FileFormat.Doc)
End Using
End If
End Sub
Private Sub AddTable(ByVal section As Section)
'prepare the data of table
Dim header() As String = { "Name", "Capital", "Continent", "Area", "Population" }
Dim data()() As String = { New String(){"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, New String(){"Bolivia", "La Paz", "South America", "1098575", "7300000"}, New String(){"Brazil", "Brasilia", "South America", "8511196", "150400000"}, New String(){"Canada", "Ottawa", "North America", "9976147", "26500000"}, New String(){"Chile", "Santiago", "South America", "756943", "13200000"}, New String(){"Colombia", "Bagota", "South America", "1138907", "33000000"}, New String(){"Cuba", "Havana", "North America", "114524", "10600000"}, New String(){"Ecuador", "Quito", "South America", "455502", "10600000"}, New String(){"El Salvador", "San Salvador", "North America", "20865", "5300000"}, New String(){"Guyana", "Georgetown", "South America", "214969", "800000"}, New String(){"Jamaica", "Kingston", "North America", "11424", "2500000"}, New String(){"Mexico", "Mexico City", "North America", "1967180", "88600000"}, New String(){"Nicaragua", "Managua", "North America", "139000", "3900000"}, New String(){"Paraguay", "Asuncion", "South America", "406576", "4660000"} }
'create the table and set the cells
Dim table As Spire.Doc.Table = section.AddTable()
table.ResetCells(data.Length + 1, header.Length)
'edit the header row
Dim row As TableRow = table.Rows(0)
row.IsHeader = True
row.Height = 20
row.HeightType = TableRowHeightType.Exactly
row.RowFormat.BackColor = Color.Gray
For i As Integer = 0 To header.Length - 1
row.Cells(i).CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle
Dim p As Paragraph = row.Cells(i).AddParagraph()
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
Dim txtRange As TextRange = p.AppendText(header(i))
txtRange.CharacterFormat.Bold = True
Next i
'edit the data rows
For r As Integer = 0 To data.Length - 1
Dim dataRow As TableRow = table.Rows(r + 1)
dataRow.Height = 20
dataRow.HeightType = TableRowHeightType.Exactly
dataRow.RowFormat.BackColor = Color.Bisque
For c As Integer = 0 To data(r).Length - 1
dataRow.Cells(c).CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle
dataRow.Cells(c).AddParagraph().AppendText(data(r)(c))
Next c
Next r
'add the borders
table.TableFormat.Borders.BorderType = BorderStyle.Thick
End Sub
End Class
End Namespace
The sample demonstrates how to add bookmark into Word for Silverlight via Spire.Doc.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Bookmark_Doc.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace Bookmark_Doc
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Windows
Namespace Bookmark_Doc
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="Bookmark_Doc.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="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="RUN" Height="23" HorizontalAlignment="Left" Margin="286,265,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Bookmark_Doc
{
public partial class MainPage : UserControl
{
private SaveFileDialog saveFile = null;
public MainPage()
{
InitializeComponent();
this.saveFile = new SaveFileDialog();
this.saveFile.Filter = "Word Document(*.doc)|*.doc";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//create a word document
Document document = new Document();
//add one section
Section section = document.AddSection();
AddBookmark(section);
//save word document
bool? result = this.saveFile.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFile.OpenFile())
{
document.SaveToStream(stream, FileFormat.Docx);
}
}
}
private void AddBookmark(Section section)
{
Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
paragraph.AppendText("This is a sample demonstrates how to use bookmark in Spire.Doc");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//writing simple bookmarks
paragraph = section.AddParagraph();
paragraph.AppendText("Simple Bookmark1");
paragraph.ApplyStyle(BuiltinStyle.Heading4);
paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("Simple_Bookmark1");
paragraph.AppendText(" This is the first simple bookmark");
paragraph.AppendBookmarkEnd("Simple_Bookmark1");
paragraph = section.AddParagraph();
paragraph.AppendText("Simple Bookmark2");
paragraph.ApplyStyle(BuiltinStyle.Heading4);
paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("Simple_Bookmark2");
paragraph.AppendText(" This is the second simple bookmark\n");
paragraph.AppendBookmarkEnd("Simple_Bookmark2");
//writing nested bookmarks
paragraph = section.AddParagraph();
paragraph.AppendText("Nested bookmarks");
paragraph.ApplyStyle(BuiltinStyle.Heading4);
paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("Root");
paragraph.AppendText(" Root data ");
paragraph.AppendBookmarkStart("NestedLevel1");
paragraph.AppendText(" Nested Level1 ");
paragraph.AppendBookmarkStart("NestedLevel2");
paragraph.AppendText(" Nested Level2 ");
paragraph.AppendBookmarkEnd("NestedLevel2");
paragraph.AppendText(" Data Level1 ");
paragraph.AppendBookmarkEnd("NestedLevel1");
paragraph.AppendText(" Data Root ");
paragraph.AppendBookmarkEnd("Root");
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace Bookmark_Doc
Partial Public Class MainPage
Inherits UserControl
Private saveFile As SaveFileDialog = Nothing
Public Sub New()
InitializeComponent()
Me.saveFile = New SaveFileDialog()
Me.saveFile.Filter = "Word Document(*.doc)|*.doc"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'create a word document
Dim document As New Document()
'add one section
Dim section As Section = document.AddSection()
AddBookmark(section)
'save word document
Dim result? As Boolean = Me.saveFile.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using stream As Stream = Me.saveFile.OpenFile()
document.SaveToStream(stream, FileFormat.Docx)
End Using
End If
End Sub
Private Sub AddBookmark(ByVal section As Section)
Dim paragraph As Paragraph = If(section.Paragraphs.Count > 0, section.Paragraphs(0), section.AddParagraph())
paragraph.AppendText("This is a sample demonstrates how to use bookmark in Spire.Doc")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
'writing simple bookmarks
paragraph = section.AddParagraph()
paragraph.AppendText("Simple Bookmark1")
paragraph.ApplyStyle(BuiltinStyle.Heading4)
paragraph = section.AddParagraph()
paragraph.AppendBookmarkStart("Simple_Bookmark1")
paragraph.AppendText(" This is the first simple bookmark")
paragraph.AppendBookmarkEnd("Simple_Bookmark1")
paragraph = section.AddParagraph()
paragraph.AppendText("Simple Bookmark2")
paragraph.ApplyStyle(BuiltinStyle.Heading4)
paragraph = section.AddParagraph()
paragraph.AppendBookmarkStart("Simple_Bookmark2")
paragraph.AppendText(" This is the second simple bookmark" & vbLf)
paragraph.AppendBookmarkEnd("Simple_Bookmark2")
'writing nested bookmarks
paragraph = section.AddParagraph()
paragraph.AppendText("Nested bookmarks")
paragraph.ApplyStyle(BuiltinStyle.Heading4)
paragraph = section.AddParagraph()
paragraph.AppendBookmarkStart("Root")
paragraph.AppendText(" Root data ")
paragraph.AppendBookmarkStart("NestedLevel1")
paragraph.AppendText(" Nested Level1 ")
paragraph.AppendBookmarkStart("NestedLevel2")
paragraph.AppendText(" Nested Level2 ")
paragraph.AppendBookmarkEnd("NestedLevel2")
paragraph.AppendText(" Data Level1 ")
paragraph.AppendBookmarkEnd("NestedLevel1")
paragraph.AppendText(" Data Root ")
paragraph.AppendBookmarkEnd("Root")
End Sub
End Class
End Namespace
The sample demonstrates how to insert comments into Word for Silverlight via Spire.Doc.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Comments_Doc.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace Comments_Doc
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Windows
Namespace Comments_Doc
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="Comments_Doc.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="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<Button Content="RUN" Height="23" HorizontalAlignment="Left" Margin="290,266,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.IO;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
namespace Comments_Doc
{
public partial class MainPage : UserControl
{
private Document document = new Document();
private SaveFileDialog saveFileDialog = null;
public MainPage()
{
InitializeComponent();
this.saveFileDialog = new SaveFileDialog();
this.saveFileDialog.Filter = "Word Document(*.doc)|*.doc";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Section section = this.document.Sections[0];
//insert comment
InsertComment(section);
//save the doc document using a SaveFileDialog
bool? result = this.saveFileDialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFileDialog.OpenFile())
{
this.document.SaveToStream(stream, FileFormat.Doc);
}
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
Assembly assembly = this.GetType().Assembly;
foreach (string name in assembly.GetManifestResourceNames())
{
if (name.EndsWith("Blank.doc"))
{
using (Stream fileStr = assembly.GetManifestResourceStream(name))
{
this.document.LoadFromStream(fileStr, FileFormat.Doc);
}
}
}
}
private void InsertComment(Section section)
{
//title
Paragraph paragraph
= section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
TextRange title = paragraph.AppendText("Spire.office Component");
title.CharacterFormat.Bold = true;
title.CharacterFormat.FontName = "Arial";
title.CharacterFormat.FontSize = 14;
paragraph.Format.HorizontalAlignment
= Spire.Doc.Documents.HorizontalAlignment.Center;
paragraph.Format.AfterSpacing = 10;
//style
ParagraphStyle style1 = new ParagraphStyle(section.Document);
style1.Name = "style1";
style1.CharacterFormat.FontName = "Arial";
style1.CharacterFormat.FontSize = 9;
style1.ParagraphFormat.LineSpacing = 1.5F * 12F;
style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
section.Document.Styles.Add(style1);
ParagraphStyle style2 = new ParagraphStyle(section.Document);
style2.Name = "style2";
style2.ApplyBaseStyle(style1.Name);
section.Document.Styles.Add(style2);
paragraph = section.AddParagraph();
paragraph.AppendText("Spire.Office for .NET is a compilation of every .NET component offered by");
TextRange text = paragraph.AppendText("e-iceblue");
//Comment e-iceblue, adding url for it.
Comment comment1 = paragraph.AppendComment("http://www.e-iceblue.com/");
comment1.AddItem(text);
comment1.Format.Author = "Suvi Wu";
comment1.Format.Initial = "HH";
paragraph.AppendText(". It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications.");
paragraph.ApplyStyle(style1.Name);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Reflection
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Fields
Imports Spire.Doc.Documents
Namespace Comments_Doc
Partial Public Class MainPage
Inherits UserControl
Private document As New Document()
Private saveFileDialog As SaveFileDialog = Nothing
Public Sub New()
InitializeComponent()
Me.saveFileDialog = New SaveFileDialog()
Me.saveFileDialog.Filter = "Word Document(*.doc)|*.doc"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim section As Section = Me.document.Sections(0)
'insert comment
InsertComment(section)
'save the doc document using a SaveFileDialog
Dim result? As Boolean = Me.saveFileDialog.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using stream As Stream = Me.saveFileDialog.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)
Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
For Each name As String In [assembly].GetManifestResourceNames()
If name.EndsWith("Blank.doc") Then
Using fileStr As Stream = [assembly].GetManifestResourceStream(name)
Me.document.LoadFromStream(fileStr, FileFormat.Doc)
End Using
End If
Next name
End Sub
Private Sub InsertComment(ByVal section As Section)
'title
Dim paragraph As Paragraph = If(section.Paragraphs.Count > 0, section.Paragraphs(0), section.AddParagraph())
Dim title As TextRange = paragraph.AppendText("Spire.office Component")
title.CharacterFormat.Bold = True
title.CharacterFormat.FontName = "Arial"
title.CharacterFormat.FontSize = 14
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
paragraph.Format.AfterSpacing = 10
'style
Dim style1 As New ParagraphStyle(section.Document)
style1.Name = "style1"
style1.CharacterFormat.FontName = "Arial"
style1.CharacterFormat.FontSize = 9
style1.ParagraphFormat.LineSpacing = 1.5F * 12F
style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
section.Document.Styles.Add(style1)
Dim style2 As New ParagraphStyle(section.Document)
style2.Name = "style2"
style2.ApplyBaseStyle(style1.Name)
section.Document.Styles.Add(style2)
paragraph = section.AddParagraph()
paragraph.AppendText("Spire.Office for .NET is a compilation of every .NET component offered by")
Dim text As TextRange = paragraph.AppendText("e-iceblue")
'Comment e-iceblue, adding url for it.
Dim comment1 As Comment = paragraph.AppendComment("http://www.e-iceblue.com/")
comment1.AddItem(text)
comment1.Format.Author = "Suvi Wu"
comment1.Format.Initial = "HH"
paragraph.AppendText(". It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications.")
paragraph.ApplyStyle(style1.Name)
End Sub
End Class
End Namespace
The sample demonstrates how to Insert Image into Word in Silverlight.

<pplication xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="doctest.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace doctest
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Windows
Namespace doctest
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="doctest.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="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="296,244,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;
namespace doctest
{
public partial class MainPage : UserControl
{
private SaveFileDialog saveFileDialog = null;
public MainPage()
{
InitializeComponent();
this.saveFileDialog = new SaveFileDialog();
this.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc";
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//load the doc template
Assembly assembly = this.GetType().Assembly;
Document doc = new Document();
foreach (string name in assembly.GetManifestResourceNames())
{
if (name.EndsWith("test.doc"))
{
using (Stream fileStr = assembly.GetManifestResourceStream(name))
{
doc.LoadFromStream(fileStr, FileFormat.Doc);
}
}
}
//get the second paragraph of the first section in doc
Section section = doc.Sections[0];
Paragraph paragraph = section.Paragraphs[2];
//get the picture
Stream imageStr = Application.GetResourceStream(new Uri("/doctest;component/sunset.jpg", UriKind.Relative)).Stream;
//insert the picture
paragraph.AppendPicture(imageStr);
//save the doc template
bool? result = this.saveFileDialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream str = this.saveFileDialog.OpenFile())
{
doc.SaveToStream(str, FileFormat.Doc);
}
}
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.IO
Imports System.Reflection
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace doctest
Partial Public Class MainPage
Inherits UserControl
Private saveFileDialog As SaveFileDialog = Nothing
Public Sub New()
InitializeComponent()
Me.saveFileDialog = New SaveFileDialog()
Me.saveFileDialog.Filter = "Word Documents(*.doc)|*.doc"
End Sub
Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'load the doc template
Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
Dim doc As New Document()
For Each name As String In [assembly].GetManifestResourceNames()
If name.EndsWith("test.doc") Then
Using fileStr As Stream = [assembly].GetManifestResourceStream(name)
doc.LoadFromStream(fileStr, FileFormat.Doc)
End Using
End If
Next name
'get the second paragraph of the first section in doc
Dim section As Section = doc.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(2)
'get the picture
Dim imageStr As Stream = Application.GetResourceStream(New Uri("/doctest;component/sunset.jpg", UriKind.Relative)).Stream
'insert the picture
paragraph.AppendPicture(imageStr)
'save the doc template
Dim result? As Boolean = Me.saveFileDialog.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using str As Stream = Me.saveFileDialog.OpenFile()
doc.SaveToStream(str, FileFormat.Doc)
End Using
End If
End Sub
End Class
End Namespace
The sample demonstrates how to create Word Footer in Silverlight via Spire.Doc.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Schedule_2_6.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace Schedule_2_6
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Windows
Namespace Schedule_2_6
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="Schedule_2_6.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="300" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="White" Width="480" Loaded="LayoutRoot_Loaded">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*" />
<ColumnDefinition Width="0*" />
<ColumnDefinition Width="480*" />
</Grid.ColumnDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="236,194,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="2" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,86,0,0" Name="textBox1" VerticalAlignment="Top" Width="456" Text="This sample demonstrates how to add footer into doc document using Spire.Doc" Grid.ColumnSpan="2" Grid.Column="1" />
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Schedule_2_6
{
public partial class MainPage : UserControl
{
private SaveFileDialog saveFiledialog = new SaveFileDialog();
private Document document = null;
public MainPage()
{
InitializeComponent();
this.saveFiledialog.Filter = "Word Document (*.docx)|*.docx";
this.document = new Document();
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
//load the template document to insert footer by a stream
Assembly assembly = this.GetType().Assembly;
foreach (String name in assembly.GetManifestResourceNames())
{
if (name.EndsWith("iceblue.docx"))
{
using (Stream stream = assembly.GetManifestResourceStream(name))
{
this.document.LoadFromStream(stream, FileFormat.Docx);
}
}
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (Section section in document.Sections)
{
//insert footer
InsertFooter(section);
}
//save the document using the saveFiledialog
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFiledialog.OpenFile())
{
this.document.SaveToStream(stream, FileFormat.Docx);
}
}
}
private static void InsertFooter(Section section)
{
//add one footer into the section
HeaderFooter footer = section.HeadersFooters.Footer;
//draw the text of footer
Paragraph footerParagraph = footer.AddParagraph();
TextRange text = footerParagraph.AppendText("Demo of Spire.doc");
//set the style of the text
text.CharacterFormat.FontName = "Arial";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.Italic = true;
footerParagraph.Format.HorizontalAlignment
= Spire.Doc.Documents.HorizontalAlignment.Right;
//draw the top line of footer
footerParagraph.Format.Borders.Top.BorderType
= Spire.Doc.Documents.BorderStyle.Single;
footerParagraph.Format.Borders.Top.Space = 0.05f;
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Reflection
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace Schedule_2_6
Partial Public Class MainPage
Inherits UserControl
Private saveFiledialog As New SaveFileDialog()
Private document As Document = Nothing
Public Sub New()
InitializeComponent()
Me.saveFiledialog.Filter = "Word Document (*.docx)|*.docx"
Me.document = New Document()
End Sub
Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
'load the template document to insert footer by a stream
Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
For Each name As String In [assembly].GetManifestResourceNames()
If name.EndsWith("iceblue.docx") Then
Using stream As Stream = [assembly].GetManifestResourceStream(name)
Me.document.LoadFromStream(stream, FileFormat.Docx)
End Using
End If
Next name
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
For Each section As Section In document.Sections
'insert footer
InsertFooter(section)
Next section
'save the document using the saveFiledialog
Dim result? As Boolean = Me.saveFiledialog.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using stream As Stream = Me.saveFiledialog.OpenFile()
Me.document.SaveToStream(stream, FileFormat.Docx)
End Using
End If
End Sub
Private Shared Sub InsertFooter(ByVal section As Section)
'add one footer into the section
Dim footer As HeaderFooter = section.HeadersFooters.Footer
'draw the text of footer
Dim footerParagraph As Paragraph = footer.AddParagraph()
Dim text As TextRange = footerParagraph.AppendText("Demo of Spire.doc")
'set the style of the text
text.CharacterFormat.FontName = "Arial"
text.CharacterFormat.FontSize = 10
text.CharacterFormat.Italic = True
footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
'draw the top line of footer
footerParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single
footerParagraph.Format.Borders.Top.Space = 0.05f
End Sub
End Class
End Namespace
The sample demonstrates how to add Word header in Silverlight via Spire.Doc.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AddHeader_Doc.App">
<Application.Resources>
</Application.Resources>
</Application>
using System;
using System.Windows;
namespace AddHeader_Doc
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}
private void Application_Exit(object sender, EventArgs e)
{
}
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
// If the app is running outside of the debugger then report the exception using
// the browser's exception mechanism. On IE this will display it a yellow alert
// icon in the status bar and Firefox will display a script error.
if (!System.Diagnostics.Debugger.IsAttached)
{
// NOTE: This will allow the application to continue running after an exception has been thrown
// but not handled.
// For production applications this error handling should be replaced with something that will
// report the error to the website and stop the application.
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
}
}
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}
}
}
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Namespace AddHeader_Doc
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Me.Startup, AddressOf Application_Startup
AddHandler Me.Exit, AddressOf Application_Exit
AddHandler Me.UnhandledException, AddressOf Application_UnhandledException
InitializeComponent()
End Sub
Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
Me.RootVisual = New MainPage()
End Sub
Private Sub Application_Exit(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs)
' If the app is running outside of the debugger then report the exception using
' the browser's exception mechanism. On IE this will display it a yellow alert
' icon in the status bar and Firefox will display a script error.
If Not Debugger.IsAttached Then
' NOTE: This will allow the application to continue running after an exception has been thrown
' but not handled.
' For production applications this error handling should be replaced with something that will
' report the error to the website and stop the application.
e.Handled = True
Deployment.Current.Dispatcher.BeginInvoke(Sub() ReportErrorToDOM(e))
End If
End Sub
Private Sub ReportErrorToDOM(ByVal e As ApplicationUnhandledExceptionEventArgs)
Try
Dim errorMsg As String = e.ExceptionObject.Message + e.ExceptionObject.StackTrace
errorMsg = errorMsg.Replace(""""c, "'"c).Replace(vbCrLf, vbLf)
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(""Unhandled Error in Silverlight Application " & errorMsg & """);")
Catch e1 As Exception
End Try
End Sub
End Class
End Namespace
<UserControl x:Class="AddHeader_Doc.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="219" d:DesignWidth="397" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="#FFD7E1FF" Width="397" Height="219" Loaded="LayoutRoot_Loaded">
<sdk:Label Height="24" HorizontalAlignment="Left" Margin="12,64,0,130" Name="label1" VerticalAlignment="Center" Width="373" Content="This sample demonstrates how to add header in a doc file" FontSize="11" FontWeight="Bold" />
<Button Content="Run" Height="23" HorizontalAlignment="Left" Margin="260,161,0,0" Name="buttonRun" VerticalAlignment="Top" Width="75" Click="buttonRun_Click" />
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddHeader_Doc
{
public partial class MainPage : UserControl
{
private SaveFileDialog saveFiledialog = new SaveFileDialog();
private Document document = null;
public MainPage()
{
InitializeComponent();
this.saveFiledialog.Filter = "Word Document (*.doc)|*.doc";
this.document = new Document();
}
private void buttonRun_Click(object sender, RoutedEventArgs e)
{
//add a header in document
Section section = this.document.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;
//draw the text of header
Paragraph headerParagraph = section.Paragraphs[0];
TextRange text = headerParagraph.AppendText("Demo of Spire.doc");
//set the style of the text
text.CharacterFormat.FontName = "Arial";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.Italic = true;
headerParagraph.Format.HorizontalAlignment
= Spire.Doc.Documents.HorizontalAlignment.Right;
//draw the bottom line of header
headerParagraph.Format.Borders.Bottom.BorderType
= Spire.Doc.Documents.BorderStyle.Single;
headerParagraph.Format.Borders.Bottom.Space = 0.05f;
//save the document using the saveDiledialog
bool? result = this.saveFiledialog.ShowDialog();
if (result.HasValue && result.Value)
{
using (Stream stream = this.saveFiledialog.OpenFile())
{
this.document.SaveToStream(stream, FileFormat.Doc);
}
}
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
//load the sample2.doc file through stream
Assembly assembly = this.GetType().Assembly;
foreach (String name in assembly.GetManifestResourceNames())
{
if (name.EndsWith("sample2.doc"))
{
using (Stream docStream = assembly.GetManifestResourceStream(name))
{
this.document = new Document(docStream, FileFormat.Doc);
}
}
}
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.IO
Imports System.Reflection
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace AddHeader_Doc
Partial Public Class MainPage
Inherits UserControl
Private saveFiledialog As New SaveFileDialog()
Private document As Document = Nothing
Public Sub New()
InitializeComponent()
Me.saveFiledialog.Filter = "Word Document (*.doc)|*.doc"
Me.document = New Document()
End Sub
Private Sub buttonRun_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
'add a header in document
Dim section As Section = Me.document.Sections(0)
Dim header As HeaderFooter = section.HeadersFooters.Header
'draw the text of header
Dim headerParagraph As Paragraph = section.Paragraphs(0)
Dim text As TextRange = headerParagraph.AppendText("Demo of Spire.doc")
'set the style of the text
text.CharacterFormat.FontName = "Arial"
text.CharacterFormat.FontSize = 10
text.CharacterFormat.Italic = True
headerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
'draw the bottom line of header
headerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single
headerParagraph.Format.Borders.Bottom.Space = 0.05f
'save the document using the saveDiledialog
Dim result? As Boolean = Me.saveFiledialog.ShowDialog()
If result.HasValue AndAlso result.Value Then
Using stream As Stream = Me.saveFiledialog.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 the sample2.doc file through stream
Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
For Each name As String In [assembly].GetManifestResourceNames()
If name.EndsWith("sample2.doc") Then
Using docStream As Stream = [assembly].GetManifestResourceStream(name)
Me.document = New Document(docStream, FileFormat.Doc)
End Using
End If
Next name
End Sub
End Class
End Namespace
The sample demonstrates how to create table in word document.

//Create word document
Document document = new Document();
Section section = document.AddSection();
String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
String[][] data =
{
new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
};
Spire.Doc.Table table = section.AddTable();
table.ResetCells(data.Length + 1, header.Length);
// ***************** First Row *************************
TableRow row = table.Rows[0];
row.IsHeader = true;
row.Height = 20; //unit: point, 1point = 0.3528 mm
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < header.Length; i++)
{
row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = row.Cells[i].AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange txtRange = p.AppendText(header[i]);
txtRange.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
dataRow.Height = 20;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = 0; c < data[r].Length; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
}
}
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
'Create word document
Dim document_Renamed As New Document()
Dim section As Section = document_Renamed.AddSection()
Dim header As String() = {"Name", "Capital", "Continent", "Area", "Population"}
Dim data As String()() = { _
New String() {"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, _
New String() {"Bolivia", "La Paz", "South America", "1098575", "7300000"}, _
New String() {"Brazil", "Brasilia", "South America", "8511196", "150400000"}, _
New String() {"Canada", "Ottawa", "North America", "9976147", "26500000"}, _
New String() {"Chile", "Santiago", "South America", "756943", "13200000"}, _
New String() {"Colombia", "Bagota", "South America", "1138907", "33000000"}, _
New String() {"Cuba", "Havana", "North America", "114524", "10600000"}, _
New String() {"Ecuador", "Quito", "South America", "455502", "10600000"}, _
New String() {"El Salvador", "San Salvador", "North America", "20865", "5300000"}, _
New String() {"Guyana", "Georgetown", "South America", "214969", "800000"}, _
New String() {"Jamaica", "Kingston", "North America", "11424", "2500000"}, _
New String() {"Mexico", "Mexico City", "North America", "1967180", "88600000"}, _
New String() {"Nicaragua", "Managua", "North America", "139000", "3900000"}, _
New String() {"Paraguay", "Asuncion", "South America", "406576", "4660000"}, _
New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _
New String() {"United States of America", "Washington", "North America", "9363130", "249200000"}, _
New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _
New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _
}
Dim table As Spire.Doc.Table = section.AddTable()
table.ResetCells(data.Length + 1, header.Length)
' ***************** First Row *************************
Dim row As TableRow = table.Rows(0)
row.IsHeader = True
row.Height = 20 'unit: point, 1point = 0.3528 mm
row.HeightType = TableRowHeightType.Exactly
row.RowFormat.BackColor = Color.Gray
For i As Integer = 0 To header.Length - 1
row.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle
Dim p As Paragraph = row.Cells(i).AddParagraph()
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
Dim txtRange As TextRange = p.AppendText(header(i))
txtRange.CharacterFormat.Bold = True
Next
For r As Integer = 0 To data.Length - 1
Dim dataRow As TableRow = table.Rows(r + 1)
dataRow.Height = 20
dataRow.HeightType = TableRowHeightType.Exactly
dataRow.RowFormat.BackColor = Color.Empty
For c As Integer = 0 To data(r).Length - 1
dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle
dataRow.Cells(c).AddParagraph().AppendText(data(r)(c))
Next
Next
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
The sample demonstrates how to insert watermark into Word document.

private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertWatermark(document);
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertWatermark(Document document)
{
Paragraph paragraph = document.AddSection().AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print documents without utilizing Microsoft Word.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a document.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print documents without utilizing Microsoft Word.");
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Watermark Demo";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'Create word document
Dim document_Renamed As New Document()
InsertWatermark(document_Renamed)
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertWatermark(ByVal document_Renamed As Document)
Dim paragraph_Renamed As Paragraph = document_Renamed.AddSection().AddParagraph()
paragraph_Renamed.AppendText("The sample demonstrates how to insert a watermark into a document.")
paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)
Dim txtWatermark As New TextWatermark()
txtWatermark.Text = "Watermark Demo"
txtWatermark.FontSize = 90
txtWatermark.Layout = WatermarkLayout.Diagonal
document_Renamed.Watermark = txtWatermark
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
The sample demonstrates how to insert a textbox into a document.

private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertTextbox(document.AddSection());
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertTextbox(Section section)
{
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a textbox into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
Spire.Doc.Fields.TextBox textBox = paragraph.AppendTextBox(50,20);
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'Create word document
Dim document_Renamed As New Document()
InsertTextbox(document_Renamed.AddSection())
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertTextbox(ByVal section_Renamed As Section)
Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
paragraph_Renamed.AppendText("The sample demonstrates how to insert a textbox into a document.")
paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)
paragraph_Renamed = section_Renamed.AddParagraph()
paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left
Dim textBox As Spire.Doc.Fields.TextBox = paragraph_Renamed.AppendTextBox(50,20)
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub