MS Word는 환상적인 기능으로 전문 문서를 사용자 정의하고 서식을 지정할 수 있는 강력한 도구입니다. 모든 영수증을 단일 파일로 결합하거나 더 쉬운 관리를 위해 다른 소프트웨어 요구 사항 문서를 결합하는 것과 같이 여러 Word 문서를 병합하려는 상황에 처할 수 있습니다. 또한 파일을 결합하면 공유 프로세스가 간소화됩니다. 이 기사에서는 C++를 사용하여 여러 MS Word 문서를 병합하는 방법을 배웁니다.
Word 문서를 병합하는 C++ API
Aspose.Words for C++은 Microsoft Word 문서를 생성, 변경 및 변환할 수 있는 네이티브 C++ 라이브러리입니다. 또한 여러 Word 문서를 단일 파일로 결합하는 것도 지원합니다. NuGet을 통해 API를 설치하거나 다운로드 섹션에서 직접 다운로드할 수 있습니다.
PM> Install-Package Aspose.Words.Cpp
C++를 사용하여 Word 문서 병합
Word 문서를 Aspose.Words for C++ API와 병합하는 것은 아주 쉽습니다. 몇 줄의 코드로 로드하고 병합할 수 있습니다. 다음은 이 기사에서 결합할 두 개의 샘플 Word 파일을 보여주는 이미지입니다.
다음은 C++를 사용하여 Word 문서를 병합하는 단계입니다.
- Document 클래스를 사용하여 대상 문서를 로드합니다.
- Document 클래스를 사용하여 소스 문서를 로드합니다.
- Document->AppendDocument(시스템::SharedPtrAspose::Words::Document srcDoc, Aspose::Words::ImportFormatMode importFormatMode) 두 문서를 병합하기 위한 대상 문서 인스턴스의 메소드.
- ImportFormatMode 열거형을 사용하여 소스 문서를 가져오기 위한 스타일 기본 설정을 지정합니다.
- Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat) 메서드를 사용하여 병합된 Word 문서를 저장합니다.
다음은 Word 문서를 병합하는 샘플 코드입니다.
// 병합할 샘플 Word 문서
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";
// 병합할 Word 문서 로드
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);
// 소스 파일의 형식을 유지하면서 문서 병합
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting);
// 출력 파일 경로
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";
// 병합된 문서를 DOCX 파일로 저장
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);
다음 이미지는 ImportFormatMode::KeepSourceFormatting 및 [ImportFormatMode](https://apireference. aspose.com/words/cpp/namespace/aspose.words#aafaa52cbf0baa49c3225787c23a8c949)::UseDestinationStyles 모드.
추가 옵션으로 Word 문서 병합
Aspose.Words for C++ API는 Word 파일 병합을 사용자 지정하기 위한 ImportFormatOptions 클래스를 제공합니다. 다음은 ImportFormatOptions 클래스에서 제공하는 옵션입니다.
- IgnoreHeaderFooter: ImportFormatMode::KeepSourceFormatting 모드를 사용할 때 머리글/바닥글 내용의 서식을 무시할지 여부를 지정합니다.
- IgnoreTextBoxes: Specifies whether to ignore the source formatting of textboxes when the ImportFormatMode::KeepSourceFormatting mode is used.
- KeepSourceNumbering: Specifies how to import the numbering when it conflicts in source and destination documents.
- SmartStyleBehavior: Specifies how to import styles when they have the same names in source and destination documents.
다음은 추가 옵션을 사용하여 여러 Word 문서를 병합하는 단계입니다.
- Document 클래스를 사용하여 대상 문서를 로드합니다.
- Document 클래스를 사용하여 소스 문서를 로드합니다.
- ImportFormatOptions 클래스의 인스턴스를 만들고 원하는 옵션을 설정합니다.
- Document->AppendDocument( System::SharedPtr)를 사용하여 문서를 병합합니다.Aspose::Words::Document srcDoc, Aspose::Words::ImportFormatMode importFormatMode, 시스템::SharedPtrAspose::Words::ImportFormatOptions importFormatOptions) 메서드.
- Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat) 메서드를 사용하여 대상 Word 문서를 저장합니다.
다음은 Word 문서를 추가 옵션과 병합하는 샘플 코드입니다.
// 병합할 샘플 Word 문서
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";
// 병합할 Word 문서 로드
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);
// 옵션 설정
auto options = MakeObject<Aspose::Words::ImportFormatOptions>();
options->set_IgnoreHeaderFooter(false);
// 소스 파일의 형식을 유지하면서 문서 병합
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting, options);
// 출력 파일 경로
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";
// 병합된 문서를 DOCX 파일로 저장
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);
다음 이미지는 IgnoreHeaderFooter 옵션을 true와 false로 설정하여 생성된 병합 문서를 비교합니다.
무료 라이선스 받기
무료 임시 라이선스를 요청하면 평가 제한 없이 API를 사용해 볼 수 있습니다.
결론
이 기사에서는 C++를 사용하여 여러 Word 문서를 병합하는 방법을 배웠습니다. 또한 Word 파일 병합을 사용자 지정하기 위한 추가 옵션을 사용하는 방법을 배웠습니다. Aspose.Words for C++ API는 Word 파일 작업을 위한 다양한 기능을 제공합니다. 공식 문서를 통해 API를 자세히 살펴볼 수 있습니다. 문의사항이 있으시면 포럼으로 연락주시기 바랍니다.