Werk Met Kommentaar

Aspose.Words laat gebruikers toe om met kommentaar te werk kommentaar in’n dokument in Aspose.Words word verteenwoordig deur die Comment klas. Gebruik ook die CommentRangeStart en CommentRangeEnd klasse om die streek van teks wat met’n kommentaar geassosieer moet word spesifiseer.

Voeg’n Opmerking by

Aspose.Words laat jou toe om kommentaar op verskeie maniere by te voeg:

  1. Gebruik die Comment klas
  2. Gebruik die CommentRangeStart en CommentRangeEnd klasse

Die volgende kode voorbeeld toon hoe om’n kommentaar by te voeg tot’n paragraaf met behulp van die Comment klas:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithComments();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Some text is added.");
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today);
builder.CurrentParagraph.AppendChild(comment);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text."));
dataDir = dataDir + "Comments_out.doc";
// Save the document.
doc.Save(dataDir);

Die volgende kode voorbeeld toon hoe om’n kommentaar by te voeg tot’n paragraaf met behulp van’n streek van teks en die CommentRangeStart en CommentRangeEnd klasse:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithComments();
Document doc = new Document();
Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc, "Some ");
Run run2 = new Run(doc, "text ");
para1.AppendChild(run1);
para1.AppendChild(run2);
doc.FirstSection.Body.AppendChild(para1);
Paragraph para2 = new Paragraph(doc);
Run run3 = new Run(doc, "is ");
Run run4 = new Run(doc, "added ");
para2.AppendChild(run3);
para2.AppendChild(run4);
doc.FirstSection.Body.AppendChild(para2);
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text."));
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id);
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id);
run1.ParentNode.InsertAfter(commentRangeStart, run1);
run3.ParentNode.InsertAfter(commentRangeEnd, run3);
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);
dataDir = dataDir + "Anchor.Comment_out.doc";
// Save the document.
doc.Save(dataDir);

Uittreksel Of Verwyder Kommentaar

Die gebruik van Kommentaar in’n Word-dokument (benewens Spoorveranderings) is’n algemene praktyk wanneer dokumente hersien word, veral wanneer daar verskeie beoordelaars is. Daar kan situasies wees waar die enigste ding wat jy van’n dokument benodig, die kommentaar is. Sê jy wil’n lys van hersieningsbevindinge genereer, of dalk het jy al die nuttige inligting uit die dokument versamel en jy wil eenvoudig onnodige kommentaar verwyder. Miskien wil u die kommentaar van’n spesifieke resensent sien of verwyder.

In hierdie voorbeeld gaan ons kyk na’n paar eenvoudige metodes vir beide die insameling van inligting uit die kommentaar binne’n dokument en vir die verwydering van kommentaar uit’n dokument. Spesifiek sal ons dek hoe om:

  • Pak al die kommentaar uit’n dokument of slegs die wat deur’n spesifieke skrywer gemaak is
  • Verwyder al die kommentaar uit’n dokument of slegs van’n spesifieke skrywer

Hoe Om Kommentaar Te Onttrek Of Te Verwyder

Die kode in hierdie voorbeeld is eintlik redelik eenvoudig en alle metodes is gebaseer op dieselfde benadering. ‘n kommentaar in’n Word dokument word verteenwoordig deur’n Comment voorwerp in die Aspose.Words dokument voorwerp model. Om al die kommentaar in’n dokument te versamel, gebruik die GetChildNodes metode met die eerste parameter wat op NodeType.Comment gestel is. Maak seker dat die tweede parameter van die GetChildNodes metode is ingestel op waar: dit dwing die GetChildNodes om te kies uit alle kind nodes rekursief, eerder as om net die onmiddellike kinders te versamel.

Om te illustreer hoe om kommentaar uit’n dokument te onttrek en te verwyder, sal ons deur die volgende stappe gaan:

  1. Open’n word-dokument met die Document - klas
  2. Kry alle kommentaar van die dokument in’n versameling
  3. Om kommentaar te onttrek:
    1. Gaan deur die versameling met behulp van die foreach operateur
    2. Uittreksel en lys die outeur se naam, datum en tyd en teks van alle kommentaar
    3. Uittreksel en lys die outeur se naam, datum en tyd en teks van kommentaar wat deur’n spesifieke outeur geskryf is, in hierdie geval die outeur’ks’
  4. Om kommentaar te verwyder:
    1. Gaan agteruit deur die versameling met behulp van die for operateur
    2. Verwyder kommentaar
  5. Stoor die veranderinge

Hoe Om Alle Kommentaar Te Onttrek

Die GetChildNodes metode is baie nuttig en jy kan dit gebruik elke keer as jy’n lys van dokumentknope van enige tipe moet kry. Die gevolglike versameling skep nie’n onmiddellike oorhoofse koste nie, want die nodusse word slegs in hierdie versameling gekies wanneer u items daarin opsom of toegang daartoe verkry.

Die volgende kode voorbeeld toon hoe om die outeur naam, datum en tyd en teks van alle kommentaar in die dokument te onttrek:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
static ArrayList ExtractComments(Document doc)
{
ArrayList collectedComments = new ArrayList();
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and gather information about them.
foreach (Comment comment in comments)
{
collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text));
}
return collectedComments;
}

Hoe Om Kommentaar Van’n Gespesifiseerde Skrywer Te Onttrek

Nadat jy Comment nodusse in’n versameling gekies het, hoef jy net die inligting wat jy nodig het, te onttrek. In hierdie voorbeeld word die outeur se voorletters, datum, tyd en die gewone teks van die kommentaar in een string gekombineer; jy kan kies om dit op ander maniere te stoor.

