ในตัวอย่างนี้ เราจะเรียนรู้วิธีการเพิ่มรูปภาพใน DOCX โดยใช้ C++ การแทรกรูปภาพภายในเอกสารเวิร์ดโดยใช้ C++ เป็นข้อกำหนดหนึ่งที่นักพัฒนาถามกันโดยทั่วไป การเพิ่มรูปภาพไปยัง DOC โดยใช้ C++ ทำได้โดยใช้การเรียก API อย่างง่าย
ขั้นตอนการเพิ่มรูปภาพใน DOCX โดยใช้ C++
- ดาวน์โหลดและติดตั้งแพ็คเกจ Aspose.Words.Cpp NuGet ล่าสุด
- เพิ่มการอ้างอิงไปยังเนมสเปซ Aspose::Words
- สร้างอินสแตนซ์ Document Class วัตถุเพื่อโหลด DOCX เพื่อเพิ่มรูปภาพ
- สร้างอินสแตนซ์คลาส DocumentBuilder เพื่อทำงานกับเนื้อหาภายใน DOCX
- ใช้วิธี imageWriter เพื่อโหลดและเพิ่มภาพ JPEG ภายในเอกสาร
- บันทึก DOCX ด้วยรูปภาพใน C++ โดยใช้วิธีบันทึก
ในตัวอย่างต่อไปนี้ คุณสามารถเพิ่มอิมเมจ JPEG ลงใน DOCX ใน C++ ได้ง่ายๆ โดยใช้การเรียก API เพียงไม่กี่ครั้ง และไม่ต้องพึ่งพา MS Word จากภายนอก API อนุญาตให้คุณเพิ่มรูปภาพ JPEG, PNG, GIF หรือ EMF ภายในเอกสาร Word
รหัสเพื่อเพิ่มรูปภาพใน DOCX โดยใช้ 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; | |
using namespace Aspose::Words::Drawing; | |
class AddContentUsingDocumentBuilder | |
{ | |
public: | |
void AddImageinWordDocument() | |
{ | |
// File name and path of license file | |
System::String LicenseFileName = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicense = System::MakeObject<License>(); | |
// Setting product license | |
wordsLicense->SetLicense(LicenseFileName); | |
// Instantiate Document class to load DOCX and add image | |
SharedPtr<Document> AddImagesToWordDOC = MakeObject<Document>(u"input.docx"); | |
// Instantiate DocumentBuilder class to work with content inside DOCX | |
SharedPtr<DocumentBuilder> imageWriter = MakeObject<DocumentBuilder>(AddImagesToWordDOC); | |
// Take cursor to Primary Header in document | |
imageWriter->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary); | |
// Insert image in word document using C++ | |
SharedPtr<Shape> headerImage = imageWriter->InsertImage(u"Add Image in Word Header.jpg"); | |
// Set Image Size in Header | |
headerImage->set_Width(1 * 72); // equals to one inch | |
headerImage->set_Height(1 * 72); | |
// Save the word document with image to docx format | |
AddImagesToWordDOC->Save(u"InsertImageinDocx.docx"); | |
} | |
}; |
ก่อนหน้านี้ เราตรวจสอบ วิธีแปลงเอกสาร Word เป็นรูปภาพโดยใช้ C ++ ในหัวข้อนี้ เราได้เรียนรู้วิธีการแทรกรูปภาพลงในเอกสาร Word โดยใช้ C++