댓글 작업
온라인 시도
당신은 우리의 이 기능을 시도할 수 있습니다 무료 온라인 주석 제거.
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"); |
댓글 추출 또는 제거
워드 문서에서 주석을 사용하는 것은(변경 사항 추적 외에도)문서를 검토할 때,특히 검토자가 여러 명인 경우 일반적인 관행입니다. 당신이 문서에서 필요로하는 유일한 것은 주석입니다 상황이있을 수 있습니다. 당신이 검토 결과의 목록을 생성 할 말,또는 아마도 당신은 문서에서 모든 유용한 정보를 수집하고 당신은 단순히 불필요한 의견을 제거 할 수 있습니다. 특정 검토자의 주석을 보거나 제거할 수 있습니다.
이 샘플에서는 문서 내의 주석에서 정보를 수집하고 문서에서 주석을 제거하는 몇 가지 간단한 방법을 살펴볼 것입니다. 특히,우리는 방법을 다룰 것입니다:
- 문서 또는 특정 저자에 의해 만들어진 사람에서 모든 의견을 추출합니다.
- 문서 또는 특정 작성자의 모든 주석을 제거합니다.
댓글 추출 또는 제거 방법
이 샘플의 코드는 매우 간단하며 모든 메서드는 동일한 접근 방식을 기반으로 합니다. Word 문서의 주석은 Aspose.Words 문서 개체 모델의 Comment
개체로 표현됩니다. 문서의 모든 주석을 수집하려면 첫 번째 매개변수를 NodeType.Comment
로 설정한 getChildNodes 메서드를 사용합니다. getChildNodes 메서드의 두 번째 매개변수가 true로 설정되어 있는지 확인합니다. 이렇게 하면 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); | |
} | |
} |