Sommario
Installato tramite NuGet
PM> Install-Package Spire.Doc
Link correlati
La funzionalità di commento in Microsoft Word offre alle persone un modo eccellente per aggiungere i propri approfondimenti o opinioni a un documento di Word senza dover modificare o interrompere il contenuto del documento. Se qualcuno commenta un documento, l'autore del documento o altri utenti possono rispondere al commento per discutere con lui, anche se non stanno visualizzando il documento contemporaneamente. Questo articolo mostrerà come farlo aggiungere, rispondere o eliminare commenti in Word in C# e VB.NET utilizzando la libreria Spire.Doc for .NET.
- Aggiungi un commento al paragrafo in Word in C# e VB.NET
- Aggiungi un commento al testo in Word in C# e VB.NET
- Rispondi a un commento in Word in C# e VB.NET
- Elimina commenti in Word in C# e VB.NET
Installa Spire.Doc for .NET
Per cominciare, devi aggiungere i file DLL inclusi nel pacchetto Spire.Doc per.NET come riferimenti nel tuo progetto .NET. I file DLL possono essere scaricati da questo link o installato tramite NuGet.
PM> Install-Package Spire.Doc
Aggiungi un commento al paragrafo in Word in C# e VB.NET
Spire.Doc for .NET fornisce il metodo Paragraph.AppendComment() per aggiungere un commento a un paragrafo specifico. Di seguito sono riportati i passaggi dettagliati:
- Inizializza un'istanza della classe Document.
- Carica un documento Word utilizzando il metodo Document.LoadFromFile().
- Accedi a una sezione specifica del documento tramite il suo indice tramite la proprietà Document.Sections[int].
- Accedi a un paragrafo specifico nella sezione tramite il suo indice tramite la proprietà Sezione.Paragraphs[int].
- Aggiungi un commento al paragrafo utilizzando il metodo Paragraph.AppendComment().
- Imposta l'autore del commento tramite la proprietà Comment.Format.Author.
- Salvare il documento risultante utilizzando il metodo Document.SaveToFile().
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddComments
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"Sample.docx");
//Get the first section in the document
Section section = document.Sections[0];
//Get the first paragraph in the section
Paragraph paragraph = section.Paragraphs[0];
//Add a comment to the paragraph
Comment comment = paragraph.AppendComment("This comment is added using Spire.Doc for .NET.");
//Set comment author
comment.Format.Author = "Eiceblue";
comment.Format.Initial = "CM";
//Save the result document
document.SaveToFile("AddCommentToParagraph.docx", FileFormat.Docx2013);
document.Close();
}
}
}

Aggiungi un commento al testo in Word in C# e VB.NET
Il metodo Paragraph.AppendComment() viene utilizzato per aggiungere commenti a un intero paragrafo. Per impostazione predefinita, i contrassegni di commento verranno posizionati alla fine del paragrafo. Per aggiungere un commento a un testo specifico, è necessario cercare il testo utilizzando il metodo Document.FindString(), quindi posizionare i contrassegni di commento all'inizio e alla fine del testo. Di seguito sono riportati i passaggi dettagliati:
- Inizializza un'istanza della classe Document.
- Carica un documento Word utilizzando il metodo Document.LoadFromFile().
- Trova il testo specifico nel documento utilizzando il metodo Document.FindString().
- Crea un segno di inizio commento e un segno di fine commento, che verranno posizionati rispettivamente all'inizio e alla fine del testo trovato.
- Inizializza un'istanza della classe Comment per creare un nuovo commento. Quindi imposta il contenuto e l'autore del commento.
- Ottieni il paragrafo proprietario del testo trovato. Quindi aggiungi il commento al paragrafo come oggetto figlio.
- Inserisci il segno di inizio commento prima dell'intervallo di testo e il segno di fine commento dopo l'intervallo di testo.
- Salvare il documento risultante utilizzando il metodo Document.SaveToFile().
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AddCommentsToText
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"CommentTemplate.docx");
//Find a specific string
TextSelection find = document.FindString("Microsoft Office", false, true);
//Create the comment start mark and comment end mark
CommentMark commentmarkStart = new CommentMark(document);
commentmarkStart.Type = CommentMarkType.CommentStart;
CommentMark commentmarkEnd = new CommentMark(document);
commentmarkEnd.Type = CommentMarkType.CommentEnd;
//Create a comment and set its content and author
Comment comment = new Comment(document);
comment.Body.AddParagraph().Text = "Developed by Microsoft.";
comment.Format.Author = "Shaun";
//Get the found text as a single text range
TextRange range = find.GetAsOneRange();
//Get the owner paragraph of the text range
Paragraph para = range.OwnerParagraph;
//Add the comment to the paragraph
para.ChildObjects.Add(comment);
//Get the index of text range in the paragraph
int index = para.ChildObjects.IndexOf(range);
//Insert the comment start mark before the text range
para.ChildObjects.Insert(index, commentmarkStart);
//Insert the comment end mark after the text range
para.ChildObjects.Insert(index + 2, commentmarkEnd);
//Save the result document
document.SaveToFile("AddCommentForText.docx", FileFormat.Docx2013);
document.Close();
}
}
}

Rispondi a un commento in Word in C# e VB.NET
Per aggiungere una risposta a un commento esistente, puoi utilizzare il metodo Comment.ReplyToComment(). Di seguito sono riportati i passaggi dettagliati:
- Inizializza un'istanza della classe Document.
- Carica un documento Word utilizzando il metodo Document.LoadFromFile().
- Ottieni un commento specifico nel documento tramite la proprietà Document.Comments[int].
- Inizializza un'istanza della classe Comment per creare un nuovo commento. Quindi imposta il contenuto e l'autore del commento.
- Aggiungi il nuovo commento come risposta al commento specifico utilizzando il metodo Comment.ReplyToComment().
- Salvare il documento risultante utilizzando il metodo Document.SaveToFile().
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
namespace ReplyToComments
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"AddCommentToParagraph.docx");
//Get the first comment in the document
Comment comment1 = document.Comments[0];
//Create a new comment and specify its author and content
Comment replyComment1 = new Comment(document);
replyComment1.Format.Author = "Michael";
replyComment1.Body.AddParagraph().AppendText("Spire.Doc is a wonderful Word library.");
//Add the comment as a reply to the first comment
comment1.ReplyToComment(replyComment1);
//Save the result document
document.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
document.Close();
}
}
}

Elimina commenti in Word in C# e VB.NET
Spire.Doc for .NET offre il metodo Document.Comments.RemoveAt(int) per rimuovere un commento specifico da un documento Word e il metodo Document.Comments.Clear() per rimuovere tutti i commenti da un documento Word. Di seguito sono riportati i passaggi dettagliati:
- Inizializza un'istanza della classe Document.
- Carica un documento Word utilizzando il metodo Document.LoadFromFile().
- Elimina un commento specifico o tutti i commenti nel documento utilizzando il metodo Document.Comments.RemoveAt(int) o Document.Comments.Clear().
- Salvare il documento risultante utilizzando il metodo Document.SaveToFile().
- C#
- VB.NET
using Spire.Doc;
namespace DeleteComments
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile(@"AddCommentToParagraph.docx");
//Delete the first comment in the document
document.Comments.RemoveAt(0);
//Delete all comments in the document
//document.Comments.Clear();
//Save the result document
document.SaveToFile("DeleteComment.docx", FileFormat.Docx2013);
document.Close();
}
}
}

Richiedi una licenza temporanea
Se desideri rimuovere il messaggio di valutazione dai documenti generati o eliminare le limitazioni della funzione, per favore richiedere una licenza di prova di 30 giorni per te.