翻譯|使用教程|編輯:胡濤|2023-01-29 09:54:14.860|閱讀 175 次
概述:在本文中,您將學習如何使用Aspose.Words for C++并使用 C++ 從頭開始創(chuàng)建 MS Word 文檔。歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
Aspose.Words是一個功能豐富的 API 集合,可讓您以編程方式創(chuàng)建、編輯和轉換 MS Word 文檔。它為操作文字處理文檔提供了廣泛的基本和高級功能。在本文中,您將學習如何使用Aspose.Words for C++并使用 C++ 從頭開始創(chuàng)建 MS Word 文檔。分步指南和代碼示例將讓您了解如何在 Word 文檔中插入文本、圖像、表格、列表和其他元素。
Aspose.Words for C++允許您在沒有 MS Word 的情況下在 C++ 應用程序中生成和操作文字處理文檔。您可以通過以下命令使用NuGet下載API 或將其安裝在您的 C++ 應用程序中。
PM> Install-Package Aspose.Words.Cpp
讓我們首先創(chuàng)建一個簡單的 Word 文檔并將其另存為.doc或.docx文件。為此,您需要執(zhí)行以下步驟:
以下代碼示例顯示了如何使用 C++ 創(chuàng)建 Word DOCX 文檔。
// Initialize a Document. System::SharedPtr<Document> doc = System::MakeObject<Document>(); // Use a document builder to add content to the document. System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Add text builder->Writeln(u"Hello World!"); // Save the document to disk. doc->Save(u"document.docx");
您還可以使用 Aspose.Words for C++ 編輯現(xiàn)有的 Word 文檔。為此,API 使用文檔對象模型 (DOM) 作為文檔的內存表示。DOM 允許您訪問 Word 文檔的元素,例如頁眉/頁腳、段落、表格等。在此處閱讀有關 DOM 的更多信息。
要更新 Word 文檔,只需使用Document類加載它并根據(jù)需要進行處理。以下是編輯和更新現(xiàn)有 Word 文檔的步驟。
下面的代碼示例顯示了如何使用 C++ 更新 Word 文檔中段落的文本。
// Initialize a Document. System::SharedPtr<Document> doc = System::MakeObject<Document>(u"document.docx"); // Use a document builder to add content to the document. System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Get section auto section = doc->get_Sections()->idx_get(0); // Get body auto body = section->get_Body(); // Get first paragraph auto para = body->get_FirstParagraph(); // Update text auto run = para->get_Runs()->idx_get(0); run->set_Text(u"This is the updated text."); // Save the document to disk. doc->Save(u"updated_document.docx");
以下是使用 C++ 在 MS Word 文檔中插入圖像的步驟。
下面的代碼示例顯示了如何使用 C++ 將圖像插入到 Word 文檔中。
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Add a logo to the top left of the page. The image is placed in front of all other text. System::SharedPtr<Shape> shape = builder->InsertImage( u"Aspose Logo.png", RelativeHorizontalPosition::Page, 60.0, RelativeVerticalPosition::Page, 60.0, -1.0, -1.0, WrapType::None); doc->Save(u"document_with_image.docx");
表格是 Word 文檔的重要元素,用于以行和列的形式保存數(shù)據(jù)。為了在 Word 文檔中生成表格,請按照以下步驟操作。
以下代碼示例顯示如何使用 C++ 在 Word 文檔中插入表格 。
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<Table> table = System::MakeObject<Table>(doc); // Add the table to the document. doc->get_FirstSection()->get_Body()->AppendChild(table); System::SharedPtr<Row> row = System::MakeObject<Row>(doc); row->get_RowFormat()->set_AllowBreakAcrossPages(true); table->AppendChild(row); // We can now apply any auto fit settings. table->AutoFit(AutoFitBehavior::FixedColumnWidths); // Create a cell and add it to the row System::SharedPtr<Cell> cell = System::MakeObject<Cell>(doc); cell->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue()); cell->get_CellFormat()->set_Width(80); // Add a paragraph to the cell as well as a new run with some text. cell->AppendChild(System::MakeObject<Paragraph>(doc)); cell->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 1 Text")); // Add the cell to the row. row->AppendChild(cell); // We would then repeat the process for the other cells and rows in the table. // We can also speed things up by cloning existing cells and rows. row->AppendChild((System::StaticCast<Node>(cell))->Clone(false)); row->get_LastCell()->AppendChild(System::MakeObject<Paragraph>(doc)); row->get_LastCell()->get_FirstParagraph()->AppendChild(System::MakeObject<Run>(doc, u"Row 1, Cell 2 Text")); // Save the document to disk. doc->Save(u"document_with_table.docx");
最后但同樣重要的是,在 Word 文檔中創(chuàng)建一個列表。以下是創(chuàng)建項目符號列表的步驟。
以下代碼示例顯示如何使用 C++ 在 Word 文檔中創(chuàng)建列表。
System::SharedPtr<Document> doc = System::MakeObject<Document>(); System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); // Create a numbered list based on one of the Microsoft Word list templates and // apply it to the current paragraph in the document builder. builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::NumberArabicDot)); // There are 9 levels in this list, lets try them all. for (int32_t i = 0; i < 9; i++) { builder->get_ListFormat()->set_ListLevelNumber(i); builder->Writeln(System::String(u"Level ") + i); } // Create a bulleted list based on one of the Microsoft Word list templates // and apply it to the current paragraph in the document builder. builder->get_ListFormat()->set_List(doc->get_Lists()->Add(ListTemplate::BulletDiamonds)); // There are 9 levels in this list, lets try them all. for (int32_t i = 0; i < 9; i++) { builder->get_ListFormat()->set_ListLevelNumber(i); builder->Writeln(System::String(u"Level ") + i); } // This is a way to stop list formatting. builder->get_ListFormat()->set_List(nullptr); // Save the document to disk. builder->get_Document()->Save(u"document_with_list.docx");
以上便是如何使用 C++ 創(chuàng)建 MS Word 文檔 (DOC/DOCX) 步驟 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn