इस उदाहरण में, हम सीखेंगे कि C++ का उपयोग करके DOCX में इमेज कैसे जोड़ें। सी ++ का उपयोग करके वर्ड डॉक्यूमेंट के अंदर छवियों को सम्मिलित करना डेवलपर्स द्वारा सामान्य रूप से पूछे जाने वाली आवश्यकताओं में से एक है। सी ++ का उपयोग करके डीओसी में छवि जोड़ना सरल एपीआई कॉल का उपयोग करके हासिल किया जा सकता है।
C++ का उपयोग करके DOCX में छवि जोड़ने के चरण
- नवीनतम Aspose.Words.Cpp NuGet पैकेज डाउनलोड और इंस्टॉल करें
- Aspose::Words नाम स्थान का संदर्भ जोड़ें
- छवि जोड़ने के लिए DOCX लोड करने के लिए Document Class ऑब्जेक्ट को तत्काल करें
- DOCX के अंदर सामग्री के साथ काम करने के लिए DocumentBuilder कक्षा को तत्काल करें
- दस्तावेज़ के अंदर JPEG छवि लोड करने और जोड़ने के लिए imageWriter विधि का उपयोग करें
- सेव मेथड का उपयोग करके DOCX को इमेज के साथ C++ में सेव करें
निम्नलिखित उदाहरण में, आप कुछ एपीआई कॉलों का उपयोग करके और एमएस वर्ड पर कोई बाहरी निर्भरता न रखते हुए आसानी से JPEG इमेज को C++ में DOCX में जोड़ सकते हैं। एपीआई आपको वर्ड दस्तावेज़ के अंदर जेपीईजी, पीएनजी, जीआईएफ या ईएमएफ छवियों को जोड़ने की अनुमति देता है।
C++ का उपयोग करके DOCX में छवि जोड़ने के लिए कोड
#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"); | |
} | |
}; |
इससे पहले, हमने C++ का उपयोग करके Word दस्तावेज़ को छवियों में कैसे बदलें? में देखा था। जबकि इस टॉपिक में हमने सीखा कि कैसे C++ का उपयोग करके वर्ड डॉक्यूमेंट में इमेज इन्सर्ट करें।