コメントは、コンテンツの横に表示されるドキュメント要素です。内容には変更を加えずに、アドバイスやレビュー、フィードバックなどを行うために使用されます。また、既存のコメントに返信することも可能で、作者と読者の交流手段としても利用できます。ただし、議論を閉じたコメントについては、閲覧の妨げにならないよう、削除することが望ましいです。本記事では、Free Spire.Doc for .NETを使用して、Word文書にコメントを追加、返信、削除する方法について説明します。
【依存関係の追加】
まず、Free Spire.Doc for.NETパッケージに含まれるDLLファイルを.NETプロジェクトにリファレンスとして追加する必要があります。DLLファイルは、このリンクからダウンロードするか、NuGet経由でインストールすることができます。
PM> Install-Package Spire.Doc
Word文書内の段落にコメントを追加する
Spire.Doc for .NETでは、特定の段落にコメントを追加するための Paragraph.AppendComment() メソッドが用意されています。以下は、その詳細な手順です。
- Document のオブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。
- Document.Section[] プロパティで1つ目のセクションを取得します。
- Section.Paragraph[] プロパティでセクションの2番目の段落を取得します。
- Paragraph.AppendComment() メソッドを使用して、コメントを追加します。
- Document.SaveToFile() メソッドを使用してドキュメントを保存します。
C#
using System;
using Document = Spire.Doc.Document;
using Section = Spire.Doc.Section;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddComment
{
class program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("中世の育児.docx");
//ドキュメントの最初のセクションを取得する
Section section = document.Sections[0];
//セクションの2つの段落を取得する
Paragraph paragraph = section.Paragraphs[1];
//段落にコメントを追加する
Comment comment = paragraph.AppendComment("この段落は、記事の導入部分です。");
comment.Format.Author = "飯塚 有紀";
//ドキュメントを保存する
document.SaveToFile("段落へのコメントの追加.docx");
}
}
}
VB.NET
Imports System
Imports Document = Spire.Doc.Document
Imports Section = Spire.Doc.Section
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Pdf.Exporting.XPS.Schema
Imports System.Data
Namespace AddComment
Class program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("中世の育児.docx")
'ドキュメントの最初のセクションを取得する
Dim section As Section = document.Sections(0)
'セクションの2つの段落を取得する
Dim paragraph As Paragraph = section.Paragraphs(1)
'段落にコメントを追加する
Dim comment As Comment = paragraph.AppendComment("この段落は、記事の導入部分です。")
comment.Format.Author = "飯塚 有紀"
'ドキュメントを保存する
document.SaveToFile("段落へのコメントの追加.docx")
End Sub
End Class
End Namespace
特定のテキストにコメントを追加する
Paragraph.AppendComment() メソッドを使用すると、段落全体にコメントを追加することができます。デフォルトでは、コメントマークは段落の末尾に配置されます。特定のテキストにコメントを挿入するカスタムメソッド InsertComment() を作成することができます。以下は、その詳細な手順です。
- Document のオブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- カスタムメソッド InsertComments() を使用して、ドキュメントにコメントを挿入します。
- Document.SaveToFile() メソッドを使用してドキュメントを保存します。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ConsoleApp11
{
internal class Program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("中世の育児.docx");
//カスタマイズしたメソッドを使用して、Wordドキュメントにコメントを追加する
InsertComments(document, "はしか");
//ドキュメントを保存する
document.SaveToFile("コメントの追加.docx", FileFormat.Docx);
document.Close();
}
private static void InsertComments(Document doc, string keystring)
{
//コメントを付けるテキストを探す
TextSelection find = doc.FindString(keystring, false, true);
//Comment クラスのオブジェクトを作成する
Comment comment = new Comment(doc);
//コメントのテキストを設定する
comment.Body.AddParagraph().Text = "はしかは、小児に最も多い急性呼吸器感染症のひとつで、感染力が非常に強い病気です。";
//コメントの作成者を設定する
comment.Format.Author = "大森 嘉";
//コメントを付けたいテキストがある段落を取得する
TextRange range = find.GetAsOneRange();
Paragraph para = range.OwnerParagraph;
//段落にコメントを追加する
para.ChildObjects.Add(comment);
//コメントの開始と終了のマーカーを作成する
CommentMark commentMarkStart = new CommentMark(doc, CommentMarkType.CommentStart);
CommentMark commentMarkEnd = new CommentMark(doc, CommentMarkType.CommentEnd);
commentMarkStart.CommentId = comment.Format.CommentId;
commentMarkEnd.CommentId = comment.Format.CommentId;
//段落に開始マークと終了マークを挿入して、コメントの開始位置と終了位置を設定する
int index = para.ChildObjects.IndexOf(range);
para.ChildObjects.Insert(index, commentMarkStart);
para.ChildObjects.Insert(index + 2, commentMarkEnd);
}
}
}
VB.NET
Imports System.Data
Imports System.Reflection.Metadata
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Pdf
Imports Spire.Pdf.Exporting.XPS.Schema
Namespace ConsoleApp11
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("中世の育児.docx")
'カスタマイズしたメソッドを使用して、Wordドキュメントにコメントを追加する
InsertComments(document, "はしか")
'ドキュメントを保存する
document.SaveToFile("コメントの追加.docx", FileFormat.DOCX)
document.Close()
End Sub
Private Shared Sub InsertComments(ByVal doc As Document, ByVal keystring As String)
'コメントを付けるテキストを探す
Dim find As TextSelection = doc.FindString(keystring, False, True)
'Comment クラスのオブジェクトを作成する
Dim comment As Comment = New Comment(doc)
'コメントのテキストを設定する
comment.Body.AddParagraph().Text = "はしかは、小児に最も多い急性呼吸器感染症のひとつで、感染力が非常に強い病気です。"
'コメントの作成者を設定する
comment.Format.Author = "大森 嘉"
'コメントを付けたいテキストがある段落を取得する
Dim range As TextRange = find.GetAsOneRange()
Dim para As Paragraph = range.OwnerParagraph
'段落にコメントを追加する
para.ChildObjects.Add(comment)
'コメントの開始と終了のマーカーを作成する
Dim commentMarkStart As CommentMark = New CommentMark(doc, CommentMarkType.CommentStart)
Dim commentMarkEnd As CommentMark = New CommentMark(doc, CommentMarkType.CommentEnd)
commentMarkStart.CommentId = comment.Format.CommentId
commentMarkEnd.CommentId = comment.Format.CommentId
'段落に開始マークと終了マークを挿入して、コメントの開始位置と終了位置を設定する
Dim index As Integer = para.ChildObjects.IndexOf(range)
para.ChildObjects.Insert(index, commentMarkStart)
para.ChildObjects.Insert(index + 2, commentMarkEnd)
End Sub
End Class
End Namespace
Word文書のコメントに返信する
既存のコメントに返信を追加するには、Comment.ReplyToComment() メソッドを使用します。以下は、その詳細な手順です。
- Document のオブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- Document.Comments[] プロパティを使って、ドキュメント内の最初のコメントを取得します。
- Comment オブジェクトを作成し、Comment クラスのメソッドで著者とコメントのテキストを設定します。
- Comment.ReplyToComment() メソッドを使用して、新しいコメントを最初のコメントへの返信として設定します。
- Document.SaveToFile() メソッドを使用してドキュメントを保存します。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace ConsoleApp12
{
internal class Program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("コメントの追加.docx");
//最初のコメントを取得する
Comment comment = document.Comments[0];
//Commentオブジェクトを作成し、コメント作成者を設定し、コメントのテキストを追加する
Comment replyComment = new Comment(document);
replyComment.Format.Author = "岩崎 叶詠";
Paragraph paragraph = replyComment.Body.AddParagraph();
paragraph.AppendText("はしかは、予防接種が行き届いていない人口密集地で流行しています。");
//このコメントを最初のコメントへの返信として設定する
comment.ReplyToComment(replyComment);
//ドキュメントを保存する
document.SaveToFile("コメントへの返信.docx", FileFormat.Docx);
document.Close();
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Pdf
Imports Spire.Pdf.Exporting.XPS.Schema
Imports System
Imports System.Data
Imports System.Reflection.Metadata
Namespace ConsoleApp12
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("コメントの追加.docx")
'最初のコメントを取得する
Dim comment As Comment = document.Comments(0)
'Commentオブジェクトを作成し、コメント作成者を設定し、コメントのテキストを追加する
Dim replyComment As Comment = New Comment(document)
replyComment.Format.Author = "岩崎 叶詠"
Dim paragraph As Paragraph = replyComment.Body.AddParagraph()
paragraph.AppendText("はしかは、予防接種が行き届いていない人口密集地で流行しています。")
'このコメントを最初のコメントへの返信として設定する
comment.ReplyToComment(replyComment)
'ドキュメントを保存する
document.SaveToFile("コメントへの返信.docx", FileFormat.Docx)
document.Close()
End Sub
End Class
End Namespace
Word文書からコメントを削除する
Spire.Doc for .NETでは、Word文書から特定のコメントを削除する Document.Comments.RemoveAt() メソッドとWord文書からすべてのコメントを削除する Document.Comments.Clear() メソッドが提供されています。以下は、その詳細な手順です。
- Document のオブジェクトを作成します。
- Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
- Document.Comments.RemoveAt() メソッドまたは Document.Comments.Clear() メソッドで、文書中の特定のコメントまたはすべてのコメントを削除します。
- Document.SaveToFile() メソッドを使用してドキュメントを保存します。
C#
using Spire.Doc;
namespace ConsoleApp13
{
internal class Program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.LoadFromFile("コメントへの返信.docx");
//すべてのコメントを削除する
document.Comments.Clear();
//指定したコメントを削除する
//document.Comments.RemoveAt(1);
//ドキュメントを保存する
document.SaveToFile("コメントの削除.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports System.Reflection.Metadata
Imports Spire.Doc
Imports Spire.Pdf
Namespace ConsoleApp13
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim document As Document = New Document()
'Wordドキュメントを読み込む
document.LoadFromFile("コメントへの返信.docx")
'すべてのコメントを削除する
document.Comments.Clear()
'指定したコメントを削除する
'document.Comments.RemoveAt(1);
'ドキュメントを保存する
document.SaveToFile("コメントの削除.docx", FileFormat.Docx)
End Sub
End Class
End Namespace