翻譯|使用教程|編輯:李顯亮|2021-05-08 10:33:25.817|閱讀 263 次
概述:在某些情況下,需要以編程方式管理評論。為此,本文將介紹如何使用C ++在Word文檔中使用注釋。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Microsoft Word使您能夠向Word文檔添加注釋。在諸如建議改進文檔或共享文本思想等情況下,注釋可能會有所幫助。在某些情況下,需要以編程方式管理評論。為此,本文將教您如何使用C ++在Word文檔中使用注釋。
讓我們探索以下有關的內容:
Aspose.Words for C ++ 是本機C ++庫,允許您創建,讀取,修改和轉換Microsoft Word文檔。此外,它還支持處理DOCX和DOC文件中的注釋。
>>你可以點擊這里下載Aspose.Words for C ++ 最新版測試體驗。
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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true); // Loop through all comments for (System::SharedPtrcomment : System::IterateOver(comments)) { // Print comment information std::cout << comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast(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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true); // Get comment System::SharedPtrcomment = System::DynamicCast(comments->idx_get(0)); // Update comment text comment->SetText(u"Updated Text"); // Save the document. doc->Save(outputDataDir + u"UpdatedComment.docx");
可以使用NodeCollection-> Clear()方法一次刪除所有注釋,而不是刪除單個注釋。以下是刪除Word文檔中所有注釋的步驟。
下面的示例代碼演示了如何使用C ++從Word文檔中刪除所有注釋。
// Directory paths. System::String sourceDataDir = u"SourceDirectory\\"; System::String outputDataDir = u"OutputDirectory\\"; // Load the Word file System::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true); // Remove all comments. comments->Clear(); // Save the document. doc->Save(outputDataDir + u"DeleteAllComments.docx");
以下是刪除特定注釋的步驟。
下面的示例代碼顯示了如何使用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::SharedPtrdoc = System::MakeObject(sourceDataDir + u"SampleComments.docx"); // Retrieve comments System::SharedPtrcomments = doc->GetChildNodes(NodeType::Comment, true); // Get comment System::SharedPtrcomment = System::DynamicCast(comments->idx_get(2)); // Remove comment comment->Remove(); // Save the document. doc->Save(outputDataDir + u"DeleteSpecificComments.docx");
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn