วิธีสร้าง DOCX ใน C++

ในหัวข้อนี้ เราจะเรียนรู้วิธี สร้าง DOCX ใน C+* เอกสาร Word ถูกสร้างขึ้นโดยทางโปรแกรมโดยเฉพาะอย่างยิ่งใน C ++ และใช้แอปพลิเคชันจำนวนมากเพื่อให้ได้ระบบอัตโนมัติ เช่น การสร้างบิลค่าสาธารณูปโภค คุณสามารถสร้าง DOCX ได้ทันทีใน C++ โดยใช้อินเทอร์เฟซ API อย่างง่าย

ขั้นตอนในการสร้าง DOCX ใน C++

  1. รวม Aspose.Words.Cpp โดยใช้ NuGet package Manager
  2. เพิ่มการอ้างอิงถึง Aspose::Words และ Aspose::Words::Saving และ System::Drawing เนมสเปซ
  3. สร้างอินสแตนซ์ของ Document Class เพื่อเพิ่มเอกสาร Word เปล่า
  4. สร้างอินสแตนซ์ DocumentBuilder Class เพื่อประมวลผลเอกสาร Word
  5. เพิ่มข้อความในเอกสารพร้อมกับการจัดรูปแบบ
  6. บันทึกเอกสาร Word ด้วยการจัดรูปแบบที่กำหนดเองในรูปแบบ DOCX โดยใช้วิธีบันทึก

ในตัวอย่างต่อไปนี้ เราได้แสดงวิธี สร้างเอกสารคำใน 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++

 ไทย