MS Word là một công cụ mạnh mẽ cho phép bạn tùy chỉnh và định dạng các tài liệu chuyên nghiệp của mình với một bộ tính năng tuyệt vời. Bạn có thể rơi vào tình huống muốn hợp nhất nhiều tài liệu Word, chẳng hạn như kết hợp tất cả các biên nhận trong một tệp duy nhất hoặc kết hợp các tài liệu yêu cầu phần mềm khác nhau để quản lý dễ dàng hơn. Hơn nữa, việc kết hợp các tệp sẽ đơn giản hóa quá trình chia sẻ chúng. Trong bài viết này, bạn sẽ học cách hợp nhất nhiều tài liệu MS Word bằng C ++.
- API C ++ để hợp nhất tài liệu Word
- Kết hợp tài liệu Word bằng C ++
- Hợp nhất tài liệu Word với các tùy chọn bổ sung
- Nhận giấy phép miễn phí
API C ++ để hợp nhất tài liệu Word
Aspose.Words for C ++ là một thư viện C ++ gốc cho phép bạn tạo, thay đổi và chuyển đổi các tài liệu Microsoft Word. Ngoài ra, nó còn hỗ trợ kết hợp nhiều văn bản Word thành một tập tin duy nhất. Bạn có thể cài đặt API thông qua NuGet hoặc tải xuống trực tiếp từ phần Tải xuống.
PM> Install-Package Aspose.Words.Cpp
Hợp nhất tài liệu Word bằng C ++
Việc hợp nhất các tài liệu Word với API Aspose. AdWords dành cho C ++ thật dễ dàng. Bạn có thể tải và hợp nhất chúng chỉ với một vài dòng mã. Sau đây là hình ảnh hiển thị hai file Word mẫu mà chúng tôi sẽ kết hợp trong bài viết này.
Sau đây là các bước để hợp nhất tài liệu Word bằng C ++.
- Tải tài liệu đích bằng lớp Tài liệu.
- Tải tài liệu nguồn bằng lớp Tài liệu.
- Sử dụng Document-> AppendDocument (System :: SharedPtrAspose::Words::Document srcDoc, Aspose :: Words :: ImportFormatMode importFormatMode) của cá thể tài liệu đích để hợp nhất cả hai tài liệu.
- Đặt tùy chọn tạo kiểu để nhập tài liệu nguồn bằng enum ImportFormatMode.
- Lưu tài liệu Word đã hợp nhất bằng phương pháp Document-> Save (System :: String fileName, Aspose :: Words :: SaveFormat saveFormat).
Sau đây là mã mẫu để trộn tài liệu Word.
// Tài liệu Word mẫu sẽ được hợp nhất
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";
// Tải tài liệu Word để được hợp nhất
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);
// Hợp nhất các tài liệu giữ định dạng của tệp nguồn
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting);
// Đường dẫn tệp đầu ra
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";
// Lưu tài liệu đã hợp nhất dưới dạng tệp DOCX
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);
Hình ảnh sau đây so sánh các tài liệu đã hợp nhất được tạo bằng cách sử dụng ImportFormatMode :: KeepSourceFormatting và [ImportFormatMode](https: // tham chiếu. aspose.com/words/cpp/namespace/aspose.words#aafaa52cbf0baa49c3225787c23a8c949)::UseDestinationStyles chế độ.
Hợp nhất tài liệu Word với các tùy chọn bổ sung
API Aspose.Words dành cho C ++ cung cấp lớp ImportFormatOptions để tùy chỉnh việc hợp nhất các tệp Word. Sau đây là các tùy chọn được cung cấp bởi lớp ImportFormatOptions.
- DubreHeaderFooter: Chỉ định xem có bỏ qua định dạng nội dung đầu trang / chân trang hay không khi ImportFormatMode :: KeepSourceFormatting mode được sử dụng.
- 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.
Sau đây là các bước để hợp nhất nhiều tài liệu Word với các tùy chọn bổ sung:
- Tải tài liệu đích bằng lớp Tài liệu.
- Tải tài liệu nguồn bằng lớp Tài liệu.
- Tạo một phiên bản của lớp ImportFormatOptions và đặt các tùy chọn mong muốn.
- Hợp nhất các tài liệu bằng Document-> AppendDocument (System :: SharedPtrAspose::Words::Document srcDoc, Aspose :: Words :: ImportFormatMode importFormatMode, System :: SharedPtrAspose::Words::ImportFormatOptions importFormatOptions).
- Lưu tài liệu Word đích bằng phương thức Document-> Save (System :: String fileName, Aspose :: Words :: SaveFormat saveFormat).
Sau đây là mã mẫu để hợp nhất tài liệu Word với các tùy chọn bổ sung.
// Tài liệu Word mẫu sẽ được hợp nhất
System::String sampleFile1 = u"SourceDirectory\\Sample 1.docx";
System::String sampleFile2 = u"SourceDirectory\\Sample 2.docx";
// Tải tài liệu Word để được hợp nhất
System::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);
System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);
// Đặt các tùy chọn
auto options = MakeObject<Aspose::Words::ImportFormatOptions>();
options->set_IgnoreHeaderFooter(false);
// Hợp nhất các tài liệu giữ định dạng của tệp nguồn
document1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting, options);
// Đường dẫn tệp đầu ra
System::String outputPath = u"OutputDirectory\\merged-doc-out.docx";
// Lưu tài liệu đã hợp nhất dưới dạng tệp DOCX
document1->Save(outputPath, Aspose::Words::SaveFormat::Docx);
Các hình ảnh sau đây so sánh các tài liệu đã hợp nhất được tạo ra bằng cách đặt tùy chọn BrowsereHeaderFooter thành true và false.
Nhận giấy phép miễn phí
Bạn có thể dùng thử API mà không có giới hạn đánh giá bằng cách yêu cầu giấy phép tạm thời miễn phí.
Sự kết luận
Trong bài viết này, bạn đã học cách hợp nhất nhiều tài liệu Word bằng C ++. Hơn nữa, bạn đã học cách sử dụng các tùy chọn bổ sung để tùy chỉnh việc hợp nhất các tệp Word. API Aspose. AdWords dành cho C ++ cung cấp một loạt các tính năng để làm việc với các tệp Word. Bạn có thể khám phá API chi tiết bằng cách sử dụng tài liệu chính thức. Nếu bạn có bất kỳ câu hỏi nào, vui lòng liên hệ với chúng tôi trên diễn đàn.