Trong chủ đề này, chúng ta sẽ xem cách tạo bảng trong DOCX bằng C++. Bảng thường được sử dụng bên trong tài liệu từ và bạn có thể chèn bảng trong DOCX bằng C++. Bạn có thể chèn bảng Word trong C++ bằng các lệnh gọi API đơn giản.
Các bước để thêm bảng trong DOCX bằng C++
- Cài đặt phiên bản mới nhất của Aspose.Words.Cpp từ NuGet
- Bao gồm tham chiếu đến không gian tên Aspose::Words
- Tạo đối tượng Document Class để tải DOCX để thêm bảng
- Khởi tạo DocumentBuilder Class để quản lý nội dung bên trong DOCX
- Sử dụng DocumentBuilder để thêm hàng, ô và hình ảnh bên trong ô
- Lưu DOCX bằng Bảng trong C++ bằng cách sử dụng phương thức Lưu
Trong ví dụ bên dưới, bạn có thể tạo bảng Word bằng C++ mà không phụ thuộc vào Microsoft Word. Bạn có thể định dạng nội dung bên trong các ô của bảng và chúng tôi cũng đã chỉ ra cách thêm hình ảnh vào bên trong ô.
Mã để thêm bảng trong DOCX bằng C++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBase.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Drawing/Shape.h> | |
#include <Aspose.Words.Cpp/HeaderFooterType.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/array.h> | |
#include <system/exceptions.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <system/io/path.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
class CreateTableInWordDocumentUsingCpp | |
{ | |
public: | |
void AddTableinWordDocument() | |
{ | |
// Set License file name | |
System::String LicenseFile = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicenseForTable = System::MakeObject<License>(); | |
// Setting product license | |
wordsLicenseForTable->SetLicense(LicenseFile); | |
// Instantiate Document class to load DOCX and add table | |
SharedPtr<Document> AddTableToWordDOC = MakeObject<Document>(u"WordDocument.docx"); | |
// Instantiate DocumentBuilder class to manage document content | |
SharedPtr<DocumentBuilder> TableWriter = MakeObject<DocumentBuilder>(AddTableToWordDOC); | |
// Mark the start of table | |
TableWriter->StartTable(); | |
// Insert Row and first Cell inside Word Table | |
TableWriter->InsertCell(); | |
// Add some text in Table Cell | |
TableWriter->Write(u"Table Row 1 and Cell 1"); | |
// Add a new Cell inside Row | |
TableWriter->InsertCell(); | |
// Insert an Image in Word Table Cell | |
TableWriter->InsertImage(u"image in table.jpg"); | |
// Mark end of Table Row | |
TableWriter->EndRow(); | |
// Mark end of Word Table creation | |
TableWriter->EndTable(); | |
// Save the word document with table to docx format | |
AddTableToWordDOC->Save(u"InsertTableinDocx.docx"); | |
} | |
}; |
Trước đây, chúng ta đã thấy Cách thêm hình ảnh vào DOCX bằng C++. Tuy nhiên, trong chủ đề này, chúng ta đã học cách chèn bảng trong DOCX bằng C++.