[java] Apache POI를 이용한 Word 문서 이력 관리

Apache POI는 Java에서 Microsoft Office 파일을 읽고 쓸 수 있는 라이브러리입니다. 이 라이브러리를 사용하여 Word 문서의 이력 정보를 관리할 수 있습니다. 이번 글에서는 Apache POI를 사용하여 Word 문서의 이력을 관리하는 방법에 대해 알아보겠습니다.

1. Apache POI 라이브러리 설치

Apache POI를 사용하기 위해서는 먼저 해당 라이브러리를 설치해야 합니다. Maven을 사용하는 경우, pom.xml 파일에 다음 의존성을 추가합니다:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

Gradle을 사용하는 경우, build.gradle 파일에 다음 의존성을 추가합니다:

implementation 'org.apache.poi:poi:4.1.2'
implementation 'org.apache.poi:poi-ooxml:4.1.2'

의존성을 추가한 후 프로젝트를 빌드하면 Apache POI가 설치됩니다.

2. Word 문서 이력 관리

Apache POI를 사용하여 Word 문서의 이력을 관리하기 위해서는 다음 단계를 따라야 합니다:

2.1. Word 문서 로드

FileInputStream fis = new FileInputStream("path/to/word/document.docx");
XWPFDocument document = new XWPFDocument(fis);

위 코드는 지정된 경로에 있는 Word 문서를 로드하여 XWPFDocument 객체로 변환합니다.

2.2. 문서 이력 추가

XWPFCommentsDecorator commentsDecorator = new XWPFCommentsDecorator(document);
XWPFComment comment = commentsDecorator.createComment();
comment.setAuthor("John Doe");
comment.setText("This is a comment.");

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("This is a paragraph.");

commentsDecorator.setCommentRefs(paragraph, comment);

위 코드는 XWPFCommentsDecorator를 사용하여 문서에 코멘트를 추가합니다. createComment()를 호출하여 새로운 코멘트 객체를 생성하고, setAuthor()setText()를 사용하여 코멘트의 작성자와 내용을 설정합니다. 이후 createParagraph()createRun()을 사용하여 새로운 문단과 텍스트를 생성합니다. 마지막으로 setCommentRefs()를 사용하여 문단에 코멘트를 연결합니다.

2.3. 문서 저장

FileOutputStream fos = new FileOutputStream("path/to/word/document_modified.docx");
document.write(fos);
fos.close();

위 코드는 수정된 문서를 지정된 경로에 저장합니다.

3. 예제 코드

import org.apache.poi.xwpf.usermodel.XWPFComment;
import org.apache.poi.xwpf.usermodel.XWPFCommentsDecorator;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WordDocumentHistory {

    public static void main(String[] args) {
        try {
            XWPFDocument document = loadDocument("path/to/word/document.docx");
            addComment(document, "John Doe", "This is a comment");
            saveDocument(document, "path/to/word/document_modified.docx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static XWPFDocument loadDocument(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        return new XWPFDocument(fis);
    }

    private static void addComment(XWPFDocument document, String author, String text) {
        XWPFCommentsDecorator commentsDecorator = new XWPFCommentsDecorator(document);
        XWPFComment comment = commentsDecorator.createComment();
        comment.setAuthor(author);
        comment.setText(text);

        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("This is a paragraph.");

        commentsDecorator.setCommentRefs(paragraph, comment);
    }

    private static void saveDocument(XWPFDocument document, String filePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(filePath);
        document.write(fos);
        fos.close();
    }
}

위 예제 코드는 Apache POI를 사용하여 Word 문서의 이력을 관리하는 간단한 예제입니다.

4. 참고 자료