이 간단한 자습서에서는 Java를 사용하여 Word에서 주석을 추가하는 방법에 대해 간략히 설명합니다. Word 파일에 사용할 수 있는 주석의 다른 고급 속성을 설정하고 파일을 다시 DOCX로 저장할 수 있습니다. 다음은 Java를 사용하여 Word에 주석을 삽입하는 방법에 대한 단계입니다.
Java를 사용하여 Word에서 주석을 추가하는 단계
- Maven 저장소에서 Aspose.Words을(를) 추가하여 주석을 삽입하세요.
- Document 클래스 개체를 사용하여 소스 Word 파일 열기
- 커서를 대상 단락으로 이동
- Java를 사용하여 Word 문서 단락에 주석 삽입
- 주석 추가 후 파일 저장
이 단계를 사용하여 Word 문서를 열고 단락과 같은 Word 파일의 다른 요소에 액세스하는 데 사용할 수 있는 DocumentBuilder 개체를 초기화합니다. MS Word에서 수동으로 커서 이동을 모방하는 모든 요소로 커서를 이동할 수 있습니다. 마지막으로 주석을 추가하고 Word 파일을 저장합니다.
Java를 사용하여 Word 문서에 주석을 추가하는 코드
import com.aspose.words.License; | |
import com.aspose.words.Document; | |
import com.aspose.words.DocumentBuilder; | |
import com.aspose.words.Comment; | |
import com.aspose.words.Paragraph; | |
import com.aspose.words.Run; | |
import java.util.Date; | |
public class HowToAddCommentsInWordUsingJava | |
{ | |
public static void main(String[] args) throws Exception { //main function for AddImageInWord class | |
// Initialize a license to avoid trial version watermark in the output Word file after adding image | |
License license = new License(); | |
license.setLicense("Aspose.Words.lic"); | |
// Load the Word document where comments are to be added | |
Document DocumentForComment = new Document("input.docx"); | |
DocumentBuilder builder = new DocumentBuilder(DocumentForComment); | |
// Move the cursor to the beginning of the document for adding comments | |
builder.moveToDocumentStart(); | |
// Insert comment to first paragraph of document by providing Author, Initial, time and comment text | |
Comment comment = new Comment(DocumentForComment, "Aspose.Words", "AW", new Date()); | |
builder.getCurrentParagraph().appendChild(comment); | |
comment.getParagraphs().add(new Paragraph(DocumentForComment)); | |
comment.getFirstParagraph().getRuns().add(new Run(DocumentForComment, "Comment text.")); | |
// Save the Document with comments | |
DocumentForComment.save("OutputDocumentWithComments.docx"); | |
} | |
} |
이 Java 코드에서는 Word 문서에서 주석을 구성하는 데 필요한 모든 속성을 포함하는 Comment 클래스 개체를 사용했습니다. 작성자 이름, 사용자 이니셜, 댓글 시간을 제공하고 마지막으로 댓글 텍스트를 설정합니다.
이 단계별 자습서에서는 기존 파일을 열고 주석을 추가했습니다. 테이블에 행 추가와 같은 기능에 대해 자세히 알아보려면 Java를 사용하여 Word에서 테이블에 행을 추가하는 방법에 대한 문서를 참조하세요. 위의 코드를 실행하기 위해 시스템에 MS Word 또는 Interop이 필요하지 않습니다.