Làm việc với chú thích

Aspose.Words cho phép người dùng làm việc với các nhận xét – các nhận xét trong tài liệu ở Aspose.Words được thể hiện bằng lớp Comment. Đồng thời sử dụng các lớp CommentRangeStartCommentRangeEnd để chỉ định vùng văn bản cần được liên kết với nhận xét.

Thêm một bình luận

Aspose.Words cho phép bạn thêm nhận xét theo nhiều cách:

  1. Sử dụng lớp Comment
  2. Sử dụng lớp CommentRangeStartCommentRangeEnd

Ví dụ mã sau đây cho thấy cách thêm nhận xét vào đoạn văn bằng lớp 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);

Ví dụ mã sau đây cho thấy cách thêm nhận xét vào một đoạn văn bằng cách sử dụng một vùng văn bản và các lớp CommentRangeStartCommentRangeEnd:

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

Trích xuất hoặc xóa bình luận

Sử dụng Nhận xét trong tài liệu Word (ngoài Theo dõi Thay đổi) là cách làm phổ biến khi xem xét tài liệu, đặc biệt khi có nhiều người đánh giá. Có thể có những tình huống mà thứ duy nhất bạn cần từ một tài liệu là các nhận xét. Giả sử bạn muốn tạo danh sách các kết quả đánh giá hoặc có thể bạn đã thu thập tất cả thông tin hữu ích từ tài liệu và bạn chỉ muốn xóa các nhận xét không cần thiết. Bạn có thể muốn xem hoặc xóa nhận xét của một người đánh giá cụ thể.

Trong mẫu này, chúng ta sẽ xem xét một số phương pháp đơn giản để thu thập thông tin từ các nhận xét trong tài liệu và để xóa nhận xét khỏi tài liệu. Cụ thể chúng tôi sẽ đề cập đến cách:

  • Trích xuất tất cả các nhận xét từ một tài liệu hoặc chỉ những nhận xét của một tác giả cụ thể
  • Xóa tất cả nhận xét khỏi tài liệu hoặc chỉ từ một tác giả cụ thể

Cách trích xuất hoặc xóa bình luận

Mã trong mẫu này thực sự khá đơn giản và tất cả các phương pháp đều dựa trên cùng một cách tiếp cận. Nhận xét trong tài liệu Word được thể hiện bằng đối tượng Comment trong mô hình đối tượng tài liệu Aspose.Words. Để thu thập tất cả nhận xét trong tài liệu, hãy sử dụng phương thức GetChildNodes với tham số đầu tiên được đặt thành NodeType.Comment. Đảm bảo rằng tham số thứ hai của phương thức GetChildNodes được đặt thành true: điều này buộc GetChildNodes phải chọn đệ quy từ tất cả các nút con, thay vì chỉ thu thập các nút con ngay lập tức.

Để minh họa cách trích xuất và xóa nhận xét khỏi tài liệu, chúng ta sẽ thực hiện các bước sau:

  1. Mở tài liệu Word bằng lớp Document
  2. Lấy tất cả các nhận xét từ tài liệu vào một bộ sưu tập
  3. Trích xuất ý kiến:
    1. Duyệt qua bộ sưu tập bằng toán tử foreach
    2. Trích xuất và liệt kê tên tác giả, ngày giờ và nội dung của tất cả các bình luận
    3. Trích xuất và liệt kê tên tác giả, ngày giờ và nội dung nhận xét của một tác giả cụ thể, trong trường hợp này là tác giả ‘ks’
  4. Để xóa bình luận:
    1. Quay ngược lại bộ sưu tập bằng toán tử for
    2. Xóa bình luận
  5. Lưu các thay đổi

Cách trích xuất tất cả bình luận

Phương pháp GetChildNodes rất hữu ích và bạn có thể sử dụng nó mỗi khi cần lấy danh sách các nút tài liệu thuộc bất kỳ loại nào. Bộ sưu tập kết quả không tạo ra chi phí chung ngay lập tức vì các nút chỉ được chọn vào bộ sưu tập này khi bạn liệt kê hoặc truy cập các mục trong đó.

Ví dụ mã sau đây cho thấy cách trích xuất tên tác giả, ngày & giờ và văn bản của tất cả nhận xét trong tài liệu:

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

Cách trích xuất nhận xét của một tác giả được chỉ định

Sau khi bạn đã chọn các nút Comment vào bộ sưu tập, tất cả những gì bạn phải làm là trích xuất thông tin bạn cần. Trong mẫu này, tên viết tắt của tác giả, ngày, giờ và văn bản thuần túy của nhận xét được kết hợp thành một chuỗi; thay vào đó bạn có thể chọn lưu trữ nó theo một số cách khác.

Phương thức nạp chồng để trích xuất Nhận xét từ một tác giả cụ thể gần như giống nhau, nó chỉ kiểm tra tên tác giả trước khi thêm thông tin vào mảng.

Ví dụ mã sau đây cho thấy cách trích xuất tên tác giả, ngày & giờ và nội dung nhận xét của tác giả được chỉ định:

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

Cách xóa bình luận

Nếu bạn đang xóa tất cả các nhận xét, bạn không cần phải di chuyển qua bộ sưu tập để xóa từng nhận xét một. Bạn có thể xóa chúng bằng cách gọi phương thức Clear trên bộ sưu tập nhận xét.

Ví dụ về mã sau đây cho thấy cách xóa tất cả nhận xét trong tài liệu:

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

Khi bạn cần xóa nhận xét có chọn lọc, quy trình sẽ trở nên giống với mã chúng tôi đã sử dụng để trích xuất nhận xét.

Ví dụ mã sau đây cho thấy cách xóa nhận xét của tác giả được chỉ định:

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

Điểm chính cần nhấn mạnh ở đây là việc sử dụng toán tử for. Không giống như cách trích xuất đơn giản, ở đây bạn muốn xóa một bình luận. Một thủ thuật phù hợp là lặp lại bộ sưu tập từ Nhận xét cuối cùng đến Nhận xét đầu tiên. Lý do cho điều này là nếu bạn bắt đầu từ cuối và di chuyển về phía sau, chỉ mục của các mục trước đó vẫn không thay đổi và bạn có thể quay lại mục đầu tiên trong bộ sưu tập.

Ví dụ mã sau đây hiển thị các phương pháp trích xuất và xóa nhận xét:

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

Cách xóa nhận xét giữa CommentRangeStart và CommentRangeEnd

Sử dụng Aspose.Words, bạn cũng có thể xóa nhận xét giữa các nút CommentRangeStartCommentRangeEnd.

Ví dụ mã sau đây cho thấy cách xóa văn bản giữa CommentRangeStartCommentRangeEnd:

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

Thêm hoặc xóa câu trả lời của bình luận

Phương thức AddReply thêm câu trả lời cho nhận xét này. Xin lưu ý rằng do các giới hạn hiện tại của Microsoft Office, chỉ cho phép 1 cấp độ trả lời trong tài liệu. Một ngoại lệ của kiểu InvalidOperationException sẽ xuất hiện nếu phương thức này được gọi trên nhận xét Trả lời hiện có.

Bạn có thể sử dụng phương pháp RemoveReply để xóa câu trả lời được chỉ định cho nhận xét này.

Ví dụ mã sau đây cho thấy cách thêm câu trả lời cho nhận xét và xóa câu trả lời của nhận xét:

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

Đọc phản hồi của bình luận

Thuộc tính Replies trả về một tập hợp các đối tượng Comment là con trực tiếp của nhận xét đã chỉ định.

Ví dụ về mã sau đây cho thấy cách lặp qua các câu trả lời của nhận xét và giải quyết chúng:

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