Praca z komentarzami

Aspose.Words umożliwia użytkownikom pracę z komentarzami-komentarze w dokumencie w Aspose.Words są reprezentowane przez klasę Comment. Użyj również klas CommentRangeStart i CommentRangeEnd, aby określić region tekstu, który powinien być powiązany z komentarzem.

Dodaj komentarz

Aspose.Words umożliwia dodawanie komentarzy na kilka sposobów:

  1. Używanie klasy Comment
  2. Korzystanie z klas CommentRangeStart i CommentRangeEnd

Poniższy przykład kodu pokazuje, jak dodać komentarz do akapitu za pomocą klasy Comment:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Write(u"Some text is added.");
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
builder->get_CurrentParagraph()->AppendChild(comment);
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::String outputPath = outputDataDir + u"AddComments.doc";
// Save the document.
doc->Save(outputPath);

Poniższy przykład kodu pokazuje, jak dodać komentarz do akapitu przy użyciu regionu tekstu oraz klas CommentRangeStart i CommentRangeEnd:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<Paragraph> para1 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run1 = System::MakeObject<Run>(doc, u"Some ");
System::SharedPtr<Run> run2 = System::MakeObject<Run>(doc, u"text ");
para1->AppendChild(run1);
para1->AppendChild(run2);
doc->get_FirstSection()->get_Body()->AppendChild(para1);
System::SharedPtr<Paragraph> para2 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run3 = System::MakeObject<Run>(doc, u"is ");
System::SharedPtr<Run> run4 = System::MakeObject<Run>(doc, u"added ");
para2->AppendChild(run3);
para2->AppendChild(run4);
doc->get_FirstSection()->get_Body()->AppendChild(para2);
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::SharedPtr<CommentRangeStart> commentRangeStart = System::MakeObject<CommentRangeStart>(doc, comment->get_Id());
System::SharedPtr<CommentRangeEnd> commentRangeEnd = System::MakeObject<CommentRangeEnd>(doc, comment->get_Id());
run1->get_ParentNode()->InsertAfter(commentRangeStart, run1);
run3->get_ParentNode()->InsertAfter(commentRangeEnd, run3);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);
System::String outputPath = outputDataDir + u"AnchorComment.doc";
// Save the document.
doc->Save(outputPath);

Wyodrębnij lub Usuń Komentarze

Używanie komentarzy w dokumencie Word (oprócz śledzenia zmian) jest powszechną praktyką podczas przeglądania dokumentów, szczególnie gdy jest wielu recenzentów. Mogą wystąpić sytuacje, w których jedyne, czego potrzebujesz z dokumentu, to Komentarze. Załóżmy, że chcesz wygenerować listę wyników przeglądu, a może zebrałeś wszystkie przydatne informacje z dokumentu i po prostu chcesz usunąć niepotrzebne komentarze. Możesz chcieć wyświetlić lub usunąć komentarze konkretnego recenzenta.

W tym przykładzie przyjrzymy się kilku prostym metodom zarówno zbierania informacji z komentarzy w dokumencie, jak i usuwania komentarzy z dokumentu. W szczególności omówimy, jak:

  • Wyodrębnij wszystkie komentarze z dokumentu lub tylko te zgłoszone przez konkretnego autora.
  • Usuń wszystkie komentarze z dokumentu lub tylko od konkretnego autora.

Jak wyodrębnić lub usunąć komentarze

Kod w tej próbce jest w rzeczywistości dość prosty i wszystkie metody opierają się na tym samym podejściu. Komentarz w dokumencie Word jest reprezentowany przez obiekt Comment w modelu obiektowym dokumentu Aspose.Words. Aby zebrać wszystkie komentarze w dokumencie, użyj metody GetChildNodes z pierwszym parametrem ustawionym na NodeType.Comment. Upewnij się, że drugi parametr metody GetChildNodes jest ustawiony na true: zmusza to GetChildNodes do rekurencyjnego wybierania spośród wszystkich węzłów potomnych, a nie tylko zbierania bezpośrednich elementów potomnych.

Aby zilustrować, jak wyodrębnić i usunąć komentarze z dokumentu, wykonamy następujące kroki:

  1. Otwórz dokument Word przy użyciu klasy Document
  2. Pobierz wszystkie komentarze z dokumentu do kolekcji
  3. Aby wyodrębnić komentarze:
    1. Przejrzyj kolekcję za pomocą operatora foreach
    2. Wyodrębnij i wymień nazwisko autora, datę i godzinę oraz tekst wszystkich komentarzy
    3. Wyodrębnij i wymień nazwisko autora, datę i godzinę oraz tekst komentarzy napisanych przez konkretnego autora, w tym przypadku autora ks
  4. Aby usunąć komentarze:
    1. Przejdź wstecz przez kolekcję za pomocą dla operatora
    2. Usuń komentarze
  5. Zapisz zmiany

Jak wyodrębnić wszystkie komentarze

Metoda GetChildNodes jest bardzo przydatna i można jej używać za każdym razem, gdy trzeba uzyskać listę węzłów dokumentów dowolnego typu. Wynikowa kolekcja nie tworzy natychmiastowego narzutu, ponieważ węzły są wybierane do tej kolekcji tylko wtedy, gdy wyliczysz lub uzyskasz dostęp do elementów w niej.

Poniższy przykład kodu pokazuje, jak wyodrębnić nazwisko autora, datę i godzinę oraz tekst wszystkich komentarzy w dokumencie:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about them.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
return collectedComments;
}

Jak wyodrębnić Komentarze określonego autora

Po wybraniu węzłów komentarzy do kolekcji wystarczy wyodrębnić potrzebne informacje. W tym przykładzie Inicjały autora, data, godzina i zwykły tekst komentarza są łączone w jeden ciąg; zamiast tego możesz zapisać go na inne sposoby.

