このトピックでは、非常に簡単な方法でC++を使用してWord Documentを画像に変換する方法を示します。 Word文書の特定のページまたはすべてのページを、C ++のTiff、PNG、JPEG、BMPなどの任意の画像形式に変換できます。
C++を使用してWord文書を画像に変換する手順
- Aspose.Words.CppNuGetパッケージを使用する
- Aspose::Wordsと名前空間の保存への参照を含める
- Document Classのインスタンスを作成して、ソースWordファイルをロードします
- SaveFormatをPNGに設定します
- 画像として保存するページ範囲を設定し、コールバックを設定します
- Saveメソッドを使用してWordファイルをC++の画像に保存します
次の例では、MicrosoftWordやOfficeInterop APIに依存せずに、いくつかのAPI呼び出しを使用してC++でDOCXコメントを追加する方法を示しました。
C++を使用してWord文書を画像に変換するコード
#pragma once | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/io/file.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Saving/ImageSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/PageRange.h> | |
#include <Aspose.Words.Cpp/Saving/IPageSavingCallback.h> | |
#include <Aspose.Words.Cpp/Saving/ImageBinarizationMethod.h> | |
#include <Aspose.Words.Cpp/Saving/ImageColorMode.h> | |
#include <Aspose.Words.Cpp/Saving/ImagePixelFormat.h> | |
#include <Aspose.Words.Cpp/Saving/ImageSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/PageRange.h> | |
#include <Aspose.Words.Cpp/Saving/PageSavingArgs.h> | |
#include <Aspose.Words.Cpp/Saving/PageSet.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Saving; | |
class WorkingWithDocSaveOptions | |
{ | |
private: | |
class Word_Pages_To_Images : public IPageSavingCallback | |
{ | |
public: | |
void PageSaving(SharedPtr<PageSavingArgs> args) override | |
{ | |
args->set_PageFileName(String::Format(u"Page_{0}.png", args->get_PageIndex())); | |
} | |
}; | |
public: | |
void WordToImageConversion() | |
{ | |
// Setting license file name and path | |
System::String testLicenseFileName = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words license initially | |
SharedPtr<License> wordsLicense = System::MakeObject<License>(); | |
// Applying product license | |
wordsLicense->SetLicense(testLicenseFileName); | |
// Create an instance of Document class of Aspose.Words for C++ | |
// to load the Word file for conveting to images | |
SharedPtr<Document> WordDocumentToImageUsingCPP = MakeObject<Document>(u"WordToImage.docx"); | |
//Set ImageSaveOptions to convert document pages to image | |
SharedPtr<ImageSaveOptions> wordpagestoimage = MakeObject<ImageSaveOptions>(SaveFormat::Png); | |
// Set page ranges to convert all word pages to image | |
SharedPtr<PageRange> pagerange = MakeObject<PageRange>(0, WordDocumentToImageUsingCPP->get_PageCount() - 1); | |
wordpagestoimage->set_PageSet(MakeObject<PageSet>(1)); | |
wordpagestoimage->set_PageSavingCallback(MakeObject<Word_Pages_To_Images>()); | |
// Save Word document to PNG image in C++ using Save method | |
WordDocumentToImageUsingCPP->Save(u"output.png", wordpagestoimage); | |
} | |
}; |
前の例では、C++を使用してHTMLをPDFに変換する方法を見ました。このトピックでは、Word文書をC++の画像にエクスポートする方法を示しました。ご覧のとおり、数行のコードを使用して、C++でドキュメントをPNGにレンダリングするのは簡単です。