עבודה עם הערות

Aspose.Words מאפשר למשתמשים לעבוד עם הערות-הערות במסמך ב Aspose.Words מיוצגות על ידי הכיתה Comment. השתמש גם בכיתות CommentRangeStart ו - CommentRangeEnd כדי לציין את אזור הטקסט שצריך לשייך לתגובה.

הוסף תגובה

Aspose.Words מאפשר לך להוסיף הערות בכמה דרכים:

  1. שימוש בכיתה Comment
  2. שימוש בכיתות CommentRangeStart ו CommentRangeEnd

דוגמת הקוד הבאה מראה כיצד להוסיף הערה לפסקה באמצעות הכיתה 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);

דוגמת הקוד הבאה מראה כיצד להוסיף הערה לפסקה באמצעות אזור טקסט ושיעורי CommentRangeStart ו - 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);

חלץ או הסר הערות

שימוש בהערות במסמך Word (בנוסף לשינויים במסלול) הוא נוהג נפוץ בעת סקירת מסמכים, במיוחד כאשר יש מספר מבקרים. לא יכול להיות מצבים שבהם הדבר היחיד שאתה צריך ממסמך הוא הערות. נניח שאתה רוצה ליצור רשימה של ממצאי סקירה, או שאולי אספת את כל המידע השימושי מהמסמך ואתה פשוט רוצה להסיר הערות מיותרות. ייתכן שתרצה להציג או להסיר את ההערות של סוקר מסוים.

במדגם זה אנו הולכים לבחון כמה שיטות פשוטות הן לאיסוף מידע מההערות במסמך והן להסרת הערות ממסמך. באופן ספציפי, נסקור כיצד:

  • חלץ את כל ההערות ממסמך או רק את אלה שנעשו על ידי מחבר מסוים.
  • הסר את כל ההערות ממסמך או רק ממחבר מסוים.

כיצד לחלץ או להסיר הערות

הקוד במדגם זה הוא למעשה די פשוט וכל השיטות מבוססות על אותה גישה. הערה במסמך Word מיוצגת על ידי אובייקט Comment במודל אובייקט המסמך Aspose.Words. כדי לאסוף את כל ההערות במסמך השתמש בשיטת GetChildNodes עם הפרמטר הראשון שהוגדר ל NodeType.Comment. ודא שהפרמטר השני של שיטת GetChildNodes מוגדר כנכון: זה מאלץ את GetChildNodes לבחור מכל צמתי הילד באופן רקורסיבי, במקום לאסוף רק את הילדים המיידיים.

כדי להמחיש כיצד לחלץ ולהסיר הערות ממסמך, נעבור על השלבים הבאים:

  1. פתח מסמך Word באמצעות הכיתה Document
  2. קבל את כל ההערות מהמסמך לאוסף
  3. כדי לחלץ הערות:
    1. לעבור את האוסף באמצעות מפעיל כל מפעיל
    2. חלץ ורשום את שם המחבר, התאריך והשעה והטקסט של כל ההערות
    3. חלץ ורשום את שם המחבר, תאריך ושעה וטקסט של הערות שנכתבו על ידי מחבר ספציפי, במקרה זה, המחבר ‘קס’
  4. כדי להסיר הערות:
    1. ללכת אחורה דרך האוסף באמצעות עבור המפעיל
    2. הסר הערות
  5. שמור את השינויים

כיצד לחלץ את כל ההערות

שיטת GetChildNodes שימושית מאוד ותוכלו להשתמש בה בכל פעם שתצטרכו לקבל רשימה של צמתי מסמכים מכל סוג שהוא. האוסף המתקבל אינו יוצר תקורה מיידית מכיוון שהצמתים נבחרים לאוסף זה רק כאשר אתה מונה או ניגש לפריטים בו.

דוגמת הקוד הבאה מראה כיצד לחלץ את שם המחבר, התאריך והשעה והטקסט של כל ההערות במסמך:

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;
}

כיצד לחלץ הערות של מחבר שצוין

לאחר שבחרת צמתים תגובה לאוסף, כל שעליך לעשות הוא לחלץ את המידע הדרוש לך. במדגם זה, ראשי התיבות של המחבר, התאריך, השעה והטקסט הפשוט של ההערה משולבים למחרוזת אחת; אתה יכול לבחור לאחסן אותו בדרכים אחרות במקום.

השיטה העמוסה המחלצת את ההערות ממחבר מסוים היא כמעט זהה, היא רק בודקת את שם המחבר לפני הוספת המידע למערך.

דוגמת הקוד הבאה מראה כיצד לחלץ את שם המחבר, התאריך והשעה והטקסט של ההערות על ידי המחבר שצוין:

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;
}

כיצד להסיר הערות

אם אתה מסיר את כל ההערות, אין צורך לעבור באוסף מחיקת הערות אחת אחת; אתה יכול להסיר אותן על ידי התקשרות NodeCollection.Clear באוסף ההערות.

דוגמת הקוד הבאה מראה כיצד להסיר את כל ההערות במסמך:

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();
}

כאשר אתה צריך להסיר הערות באופן סלקטיבי, התהליך הופך להיות דומה יותר לקוד בו השתמשנו לחילוץ הערות.

דוגמת הקוד הבאה מראה כיצד להסיר הערות של המחבר שצוין:

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();
}
}
}

הנקודה העיקרית להדגיש כאן היא השימוש של עבור מפעיל. שלא כמו החילוץ פשוט, כאן אתה רוצה למחוק תגובה. טריק מתאים הוא לחזור על האוסף לאחור מההערה האחרונה לתגובה הראשונה. הסיבה לכך אם אתה מתחיל מהסוף ולעבור אחורה, המדד של הפריטים הקודמים נשאר ללא שינוי, ואתה יכול לעבוד בדרך חזרה לפריט הראשון באוסף.

דוגמת הקוד הבאה מציגה את השיטות להוצאת ההערות והסרתן:

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);

כיצד להסיר תגובה בין CommentRangeStart ו CommentRangeEnd

באמצעות Aspose.Words ניתן גם להסיר הערות בין הצמתים CommentRangeStart ו CommentRangeEnd.

דוגמת הקוד הבאה מראה כיצד להסיר טקסט בין CommentRangeStart ו 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);

הוספה והסרה של תגובה

שיטת AddReply מוסיפה תשובה לתגובה זו. לידיעתך, בשל מגבלות המשרד הקיימות Microsoft רק רמת 1 של תגובות מותרת במסמך. חריגה מסוג InvalidOperationException תועלה אם שיטה זו נקראת בהערת התשובה הקיימת.

אתה יכול להשתמש בשיטת RemoveReply כדי להסיר את התשובה שצוינה לתגובה זו.

דוגמת הקוד הבאה מראה כיצד להוסיף תגובה לתגובה ולהסיר את תגובת התגובה:

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);

קרא תגובה של תגובה

המאפיין Replies מחזיר אוסף של האובייקטים Comment שהם ילדים מיידיים של ההערה שצוינה.

דוגמת הקוד הבאה מראה כיצד לחזור על התגובות של תגובה ולפתור אותן:

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);
}
}