翻譯|使用教程|編輯:胡濤|2022-10-19 11:05:59.047|閱讀 249 次
概述:本文主要向大家介紹如何使用 C++ 處理 Word 文檔中的注釋,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
Microsoft Word 使您能夠向 Word 文檔添加注釋。在建議改進文檔或分享對文本的想法等情況下,評論可能會有所幫助。在某些情況下,您可能需要以編程方式管理評論。為此,本文將教您如何使用 C++ 處理 Word 文檔中的注釋。
Aspose.Words for C++是一個原生 C++ 庫,允許您創建、閱讀、修改和轉換 Microsoft Word 文檔。此外,它還支持處理DOCX和DOC文件中的注釋。您可以通過NuGet安裝 API,也可以直接從“下載”部分下載。
PM> Install-Package Aspose.Words.Cpp
Aspose.Words for C++ API 提供了添加帶有作者姓名、首字母縮寫和日期/時間的評論的能力。以下是向 Word 文檔添加注釋的步驟。
以下示例代碼演示了如何使用 C++ 向 Word 文檔添加注釋。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"Sample 1.docx"); // Create an instance of the DocumentBuilder class System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Add comment System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Aspose", u"AFFA", System::DateTime::get_Today()); builder->get_CurrentParagraph()->AppendChild(comment); comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc)); comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text.")); // Save the document. doc->Save(outputDataDir + u"AddCommentsToExistingDoc.docx");
以下是示例代碼生成的輸出圖像。
以下是從 Word 文檔中讀取注釋的步驟。
以下是使用 C++ 從 Word 文檔中讀取注釋的示例代碼。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true); // Loop through all comments for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments)) { // Print comment information std::cout << comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text); }
要修改評論,請使用NodeCollection->idx_get(int32_t index)方法檢索它并根據需要進行更改。以下是修改 Word 文檔中的注釋的步驟。
以下示例代碼展示了如何使用 C++ 修改 Word 文檔中的注釋。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true); // Get comment System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(0)); // Update comment text comment->SetText(u"Updated Text"); // Save the document. doc->Save(outputDataDir + u"UpdatedComment.docx");
Aspose.Words for C++ API 提供了多種從 Word 文檔中刪除注釋的方法。在本節中,您將學習如何使用 C++ 刪除特定評論、作者評論和所有評論。
刪除特定評論
以下是刪除特定評論的步驟。
以下示例代碼顯示如何使用 C++ 從 Word 文檔中刪除特定注釋。
// Directory paths. System::String sourceDataDir = u"D:\\Work\\Aspose\\01_SourceDirectory\\"; System::String outputDataDir = u"D:\\Work\\Aspose\\02_OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true); // Get comment System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(2)); // Remove comment comment->Remove(); // Save the document. doc->Save(outputDataDir + u"DeleteSpecificComments.docx");
按作者刪除評論
以下是按作者刪除評論的步驟。
以下是作者使用 C++ 刪除評論的示例代碼。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true); // Loop through all comments and remove those written by the "Aspose" author. for (int32_t i = comments->get_Count() - 1; i >= 0; i--) { System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(i)); if (comment->get_Author() == u"Aspose") { comment->Remove(); } } // Save the document. doc->Save(outputDataDir + u"DeleteCommentsByAuthor.docx");
您可以使用NodeCollection->Clear()方法一次刪除所有評論,而不是刪除單個評論。以下是從 Word 文檔中刪除所有評論的步驟。
以下示例代碼演示了如何使用 C++ 從 Word 文檔中刪除所有注釋。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtr<Document> doc = System::MakeObject<Document>(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true); // Remove all comments. comments->Clear(); // Save the document. doc->Save(outputDataDir + u"DeleteAllComments.docx");
以上便是使用 C++ 處理 Word 文檔中的注釋詳細步驟 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn