עבודה עם הערות
נסה באינטרנט
אתה יכול לנסות את הפונקציונליות שלנו חינם באינטרנט להסיר הודעות.
Aspose.Words מאפשר למשתמשים לעבוד עם הערות - הערות במסמך Aspose.Words מיוצגים על ידי Comment מעמד. השתמש גם CommentRangeStart ו CommentRangeEnd שיעורים כדי לציין את אזור הטקסט כי צריך להיות קשור עם תגובה.
הוסף תגובה
Aspose.Words מאפשר להוסיף הערות בכמה דרכים:
1.1 1. באמצעות Comment הכיתה 2. באמצעות CommentRangeStart ו CommentRangeEnd שיעורים
הדוגמה הבאה של הקוד מראה כיצד להוסיף תגובה לפסקה באמצעות הסעיף Comment קטגוריה:
// 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); |
הדוגמה הבאה של הקוד מראה כיצד להוסיף תגובה לפסקה באמצעות אזור טקסט והטקסט CommentRangeStart ו CommentRangeEnd שיעורים:
// 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); |
לחלץ או להסיר הערות
שימוש בהערות במסמך Word (בנוסף לשינויים במעקב) הוא תרגול נפוץ בעת בדיקת מסמכים, במיוחד כאשר ישנם מספר סוקרים. יש מצבים שבהם הדבר היחיד שאתה צריך במסמך הוא התגובות. נניח שאתה רוצה ליצור רשימה של ממצאי ביקורת, או אולי אספת את כל המידע השימושי של המסמך ואתה פשוט רוצה להסיר הערות מיותרות. ייתכן שתרצה להציג או להסיר הערות של מבקר מסוים.
בדגימה זו אנו הולכים לבחון כמה שיטות פשוטות עבור איסוף מידע מההערות בתוך מסמך ועל הסרת הערות ממסמכים. באופן ספציפי, אנו נעמוד כיצד:
- להפיק את כל התגובות של מסמך או רק את אלה שנעשו על ידי מחבר מסוים
- להסיר את כל ההערות של מסמך או רק ממחבר מסוים
כיצד להסיר או להסיר הערות
הקוד במדגם זה הוא למעשה פשוט למדי וכל השיטות מבוססות על אותה גישה. תגובה במסמך Word מיוצגת על ידי Comment אובייקט Aspose.Words מסמך מודל אובייקט כדי לאסוף את כל התגובות במסמך להשתמש GetChildNodes שיטה עם הפרמטר הראשון להגדיר NodeType.Comment. ודא כי הפרמטר השני של GetChildNodes השיטה מיועדת trueכוחות אלה GetChildNodes כדי לבחור מכל בלוטות הילד, במקום רק לאסוף את הילדים המיידיים.
כדי להמחיש כיצד לחלץ ולהסיר הערות ממסמכים, אנו עוברים את השלבים הבאים:
1.1 1. פתח מסמך Word באמצעות Document הכיתה 2. קבלו את כל התגובות מהמסמכים לאוסף 3. להוציא הערות: 1 לעבור את האוסף באמצעות מפעיל ה-Foreach 2. לחלץ ולרשום את שם המחבר, תאריך וזמן וטקסט של כל הערות 3. לחלץ ולרשום את שם המחבר, תאריך ושעה וטקסט של הערות שנכתבו על ידי מחבר ספציפי, במקרה זה המחבר “ks” 4. להסיר הערות: 1 הפוך דרך האוסף באמצעות המפעיל 2 הסרת הערות 5. להציל את השינויים
כיצד להפיק את כל התגובות
The The The GetChildNodes שיטה היא מאוד שימושית ואתה יכול להשתמש בה בכל פעם שאתה צריך לקבל רשימה של דפי מסמך מכל סוג. האוסף המתקבל אינו יוצר כותרת מיידית, כי הצמתים נבחרים לתוך אוסף זה רק כאשר אתה אוכל או גישה פריטים בו.
דוגמה הקוד הבא מראה כיצד לחלץ את שם המחבר, תאריך וטקסט של כל התגובות במסמך:
// 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; | |
} |
כיצד להפיק הערות של מחבר ספציפי
אחרי שבחרת Comment צומת לתוך אוסף, כל שעליך לעשות הוא לחלץ את המידע שאתה צריך. בדגימה זו, המחבר הראשוניים, התאריך, הזמן והטקסט הפשוט של ההערה משולבים לתוך מיתר אחד; אתה יכול לבחור לאחסן אותו בדרכים אחרות במקום.
השיטה המוגברת שמוציאה את ההערות של מחבר מסוים כמעט זהה, היא רק בודקת את שם המחבר לפני הוספת המידע למערך.
הדוגמה הבאה של הקוד מראה כיצד לחלץ את שם המחבר, תאריך וטקסט של ההערות של המחבר שצוין:
כיצד להסיר הערות
אם אתם מסירים את כל ההערות, אין צורך לעבור דרך ההערות של האוסף. אתה יכול להסיר אותם על ידי קריאה Clear שיטה לאיסוף הערות
הדוגמה הבאה של הקוד מראה כיצד להסיר את כל התגובות במסמך:
// 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(); | |
} |
כאשר אתה צריך להסיר הערות באופן סלקטיבי, התהליך הופך להיות דומה יותר לקוד שבו השתמשנו למיצוי תגובה.
הדוגמה הבאה של הקוד מראה כיצד להסיר הערות על ידי המחבר שצוין:
הנקודה העיקרית להדגיש כאן היא השימוש של המפעיל. בניגוד למיצוי הפשוט, אתם רוצים למחוק תגובה. טריק מתאים הוא להפוך את האוסף לאחור מההערה האחרונה לראשון. הסיבה לכך אם אתה מתחיל מן הסוף והזיז לאחור, המדד של הפריטים הקודמים נשאר ללא שינוי, ואתה יכול לעבוד את דרכך חזרה אל הפריט הראשון באוסף.
הדוגמה הבאה של הקוד מציגה את השיטות למיצוי הערות וההסרה:
// 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); |
כיצד להסיר הערה בין CommentRangeStart ו- CommentRange End
שימוש Aspose.Words ניתן גם להסיר הערות בין CommentRangeStart ו CommentRangeEnd צומת
דוגמה לקוד הבא מראה כיצד להסיר טקסט בין CommentRangeStart ו CommentRangeEnd:
// 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); |
הוסף או להסיר תגובה
The The The AddReply השיטה מוסיפה תשובה להערה זו. שימו לב כי בשל הקיים Microsoft הגבלות Office רק רמה אחת של תשובות מותרות במסמך. יוצא דופן מסוג InvalidOperationException יעלה אם שיטה זו נקראת על תגובת התשובה הקיימת.
אתה יכול להשתמש RemoveReply שיטה להסיר את התשובה המפורטת להערה זו.
הדוגמה הבאה של הקוד מראה כיצד להוסיף תשובה להגיב ולהסיר את התשובה:
// 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); |
תגובה
The The The Replies הנכס מחזיר אוסף של Comment פריטים שהם ילדים מיידיים של התגובה המפורטת.
הדוגמה הבאה של הקוד מראה כיצד לתקן באמצעות תשובות של תגובה ופתירתם:
// 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; | |
} | |
} |