- Demo
- App.xaml
- App.xaml.cs
- App.xaml.vb
- MainPage.xaml
- MainPage.xaml.cs
- MainPage.xaml.vb
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
