Ebben a példában megtudjuk, hogyan adhat hozzá képet a DOCX nyelven C++ használatával. A képek beillesztése Word dokumentumba C++ használatával a fejlesztők által gyakran felkért követelmény. Kép hozzáadása a DOC-hoz C++ használatával egyszerű API-hívásokkal érhető el.
Lépések a kép hozzáadásához DOCX-ben C++ használatával
- Töltse le és telepítse a legújabb Aspose.Words.Cpp NuGet-csomagot
- Adjon hozzá hivatkozást a Aspose::Words névtérhez
- Példányosítsa a Document Class objektumot a DOCX betöltéséhez kép hozzáadásához
- Példányosítsa a DocumentBuilder osztályt a DOCX-en belüli tartalommal való munkavégzéshez
- Használja az imageWriter módszert a JPEG kép betöltéséhez és hozzáadásához a dokumentumban
- Mentse a DOCX-et képpel C++ nyelven a Mentés módszerrel
A következő példában egyszerűen hozzáadhat JPEG képet a DOCX-hez C++-ban, kevés API-hívás használatával, és nincs külső függősége az MS Word-től. Az API lehetővé teszi JPEG, PNG, GIF vagy EMF képek hozzáadását a Word dokumentumhoz.
Kód a kép hozzáadásához DOCX-ben C++ használatával
#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"); | |
} | |
}; |
Korábban benéztünk ide: Word-dokumentumok konvertálása képekké a C++ használatával. Míg ebben a témakörben megtanultuk, hogyan lehet képet beilleszteni a Word dokumentumba C++ használatával.