GroupDocs.Merger와 함께 Java 문서 분할 마스터하기: DOCX 페이지를 파일 및 스트림으로 분할
이 튜토리얼에서는 how to split docx 문서를 GroupDocs.Merger for Java를 사용해 효율적으로 분할하는 방법을 알아봅니다. 큰 계약서를 개별 페이지로 나누거나 특정 섹션을 스트림으로 추출해야 할 때, 설정부터 실제 사용까지 모든 단계를 자세히 안내합니다.
Quick Answers
- What library handles DOCX splitting in Java? GroupDocs.Merger for Java.
- Can I split a DOCX into separate files? Yes – use
SplitOptionswith page numbers. - Is it possible to get pages as streams instead of files? Absolutely, by providing a custom
SplitStreamFactory. - Do I need a license? A temporary trial license is enough for evaluation; a full license is required for production.
- Which Java versions are supported? Any JDK 8+ works with the latest GroupDocs.Merger release.
“how to split docx”란?
DOCX를 분할한다는 것은 다중 페이지 Word 문서를 하나 이상의 선택된 페이지를 포함하는 개별 파일(또는 스트림)로 만드는 것을 의미합니다. 이는 모듈식 문서 제공, 규정 준수 워크플로, 또는 임시 파일을 저장하고 싶지 않은 실시간 처리에 유용합니다.
왜 GroupDocs.Merger for Java를 사용해야 할까요?
- Zero‑dependency processing: 순수 Java만으로 동작하며 네이티브 바이너리가 필요 없습니다.
- Fine‑grained control: 정확한 페이지, 출력 형식, 심지어 메모리 내 스트림까지 선택할 수 있습니다.
- Scalable performance: 스트림 기반 분할은 대용량 파일의 메모리 부담을 줄여줍니다.
Prerequisites
Required Libraries and Dependencies
- Java Development Kit (JDK): JDK 8 이상.
- GroupDocs.Merger for Java: 문서 조작을 위한 핵심 라이브러리.
Adding the Dependency
Maven 또는 Gradle을 통해 라이브러리를 포함합니다 (코드 블록은 그대로 유지).
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger</artifactId>
<version>latest-version</version>
</dependency>
implementation 'com.groupdocs:groupdocs-merger:latest-version'
또한 공식 사이트에서 최신 릴리스를 다운로드할 수 있습니다: GroupDocs.Merger for Java releases.
License Acquisition
- Trial license: GroupDocs.Trial License 페이지에서 임시 키를 받으세요.
- Production license: GroupDocs Purchase에서 정식 라이선스를 구매하세요.
Setting Up GroupDocs.Merger for Java
Java 프로젝트에서 라이브러리를 초기화합니다:
import com.groupdocs.merger.Merger;
public class DocumentSetup {
public static void main(String[] args) {
// Initialize a Merger object with the path of the input document
Merger merger = new Merger("path/to/your/document.docx");
// Perform operations on your document...
merger.close();
}
}
환경이 준비되었으면, split docx into files 또는 스트림 두 가지 주요 방법을 살펴보겠습니다.
GroupDocs.Merger로 DOCX를 파일로 분할하는 방법
Split Document into Single Pages
Overview
선택한 각 페이지마다 별도의 파일을 생성하는 방식으로, 개별 섹션을 배포할 때 적합합니다.
Step‑by‑Step Implementation
Step 1 – Specify Input and Output Paths
원본 DOCX가 위치한 경로와 분할 파일을 저장할 경로를 정의합니다.
String filePath = "YOUR_DOCUMENT_DIRECTORY/SAMPLE_DOCX_10_PAGES";
String filePathOut = new File("YOUR_OUTPUT_DIRECTORY",
"SplitToSinglePages-" +
Paths.get(filePath).getFileName().toString()
).getPath();
Step 2 – Configure SplitOptions (split options java)
추출할 페이지를 지정합니다.
import com.groupdocs.merger.domain.options.SplitOptions;
SplitOptions splitOptions = new SplitOptions(filePathOut, new int[] { 3, 6, 8 });
filePathOut– 각 페이지 파일이 저장될 폴더.new int[]{3,6,8}– 분할하려는 페이지 번호.
Step 3 – Perform the SplitMerger 인스턴스로 작업을 실행합니다.
import com.groupdocs.merger.Merger;
Merger merger = new Merger(filePath);
merger.split(splitOptions);
Pro tip: 출력 디렉터리가 존재하는지, 애플리케이션에 쓰기 권한이 있는지 확인하세요. 그렇지 않으면 분할이 실패합니다.
Common Pitfalls
- Missing output folder: API가 디렉터리를 자동으로 생성하지 않습니다.
- Incorrect page numbers: 페이지 인덱스는 1부터 시작합니다. 0을 지정하면 오류가 발생합니다.
DOCX 페이지를 스트림(메모리)으로 분할하는 방법 (In‑Memory)
Overview
예를 들어 웹 서비스로 페이지를 전송해야 할 때와 같이 일시적인 접근이 필요하면, 스트림으로 캡처해 디스크 I/O를 피할 수 있습니다.
Step‑by‑Step Implementation
Step 1 – Define Input Path and Prepare a List for Streams
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
String filePath = "YOUR_DOCUMENT_DIRECTORY/SAMPLE_DOCX_10_PAGES";
final List<OutputStream> resultStreams = new ArrayList<>();
Step 2 – Configure SplitOptions with a Custom SplitStreamFactory
import com.groupdocs.merger.domain.common.SplitStreamFactory;
import com.groupdocs.merger.domain.options.SplitMode;
SplitOptions splitOptions = new SplitOptions(new SplitStreamFactory() {
@Override
public OutputStream createSplitStream(int pageNumber) {
return new ByteArrayOutputStream(); // Create a stream for each page
}
@Override
public void closeSplitStream(int pageNumber, OutputStream pageStream) {
resultStreams.add(pageStream); // Collect the streams
}
}, new int[] { 3, 4 }, SplitMode.Pages);
createSplitStream– 요청된 각 페이지에 대해 새로운OutputStream을 생성합니다.closeSplitStream– 완료된 스트림을 나중에 사용할 수 있도록 저장합니다.
Step 3 – Execute the Split and Retrieve Streams
Merger merger = new Merger(filePath);
merger.split(splitOptions);
return resultStreams; // Retrieve streams for processing
Troubleshooting Tips
- 소스 DOCX 경로가 정확한지 확인하세요. 오타가 있으면
FileNotFoundException이 발생합니다. - 사용이 끝난 스트림은 반드시 닫아 메모리를 해제하세요.
Practical Applications
- Legal contracts: 개별 조항을 추출해 별도 검토용으로 제공.
- E‑learning platforms: 전체 교재를 노출하지 않고 챕터별 Word 파일을 제공.
- Business reporting: 분기 보고서 중 재무 섹션만 CFO에게 전송.
Performance Considerations
- Memory‑efficient streams: 50 MB 이상의 대용량 문서는 스트림 방식을 권장합니다.
- Batch processing: 여러 분할 작업을 하나의 JVM 세션에서 처리해 시작 오버헤드를 감소시킵니다.
- Resource cleanup:
merger.close()와 모든 스트림을 닫아 메모리 누수를 방지합니다.
Conclusion
이제 GroupDocs.Merger for Java를 사용해 how to split docx 파일을 별도 파일이나 메모리 스트림으로 분할하는 방법을 알게 되었습니다. 이러한 기술을 활용하면 비즈니스 요구에 맞게 문서 전달 방식을 자유롭게 조정할 수 있습니다.
Next Steps
- 다양한 페이지 범위와 출력 형식(PDF, HTML 등)을 실험해 보세요.
- 분할과 병합을 결합해 실시간으로 맞춤 번들을 재구성해 보세요.
Frequently Asked Questions
Q: What is GroupDocs.Merger for Java?
A: It’s a Java library that enables merging, splitting, and converting a wide range of document formats, including DOCX, PDF, PPTX, and more.
Q: How do I obtain a license for GroupDocs.Merger?
A: You can acquire a temporary trial license from the GroupDocs website for evaluation. For production use, purchase a full license at the same site.
Q: Can I split PDF files using the same API?
A: Yes, the split method works with PDF, DOCX, PPTX, and other supported formats.
Q: Is it possible to split a document without writing to disk?
A: Absolutely—use the stream‑based approach shown above to keep everything in memory.
Q: Which version of GroupDocs.Merger should I use?
A: Always target the latest stable release to benefit from performance improvements and bug fixes.
Last Updated: 2026-02-06
Tested With: GroupDocs.Merger for Java latest-version
Author: GroupDocs