Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách chèn nhận xét vào DOCX bằng C++. Nhận xét rất hữu ích để xem lại tài liệu Word. Chúng không tự thay đổi tài liệu nhưng được các tác giả sử dụng để thảo luận hoặc nhận xét về bất kỳ phần nào của Tài liệu Word.
Các bước để thêm nhận xét trong DOCX bằng C++
- Cài đặt gói NuGet Aspose.Words.Cpp
- Bao gồm tham chiếu đến không gian tên Aspose::Words
- Tạo phiên bản Document Class để tải DOCX để thêm nhận xét
- Khởi tạo đối tượng Comment Class để thêm nhận xét trong DOCX bằng C++
- Lưu tệp Word có Nhận xét trong C ++ bằng cách sử dụng phương thức Lưu
Trong đoạn mã sau, chúng tôi đã chỉ ra cách bạn có thể đưa nhận xét vào tệp DOC dễ dàng như thế nào bằng C++ mà không phụ thuộc vào Microsoft Word hoặc Office Interop API. Bạn có thể sử dụng SaveFormat để lưu tài liệu ở định dạng DOCX hoặc DOC.
Mã để thêm nhận xét trong DOCX bằng C++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/Body.h> | |
#include <Aspose.Words.Cpp/Comment.h> | |
#include <Aspose.Words.Cpp/CommentCollection.h> | |
#include <Aspose.Words.Cpp/CommentRangeEnd.h> | |
#include <Aspose.Words.Cpp/CommentRangeStart.h> | |
#include <Aspose.Words.Cpp/CompositeNode.h> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Node.h> | |
#include <Aspose.Words.Cpp/NodeCollection.h> | |
#include <Aspose.Words.Cpp/NodeType.h> | |
#include <Aspose.Words.Cpp/Paragraph.h> | |
#include <Aspose.Words.Cpp/ParagraphCollection.h> | |
#include <Aspose.Words.Cpp/Run.h> | |
#include <Aspose.Words.Cpp/RunCollection.h> | |
#include <Aspose.Words.Cpp/SaveFormat.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <Aspose.Words.Cpp/Section.h> | |
#include <system/collections/list.h> | |
#include <system/convert.h> | |
#include <system/date_time.h> | |
#include <system/enumerator_adapter.h> | |
#include <system/exceptions.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
class WorkingWithCommentsinCPP | |
{ | |
public: | |
void AddComments() | |
{ | |
// Create an instance of Document class of Aspose.Words for C++ | |
// to add a blank Word document | |
SharedPtr<Document> WordDocumentUsingCPP = MakeObject<Document>(); | |
// Instantiate DocumentBuilder class to add content to the Word Document | |
SharedPtr<DocumentBuilder> WordDocumentBuilder = MakeObject<DocumentBuilder>(WordDocumentUsingCPP); | |
// Add some text | |
WordDocumentBuilder->Write(u"Some text is added."); | |
// Add comment class object to add comment | |
SharedPtr<Comment> comment = MakeObject<Comment>(WordDocumentUsingCPP, u"Test Author", u"TA", | |
System::DateTime::get_Today()); | |
// Append comment to current paragraph of document | |
WordDocumentBuilder->get_CurrentParagraph()->AppendChild(comment); | |
// Add Comment | |
comment->get_Paragraphs()->Add(MakeObject<Paragraph>(WordDocumentUsingCPP)); | |
comment->get_FirstParagraph()->get_Runs()->Add(MakeObject<Run>(WordDocumentUsingCPP, u"Comment text.")); | |
// Save the word document with comments to docx format | |
WordDocumentUsingCPP->Save(u"WorkingWithComments.AddComments.docx"); | |
} | |
}; |
Trong ví dụ trước, chúng ta đã thấy Cách chuyển đổi HTML sang PDF bằng C ++. Trong chủ đề này, chúng tôi đã giải thích cách chèn nhận xét trong DOCX bằng C++ bằng một vài dòng mã.