Przeciążona metoda, która wyodrębnia komentarze od konkretnego autora, jest prawie taka sama, po prostu sprawdza nazwisko autora przed dodaniem informacji do tablicy.

Poniższy przykład kodu pokazuje, jak wyodrębnić nazwisko autora, datę i godzinę oraz tekst komentarzy określonego autora:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about those written by the authorName author.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
if (comment->get_Author() == authorName)
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
}
return collectedComments;
}

Jak usunąć komentarze

Jeśli usuwasz wszystkie komentarze, nie ma potrzeby poruszania się po kolekcji, usuwając komentarze jeden po drugim; możesz je usunąć, dzwoniąc do NodeCollection.Clear w kolekcji komentarzy.

Poniższy przykład kodu pokazuje, jak usunąć wszystkie komentarze w dokumencie:

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

Kiedy trzeba selektywnie usuwać komentarze, proces staje się bardziej podobny do kodu, którego użyliśmy do wyodrębnienia komentarzy.

Poniższy przykład kodu pokazuje, jak usunąć komentarze określonego autora:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void RemoveComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and remove those written by the authorName author.
for (int32_t i = comments->get_Count() - 1; i >= 0; i--)
{
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(i));
if (comment->get_Author() == authorName)
{
comment->Remove();
}
}
}

Głównym punktem, który należy tutaj podkreślić, jest użycie operatora for. W przeciwieństwie do prostej ekstrakcji, tutaj chcesz usunąć komentarz. Odpowiednią sztuczką jest iteracja kolekcji wstecz od ostatniego komentarza do pierwszego. Powód tego, jeśli zaczniesz od końca i cofniesz się, indeks poprzednich pozycji pozostanie niezmieniony i możesz wrócić do pierwszego elementu w kolekcji.

Poniższy przykład kodu pokazuje metody wyodrębniania i usuwania komentarzy:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
// Extract the information about the comments of all the authors.
for (System::String const &comment : ExtractComments(doc))
{
std::cout << comment.ToUtf8String();
}
// Remove comments by the "pm" author.
RemoveComments(doc, u"pm");
std::cout << "Comments from \"pm\" are removed!" << std::endl;
// Extract the information about the comments of the "ks" author.
for (System::String const &comment: ExtractComments(doc, u"ks"))
{
std::cout << comment.ToUtf8String();
}
//Read the comment's reply and resolve them.
CommentResolvedandReplies(doc);
// Remove all comments.
RemoveComments(doc);
std::cout << "All comments are removed!" << std::endl;
System::String outputPath = outputDataDir + u"ProcessComments.doc";
// Save the document.
doc->Save(outputPath);

Jak usunąć komentarz między CommentRangeStart a CommentRangeEnd

Za pomocą Aspose.Words możesz także usuwać komentarze między węzłami CommentRangeStart i CommentRangeEnd.

Poniższy przykład kodu pokazuje, jak usunąć tekst między CommentRangeStart a CommentRangeEnd:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<CommentRangeStart> commentStart = System::DynamicCast<CommentRangeStart>(doc->GetChild(NodeType::CommentRangeStart, 0, true));
System::SharedPtr<CommentRangeEnd> commentEnd = System::DynamicCast<CommentRangeEnd>(doc->GetChild(NodeType::CommentRangeEnd, 0, true));
System::SharedPtr<Node> currentNode = commentStart;
bool isRemoving = true;
while (currentNode != nullptr && isRemoving)
{
if (currentNode->get_NodeType() == NodeType::CommentRangeEnd)
{
isRemoving = false;
}
System::SharedPtr<Node> nextNode = currentNode->NextPreOrder(doc);
currentNode->Remove();
currentNode = nextNode;
}
System::String outputPath = outputDataDir + u"RemoveRegionText.doc";
// Save the document.
doc->Save(outputPath);

Dodaj i usuń odpowiedź komentarza

Metoda AddReply dodaje odpowiedź do tego komentarza. Należy pamiętać, że ze względu na istniejące ograniczenia Microsoft Office tylko 1 poziom odpowiedzi jest dozwolony w dokumencie. Wyjątek typu InvalidOperationException zostanie podniesiony, jeśli ta metoda zostanie wywołana w istniejącym komentarzu odpowiedzi.

Możesz użyć metody RemoveReply, aby usunąć określoną odpowiedź na ten komentarz.

Poniższy przykład kodu pokazuje, jak dodać odpowiedź do komentarza i usunąć odpowiedź komentarza:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(doc->GetChild(NodeType::Comment, 0, true));
//Remove the reply
comment->RemoveReply(comment->get_Replies()->idx_get(0));
//Add a reply to comment
comment->AddReply(u"John Doe", u"JD", System::DateTime(2017, 9, 25, 12, 15, 0), u"New reply");
System::String outputPath = outputDataDir + u"CommentReply.doc";
// Save the document to disk.
doc->Save(outputPath);

Przeczytaj odpowiedź komentarza

Właściwość Replies zwraca zbiór obiektów Comment, które są bezpośrednimi dziećmi określonego komentarza.

Poniższy przykład kodu pokazuje, jak iterować odpowiedzi komentarza i rozwiązywać je:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void CommentResolvedandReplies(const System::SharedPtr<Document>& doc)
{
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
System::SharedPtr<Comment> parentComment = System::DynamicCast<Comment>(comments->idx_get(0));
for (System::SharedPtr<Comment> childComment : System::IterateOver<System::SharedPtr<Comment>>(parentComment->get_Replies()))
{
// Get comment parent and status.
std::cout << childComment->get_Ancestor()->get_Id() << std::endl << childComment->get_Done() << std::endl;
// And update comment Done mark.
childComment->set_Done(true);
}
}