C++でPDFまたはWord文書を作成する

忠実度の高いC++ライブラリを使用して、プログラムでほぼすべての形式で新しいドキュメントを作成します

プログラミングAPIを使用すると、C++開発者は、わずか数行のコードでPDF、DOC、DOCX、HTML、EPUB、およびその他の多くの形式のドキュメントを簡単に作成できます。

コード スニペットを表示

C++ を使用してドキュメントを作成する

与えられた強力なAPIを使用すると、C++ 開発者はほぼすべての形式でドキュメントを作成できます。これを行うには、C++ ライブラリを使用していくつかの手順を実行する必要があります。

  1. ファイル名を追加
  2. C++ を使用してドキュメントの作成を開始します
  3. 作成したドキュメントを選択した形式で保存します

空白のドキュメントには技術的に1つの段落が含まれることになっているため、プログラムでドキュメントを作成すると、まさにその基本的なドキュメント構造が得られることに注意してください。

新しく作成したドキュメントにコンテンツをすぐに追加できることに注意してください。したがって、空のドキュメントだけでなく、必要なコンテンツを含むドキュメントも取得できます。ドキュメントを編集する方法の詳細については、編集ページを参照してください。

プログラムで C++ でドキュメントを作成する

特定の C++ ライブラリを使用すると、サポートされている任意の形式(PDF、DOCX、DOC、RTF、ODT、EPUB、HTMLなど)でドキュメントをプログラムで作成できます。

強力な機能を試して、次の例を使用していくつかの形式でドキュメントを作成する方法を確認してください。

C++を使用して新しいドキュメントを作成します
リストからターゲットフォーマットを選択します
コードを実行する
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto font = builder->get_Font();
font->set_Name(u"Courier New");
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Size(36);
font->set_HighlightColor(System::Drawing::Color::get_Yellow());

builder->Write(u"Morbi enim nunc faucibus a.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();

auto run = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed.");
auto para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(run);

auto comment = MakeObject<Comment>(doc, u"John Doe", u"JD", System::DateTime::get_Today());
comment->SetText(u"Quisque fringilla leo.");

auto commentRangeStart = MakeObject<CommentRangeStart>(doc, comment->get_Id());
auto commentRangeEnd = MakeObject<CommentRangeEnd>(doc, comment->get_Id());

run->get_ParentNode()->InsertBefore(commentRangeStart, run);
run->get_ParentNode()->InsertAfter(commentRangeEnd, run);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);

comment->AddReply(u"Jane Doe", u"JD", System::DateTime::get_Now(),
    u"Pellentesque vel sapien justo.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);

builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。
if (secondRun->get_NextSibling() != NULL)
    builder->MoveTo(secondRun->get_NextSibling());
else
    builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto shape = builder->InsertChart(ChartType::Pie, 432, 252);
auto chart = shape->get_Chart();
chart->get_Title()->set_Text(u"Demo Chart");

chart->get_Series()->Clear();
chart->get_Series()->Add(u"Series 1",
    MakeArray<String>({ u"Category1", u"Category2", u"Category3" }),
    MakeArray<double>({ 2.7, 3.2, 0.8 }));

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // NextSibling が null の場合、これが段落内の最後の Run である可能性が高くなります。 if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
コードを実行する

C++でドキュメントを作成する方法

  1. Aspose.Words for C++インストールします
  2. C++プロジェクトにライブラリ参照を追加(ライブラリをインポート)
  3. 新しいドキュメントを作成する
  4. ファイル名を渡して Save() メソッドを呼び出します
  5. 結果を別のファイルとして取得する

C++ドキュメントを作成するためのライブラリ

Aspose.Words for C++ を開発者環境にインストールするための3つのオプションがあります。ニーズに合ったものを選択し、ステップバイステップの手順に従ってください。

システム要求

このC++ライブラリを使用して、 Microsoft Windows 、Linux、およびmacOSオペレーティングシステムでソフトウェアを開発できます。

  • Linuxには GCC >= 6.3.0 および Clang >= 3.9.1
  • Xcode >= 12.5.1 、macOSには Clang と libc++

LinuxまたはmacOS用のソフトウェアを開発する場合は、製品ドキュメント fontconfig および mesa-glu オープンソースパッケージ)に関する情報を確認してください。

最も人気のあるファイル形式

5%

Aspose製品アップデートを購読する

メールボックスに直接配信される月刊ニュースレターとオファーを入手してください。

© Aspose Pty Ltd 2001-2024. 全著作権所有。