翻譯|使用教程|編輯:胡濤|2022-10-19 11:05:59.047|閱讀 265 次
概述:本文主要向大家介紹如何使用 C++ 處理 Word 文檔中的注釋?zhuān)瑲g迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
Microsoft Word 使您能夠向 Word 文檔添加注釋。在建議改進(jìn)文檔或分享對(duì)文本的想法等情況下,評(píng)論可能會(huì)有所幫助。在某些情況下,您可能需要以編程方式管理評(píng)論。為此,本文將教您如何使用 C++ 處理 Word 文檔中的注釋。
Aspose.Words for C++是一個(gè)原生 C++ 庫(kù),允許您創(chuàng)建、閱讀、修改和轉(zhuǎn)換 Microsoft Word 文檔。此外,它還支持處理DOCX和DOC文件中的注釋。您可以通過(guò)NuGet安裝 API,也可以直接從“下載”部分下載。
PM> Install-Package Aspose.Words.Cpp
Aspose.Words for C++ API 提供了添加帶有作者姓名、首字母縮寫(xiě)和日期/時(shí)間的評(píng)論的能力。以下是向 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); }
要修改評(píng)論,請(qǐng)使用NodeCollection->idx_get(int32_t index)方法檢索它并根據(jù)需要進(jìn)行更改。以下是修改 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 文檔中刪除注釋的方法。在本節(jié)中,您將學(xué)習(xí)如何使用 C++ 刪除特定評(píng)論、作者評(píng)論和所有評(píng)論。
刪除特定評(píng)論
以下是刪除特定評(píng)論的步驟。
以下示例代碼顯示如何使用 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");
按作者刪除評(píng)論
以下是按作者刪除評(píng)論的步驟。
以下是作者使用 C++ 刪除評(píng)論的示例代碼。
// 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()方法一次刪除所有評(píng)論,而不是刪除單個(gè)評(píng)論。以下是從 Word 文檔中刪除所有評(píng)論的步驟。
以下示例代碼演示了如何使用 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 文檔中的注釋詳細(xì)步驟 ,要是您還有其他關(guān)于產(chǎn)品方面的問(wèn)題,歡迎咨詢(xún)我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn