การทำงานกับความคิดเห็น
ลองออนไลน์
คุณสามารถลองฟังก์ชั่นนี้กับเรา ฟรีออนไลน์เอาคำอธิบายประกอบ.
Aspose.Wordsอนุญาตให้ผู้ใช้สามารถทำงานกับความคิดเห็น-ความคิดเห็นในเอกสารในAspose.WordsจะแสดงโดยCommentชั้ ใช้ชั้นเรียนCommentRangeStartและCommentRangeEndเพื่อระบุพื้นที่ของข้อความที่ควรจะเชื่อมโยงกับข้อคิดเห็น.
เพิ่มความคิดเห็น
Aspose.Wordsช่วยให้คุณสามารถเพิ่มความคิดเห็นในหลายวิธี:
- การใช้Commentคลาส
- การใช้ชั้นเรียนCommentRangeStartและCommentRangeEnd
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มความคิดเห็นในย่อหน้าโดยใช้คลาสComment:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(AddComments.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Some text is added."); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date()); | |
builder.getCurrentParagraph().appendChild(comment); | |
comment.getParagraphs().add(new Paragraph(doc)); | |
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text.")); | |
doc.save(dataDir + "output.doc"); |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มความคิดเห็นในย่อหน้าโดยใช้พื้นที่ของข้อความและCommentRangeStartและCommentRangeEndคลาส:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(AnchorComment.class); | |
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.getFirstSection().getBody().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.getFirstSection().getBody().appendChild(para2); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date()); | |
comment.getParagraphs().add(new Paragraph(doc)); | |
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text.")); | |
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId()); | |
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId()); | |
run1.getParentNode().insertAfter(commentRangeStart, run1); | |
run3.getParentNode().insertAfter(commentRangeEnd, run3); | |
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd); | |
doc.save(dataDir + "output.doc"); |
แยกหรือลบความคิดเห็น
ใช้ความคิดเห็นในเอกสารคำ(นอกเหนือจากการติดตามการเปลี่ยนแปลง)เป็นวิธีปฏิบัติทั่วไ สามารถมีสถานการณ์ที่สิ่งเดียวที่คุณต้องการจากเอกสารเป็นความคิดเห็น. สมมติว่าคุณต้องการที่จะสร้างรายการของการค้นพบการตรวจสอบหรือบางทีคุณอาจจะได้ คุณอาจต้องการดูหรือลบความคิดเห็นของผู้ตรวจทานรายใดรายหนึ่ง.
ในตัวอย่างนี้,เราจะไปดูที่วิธีการบางอย่างง่ายสำหรับทั้งการรวบรวมข้อมูลจากความคิดเห็นภายในเอกสารและสำหรับการลบความคิดเห็นจากเอกสาร. โดยเฉพาะอย่างยิ่งเราจะครอบคลุมถึงวิธีการ:
- สารสกัดจากความคิดเห็นทั้งหมดจากเอกสารหรือเฉพาะคนที่ทำโดยผู้เขียนโดยเฉพาะอย่.
- ลบความคิดเห็นทั้งหมดจากเอกสารหรือจากผู้เขียนโดยเฉพาะอย่างยิ่ง.
วิธีการแยกหรือลบความคิดเห็น
รหัสในตัวอย่างนี้ค่อนข้างง่ายและวิธีการทั้งหมดจะขึ้นอยู่กับวิธีการเดียวกัน ข้อคิดเห็นในเอกสารคำจะแสดงโดยวัตถุComment
ในรูปแบบวัตถุเอกสารAspose.Words ในการรวบรวมความคิดเห็นทั้งหมดในเอกสารให้ใช้วิธีการgetChildNodesกับพารามิเตอร์แรกที่ตั้งไว้ที่NodeType.Comment
ตรวจสอบให้แน่ใจว่าพารามิเตอร์ที่สองของวิธีการgetChildNodesถูกตั้งค่าเป็นจริง:สิ่งนี้บังคับให้getChildNodesเลือกจา.
เพื่อแสดงให้เห็นถึงวิธีการแยกและลบความคิดเห็นจากเอกสาร,เราจะไปผ่านขั้นตอนต่อ:
- เปิดเอกสารคำโดยใช้คลาสDocument
- รับความคิดเห็นทั้งหมดจากเอกสารลงในคอลเลกชัน
- การแยกความคิดเห็น:
- ผ่านการเก็บรวบรวมโดยใช้สำหรับผู้ประกอบการ
- สารสกัดจากและรายชื่อชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นทั้งหมด
- สารสกัดจากและรายชื่อชื่อผู้เขียนวันที่และเวลาและข้อความแสดงความคิดเห็นที่เขียนโดย
- วิธีลบความคิดเห็น:
- ไปข้างหลังผ่านคอลเลกชันโดยใช้สำหรับผู้ประกอบการ
- ลบความคิดเห็น
- บันทึกการเปลี่ยนแปลง.
เราจะใช้เอกสารคำต่อไปนี้สำหรับการออกกำลังกายนี้:
ที่คุณสามารถดูมันมีหลายความคิดเห็นจากสองผู้เขียนที่มีชื่อย่อ"น.“และ"แคนซัส”.
วิธีการแยกความคิดเห็นทั้งหมด
วิธีgetChildNodesมีประโยชน์มากและคุณสามารถใช้มันทุกครั้งที่คุณต้องการที่จะได้รับรายชื่อของโหนดเ คอลเล็กชันผลลัพธ์ไม่ได้สร้างค่าใช้จ่ายในทันทีเนื่องจากโหนดถูกเลือกไว้ในคอลเล็กชันนี้เ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นทั้งหมดในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
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. | |
for (Comment comment : (Iterable<Comment>) comments) { | |
collectedComments.add(comment.getAuthor() + " " + comment.getDateTime() + " " + comment.toString(SaveFormat.TEXT)); | |
} | |
return collectedComments; |
วิธีการแยกความคิดเห็นของผู้เขียนที่ระบุ
หลังจากที่คุณได้เลือกโหนดความคิดเห็นลงในคอลเลกชันทั้งหมดที่คุณต้องทำคือการแยกข้ ในตัวอย่างนี้ชื่อย่อวันที่เวลาและข้อความธรรมดาของความคิดเห็นจะรวมกันเป็นสตริงหนึ่ง;คุณสามารถเลือกที่จะเก็บไว้ในบางวิธีอื่นๆแทน.
วิธีการมากเกินไปที่แยกความคิดเห็นจากผู้เขียนโดยเฉพาะอย่างยิ่งเกือบจะเหมือนกันก็เ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกชื่อผู้เขียนวันที่และเวลาและข้อความของความคิดเห็นโดยผู้เขียนที่ระบุ:
วิธีการลบความคิดเห็น
หากคุณกำลังลบความคิดเห็นทั้งหมดไม่จำเป็นต้องย้ายผ่านคอลเล็กชันการลบความคิดเห็นทีละรายการคุณสามารถลบความคิดเห็นได้โดยโทรclearบนคอลเล็กชันความคิดเห็น.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบความคิดเห็นทั้งหมดในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ProcessComments.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
for (String comment : (Iterable<String>) extractComments(doc)) | |
System.out.print(comment); | |
// Remove comments by the "pm" author. | |
removeComments(doc, "pm"); | |
System.out.println("Comments from \"pm\" are removed!"); | |
// Extract the information about the comments of the "ks" author. | |
for (String comment : (Iterable<String>) extractComments(doc, "ks")) | |
System.out.print(comment); | |
//Read the comment's reply and resolve them. | |
System.out.println("Read the comment's reply and resolve them."); | |
CommentResolvedandReplies(doc); | |
// Remove all comments. | |
removeComments(doc); | |
System.out.println("All comments are removed!"); | |
// Save the document. | |
doc.save(dataDir + "output.doc"); |
เมื่อเปิดตัวตัวอย่างจะแสดงผลลัพธ์ต่อไปนี้ ครั้งแรกที่แสดงความคิดเห็นทั้งหมดโดยผู้เขียนทั้งหมดแล้วจะแสดงความคิดเห็นโดยผู้เขียนที่เลือกเท่านั้น สุดท้ายรหัสลบความคิดเห็นทั้งหมด.
เอกสารคำที่ส่งออกได้ในขณะนี้ความคิดเห็นออกจากมัน:
วิธีการลบข้อความระหว่าง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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RemoveCommentRegionText.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
CommentRangeStart commentStart = (CommentRangeStart) doc.getChild(NodeType.COMMENT_RANGE_START, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd) doc.getChild(NodeType.COMMENT_RANGE_END, 0, true); | |
Node currentNode = commentStart; | |
Boolean isRemoving = true; | |
while (currentNode != null && isRemoving) { | |
if (currentNode.getNodeType() == NodeType.COMMENT_RANGE_END) | |
isRemoving = false; | |
Node nextNode = currentNode.nextPreOrder(doc); | |
currentNode.remove(); | |
currentNode = nextNode; | |
} | |
doc.save(dataDir + "output.doc"); |
เพิ่มหรือลบคำตอบของความคิดเห็น
วิธีการaddReplyเพิ่มการตอบกลับความคิดเห็นนี้ โปรดทราบว่าเนื่องจากข้อจำกัดของสำนักงานMSที่มีอยู่เพียงหนึ่ง(1)ระดับของการตอบกลับที่ไ ข้อยกเว้นของชนิดInvalidOperationExceptionจะถูกยกขึ้นถ้าวิธีนี้ถูกเรียกใช้ในการตอบกลับความคิดเห็นที่มีอยู่.
คุณสามารถใช้วิธีการremoveReplyเพื่อลบการตอบกลับที่ระบุในความคิดเห็นนี้.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มการตอบกลับความคิดเห็นและลบการตอบกลับของค:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
Comment comment = (Comment) doc.getChild(NodeType.COMMENT, 0, true); | |
//Remove the reply | |
comment.removeReply(comment.getReplies().get(0)); | |
//Add a reply to comment | |
comment.addReply("John Doe", "JD", new Date(), "New reply"); | |
dataDir = dataDir + "TestFile_Out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
อ่านความคิดเห็นตอบ
Aspose.Wordsสนับสนุนการอ่านการตอบกลับของความคิดเห็น คุณสมบัติRepliesจะส่งคืนคอลเล็กชันของออบเจกต์Commentที่เป็นเด็กทันทีของข้อคิดเห็นที่ระบุ.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำซ้ำผ่านการตอบกลับของความคิดเห็นและแก้ไขโค้ดเหล่:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
static void CommentResolvedandReplies(Document doc) { | |
NodeCollection<Comment> comments = doc.getChildNodes(NodeType.COMMENT, true); | |
Comment parentComment = (Comment) comments.get(0); | |
for (Comment childComment : parentComment.getReplies()) { | |
// Get comment parent and status. | |
System.out.println(childComment.getAncestor().getId()); | |
System.out.println(childComment.getDone()); | |
// And update comment Done mark. | |
childComment.setDone(true); | |
} | |
} |