در این مبحث با نحوه ایجاد DOCX در C++ آشنا خواهیم شد. اسناد Word به طور برنامه نویسی به خصوص در C++ ایجاد می شوند و از برنامه های بسیاری برای دستیابی به اتوماسیون مانند تولید صورتحساب ابزار استفاده می شود. میتوانید با استفاده از رابط کاربری ساده API، DOCX را به سرعت در C++ ایجاد کنید.
مراحل ایجاد DOCX در C++
- Aspose.Words.Cpp را با استفاده از مدیریت بسته NuGet اضافه کنید
- افزودن مرجع به Aspose::Words و Aspose::Words::Saving و فضای نام سیستم::طراحی
- برای افزودن یک سند Word خالی، نمونه ای از Document Class ایجاد کنید
- نمونه DocumentBuilder Class را برای پردازش سند Word ایجاد کنید
- اضافه کردن متن در سند همراه با قالب بندی
- سند Word را با قالب بندی سفارشی در فرمت DOCX با استفاده از روش Save ذخیره کنید
در مثال زیر نشان دادهایم که چگونه میتوانید با افزودن متن و تنظیم قالببندی آن، سند word را در C++** ایجاد کنید. همچنین خواهید دید که چگونه می توانید متن را در سند خود پیوند دهید.
کد برای ایجاد DOCX در C++
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <Aspose.Words.Cpp/Font.h> | |
#include <Aspose.Words.Cpp/Border.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Saving/CompressionLevel.h> | |
#include <Aspose.Words.Cpp/Saving/OoxmlCompliance.h> | |
#include <Aspose.Words.Cpp/Saving/OoxmlSaveOptions.h> | |
#include <system/io/file.h> | |
#include <drawing/color.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Saving; | |
using namespace System::Drawing; | |
class CreatingWordDocumentinCPP | |
{ | |
public: | |
void CreateWordDocumentinCPP() | |
{ | |
// Access liense file and set for API | |
System::String WordsLicenseFileName = u"Aspose.Total.NET.lic"; | |
SharedPtr<License> wordsLic = System::MakeObject<License>(); | |
wordsLic->SetLicense(WordsLicenseFileName); | |
// Create Document Class instance to add a blank Word document | |
SharedPtr<Document> WordDocUsingCPP = MakeObject<Document>(); | |
// Create instance of DocumentBuilder class to process the Word Document | |
SharedPtr<DocumentBuilder> WordDocBuilder = MakeObject<DocumentBuilder>(WordDocUsingCPP); | |
// Insert a string surrounded by a border | |
WordDocBuilder->get_Font()->get_Border()->set_Color(Color::get_Green()); | |
WordDocBuilder->get_Font()->get_Border()->set_LineWidth(2.5); | |
WordDocBuilder->get_Font()->get_Border()->set_LineStyle(LineStyle::DashDotStroker); | |
WordDocBuilder->Write(u"Text with a green border around it."); | |
// Remove the font formatting and set it explicitly | |
WordDocBuilder->get_Font()->ClearFormatting(); | |
// Adding paragraph break for text | |
WordDocBuilder->InsertBreak(BreakType::ParagraphBreak); | |
WordDocBuilder->Write(u"For more info, please visit oue site"); | |
// Add a hyperlink and using custom formatting. The hyperlink shall | |
// be a clickable text which will redirect to URL set inside it | |
WordDocBuilder->get_Font()->set_Color(Color::get_Blue()); | |
WordDocBuilder->get_Font()->set_Underline(Underline::Single); | |
WordDocBuilder->InsertHyperlink(u"Aspose Knowledge Base", u"https://kb.aspose.com/", false); | |
WordDocBuilder->get_Font()->ClearFormatting(); | |
WordDocBuilder->Writeln(u"."); | |
SharedPtr <OoxmlSaveOptions> DocSaveOptions = MakeObject<OoxmlSaveOptions>(); | |
DocSaveOptions->set_Compliance(OoxmlCompliance::Iso29500_2008_Strict); | |
DocSaveOptions->set_SaveFormat(SaveFormat::Docx); | |
// Save the generated DOCX file using Save method | |
WordDocUsingCPP->Save(u"Word_Doc_Created_using_CPP.docx", DocSaveOptions); | |
} | |
}; |
در مثال قبلی، ما به نحوه اضافه کردن ردیف به جدول در DOCX با استفاده از ++C نگاه کردیم. این موضوع بر روی تولید یک سند Word از ابتدا با استفاده از C++ تمرکز دارد.