이 예에서는 C++를 사용하여 DOCX에 이미지를 추가하는 방법을 배웁니다. C++를 사용하여 워드 문서 내부에 이미지를 삽입하는 것은 개발자들이 일반적으로 묻는 요구 사항 중 하나입니다. C++를 사용하여 DOC에 이미지를 추가하는 것은 간단한 API 호출을 사용하여 달성할 수 있습니다.
C++를 사용하여 DOCX에 이미지를 추가하는 단계
- 최신 Aspose.Words.Cpp NuGet 패키지 다운로드 및 설치
- Aspose::Words 네임스페이스에 대한 참조 추가
- Document Class 개체를 인스턴스화하여 이미지 추가를 위한 DOCX 로드
- DOCX 내 콘텐츠 작업을 위해 DocumentBuilder 클래스 인스턴스화
- imageWriter 메서드를 사용하여 문서 내부에 JPEG 이미지 로드 및 추가
- Save 메서드를 사용하여 C++에서 이미지와 함께 DOCX 저장
다음 예에서는 API 호출을 거의 사용하지 않고 MS Word에 대한 외부 종속성이 없어 C++에서 DOCX에 JPEG 이미지를 쉽게 추가할 수 있습니다. API를 사용하면 Word 문서에 JPEG, PNG, GIF 또는 EMF 이미지를 추가할 수 있습니다.
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++를 사용하여 Word 문서에 이미지를 삽입하는 방법을 배웠습니다.