C++を使用してDOCXに画像を追加する方法

この例では、C++を使用してDOCXに画像を追加する方法を学習します。 C ++を使用してWord文書内に画像を挿入することは、開発者がよく尋ねる要件の1つです。 C ++を使用してDOCに画像を追加するには、単純なAPI呼び出しを使用します。

C++を使用してDOCXに画像を追加する手順

  1. 最新のAspose.Words.CppNuGetパッケージをダウンロードしてインストールします
  2. Aspose::Words名前空間への参照を追加します
  3. Document Classオブジェクトをインスタンス化して、画像を追加するためのDOCXをロードします
  4. DocumentBuilderクラスをインスタンス化して、DOCX内のコンテンツを操作します
  5. imageWriterメソッドを使用して、ドキュメント内にJPEG画像を読み込んで追加します
  6. 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文書に画像を挿入する方法を学びました。

 日本語