Die oorbelaaide metode wat Die Kommentaar van’n spesifieke skrywer onttrek, is amper dieselfde, dit kontroleer net die skrywer se naam voordat die inligting by die skikking gevoeg word.

Die volgende kode voorbeeld toon hoe om die outeur naam, datum en tyd en teks van die kommentaar deur die gespesifiseerde outeur te onttrek:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
static ArrayList ExtractComments(Document doc, string authorName)
{
ArrayList collectedComments = new ArrayList();
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and gather information about those written by the authorName author.
foreach (Comment comment in comments)
{
if (comment.Author == authorName)
collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text));
}
return collectedComments;
}

Hoe Om Kommentaar Te Verwyder

As u alle kommentaar verwyder, hoef u nie een vir een deur die versameling te beweeg nie. U kan dit verwyder deur die Clear - metode op die kommentaarversameling te skakel.

Die volgende kode voorbeeld toon hoe om alle kommentaar in die dokument te verwyder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
static void RemoveComments(Document doc)
{
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Remove all comments.
comments.Clear();
}

Wanneer jy kommentaar selektief moet verwyder, word die proses meer soortgelyk aan die kode wat ons gebruik het vir kommentaar onttrekking.

Die volgende kode voorbeeld toon hoe om kommentaar te verwyder deur die gespesifiseerde skrywer:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
static void RemoveComments(Document doc, string authorName)
{
// Collect all comments in the document
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
// Look through all comments and remove those written by the authorName author.
for (int i = comments.Count - 1; i >= 0; i--)
{
Comment comment = (Comment)comments[i];
if (comment.Author == authorName)
comment.Remove();
}
}

Die belangrikste punt om hier uit te lig is die gebruik van die for operateur. Anders as die eenvoudige onttrekking, hier wil jy’n opmerking uitvee. ‘n gepaste truuk is om die versameling agteruit te herhaal van die laaste Kommentaar na die eerste Een. Die rede hiervoor as jy van die einde af begin en agteruit beweeg, bly die indeks van die voorafgaande items onveranderd, en jy kan jou pad terugwerk na die eerste item in die versameling.

Die volgende kode voorbeeld toon die metodes vir die kommentaar onttrekking en verwydering:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithComments();
string fileName = "TestFile.doc";
// Open the document.
Document doc = new Document(dataDir + fileName);
// Extract the information about the comments of all the authors.
foreach (string comment in ExtractComments(doc))
Console.Write(comment);
// Remove comments by the "pm" author.
RemoveComments(doc, "pm");
Console.WriteLine("Comments from \"pm\" are removed!");
// Extract the information about the comments of the "ks" author.
foreach (string comment in ExtractComments(doc, "ks"))
Console.Write(comment);
//Read the comment's reply and resolve them.
CommentResolvedandReplies(doc);
// Remove all comments.
RemoveComments(doc);
Console.WriteLine("All comments are removed!");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the document.
doc.Save(dataDir);

Hoe Om’n Kommentaar Tussen CommentRangeStart En CommentRangeEndTe Verwyder

Met behulp van Aspose.Words kan jy ook kommentaar tussen die CommentRangeStart en CommentRangeEnd nodusse verwyder.

Die volgende kode voorbeeld toon hoe om teks tussen CommentRangeStart en CommentRangeEndte verwyder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithComments();
string fileName = "TestFile.doc";
// Open the document.
Document doc = new Document(dataDir + fileName);
CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true);
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true);
Node currentNode = commentStart;
Boolean isRemoving = true;
while (currentNode != null && isRemoving)
{
if (currentNode.NodeType == NodeType.CommentRangeEnd)
isRemoving = false;
Node nextNode = currentNode.NextPreOrder(doc);
currentNode.Remove();
currentNode = nextNode;
}
dataDir = dataDir + "RemoveRegionText_out.doc";
// Save the document.
doc.Save(dataDir);

Voeg By Of Verwyder Kommentaar Se Antwoord

Die AddReply metode voeg’n antwoord by hierdie opmerking. Let asseblief daarop dat as gevolg van die bestaande Microsoft Office beperkings slegs 1 vlak van antwoorde in die dokument toegelaat word. ‘n uitsondering van tipe InvalidOperationException sal geopper word indien hierdie metode op die bestaande Antwoord kommentaar genoem word.

U kan die RemoveReply - metode gebruik om die gespesifiseerde antwoord op hierdie opmerking te verwyder.

Die volgende kode voorbeeld toon hoe om’n antwoord by te voeg tot kommentaar en verwyder kommentaar se antwoord:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "TestFile.doc");
Comment comment = (Comment)doc.GetChild(NodeType.Comment, 0, true);
//Remove the reply
comment.RemoveReply(comment.Replies[0]);
//Add a reply to comment
comment.AddReply("John Doe", "JD", new DateTime(2017, 9, 25, 12, 15, 0), "New reply");
dataDir = dataDir + "TestFile_Out.doc";
// Save the document to disk.
doc.Save(dataDir);

Lees Kommentaar Se Antwoord

Die Replies eiendom gee’n versameling van die Comment voorwerpe wat onmiddellike kinders van die gespesifiseerde kommentaar.

Die volgende kode voorbeeld toon hoe om te iterate deur middel van’n kommentaar se antwoorde en opgelos hulle:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
static void CommentResolvedandReplies(Document doc)
{
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
Comment parentComment = (Comment)comments[0];
foreach (Comment childComment in parentComment.Replies)
{
// Get comment parent and status.
Console.WriteLine(childComment.Ancestor.Id);
Console.WriteLine(childComment.Done);
// And update comment Done mark.
childComment.Done = true;
}
}