この簡単なトピックでは、C++を使用してWordをPDFに変換する方法を学習します。単純なAPI呼び出しを使用して外観やその他のプロパティを変更することで出力PDFをカスタマイズし、ブラウザやその他のビューアで表示できるC++**のDOCXからPDFを生成できます。
C++を使用してWordをPDFに変換する手順
- NuGetパッケージマネージャーからAspose.Words for C++をインストールします
- Aspose.Words名前空間とすべての必要なヘッダーファイルをインクルードします
- Document classを使用して、C++でPDFにエクスポートするためのソースWordファイルをロードします
- PdfSaveOptionsをインスタンス化して、PDFのページ、ページモード、およびコンプライアンスモードを設定します
- Saveメソッドを使用してDOCXファイルをPDFとして保存します
上記の手順では、単純なAPI呼び出しを使用して*WordをC++でPDFにエクスポートできます。ディスクからドキュメントをロードした後、 PdfSaveOptions *クラスを使用して出力PDFをカスタマイズし、エクスポートされたPDFのページ範囲、ページモード、およびPDFコンプライアンスを設定できます。最後に、PDFをディスクまたはMemoryStreamに保存します。
C++でDOCXからPDFを生成するコード
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/Range.h> | |
#include <Aspose.Words.Cpp/Saving/PageSet.h> | |
#include <Aspose.Words.Cpp/Saving/PdfSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <Aspose.Words.Cpp/Saving/PageSet.h> | |
#include <Aspose.Words.Cpp/Saving/PdfPageMode.h> | |
#include <Aspose.Words.Cpp/Saving/PdfCompliance.h> | |
#include <system/enumerator_adapter.h> | |
#include <system/smart_ptr.h> | |
#include <system/shared_ptr.h> | |
#include <system/io/path.h> | |
using namespace Aspose::Words::Saving; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Replacing; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
class WordToPDFEx | |
{ | |
public: | |
static void WordToPDF() | |
{ | |
// Load and Set API License | |
System::String LicFilePath = u"Aspose.Total.Net.lic"; | |
SharedPtr<Aspose::Words::License> WordsCPPLicenseForTable = System::MakeObject<Aspose::Words::License>(); | |
// Setting product license | |
WordsCPPLicenseForTable->SetLicense(LicFilePath); | |
// Open the DOCX file using Document class for saving as PDF | |
SharedPtr<Document> sampleDocx = MakeObject<Document>(u"TestAspose.docx"); | |
// Instantiate the PdfSaveOptions class object before converting the Docx to PDF | |
SharedPtr <PdfSaveOptions> options = MakeObject <PdfSaveOptions>(); | |
System::ArrayPtr <int32_t> pages = System::MakeObject<System::Array<int32_t>>(2); | |
pages[0] = 1; | |
pages[1] = 2; | |
SharedPtr <PageSet> pageSet = MakeObject<PageSet>(pages); | |
// Set the page numbers of the document to be rendered to output PDF | |
options->set_PageSet(pageSet); | |
// Configuring the full screen page mode while opening it in a viewer | |
options->set_PageMode(PdfPageMode::FullScreen); | |
// Set the output PDF document compliance mode | |
options->set_Compliance(PdfCompliance::Pdf17); | |
// Save the DOCX as PDF file using the above mentioned options | |
sampleDocx->Save(u"Output.pdf", options); | |
} | |
}; |
上記の例はC++でWordをPDFに変換します。 PDFへのレンダリングは、Wordファイルをディスクからロードすることから始まります。その後の手順では、PdfSaveOptionsクラスを使用してさまざまなPDFオプションを設定しました。また、テキストコンテンツの圧縮、ズーム動作、ズーム率、アウトラインオプション、アンチエイリアスを使用するオプションなど、他のオプション設定を設定してPDFをカスタマイズすることもできます。最後に、生成されたPDFをディスクに保存しました。
ここでは、C++を使用してWordをPDFに変換する方法を学びました。 WordファイルをMDファイル形式に変換する方法について知りたい場合は、C++を使用してWordをMarkdownに変換する方法の記事を参照してください。