チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»C++»Spire.Doc for C++»リスト»C++:Word 文書にリストを挿入する方法
2023-06-06

C++:Word 文書にリストを挿入する方法

文書の段落を読みやすくしたり、論理的な関係性を持たせたい場合は、文字や数字、項目記号によって並べ替えることができます。この機能により、読者は特定の内容を迅速に見つけることができ、リストの内容を短時間で把握することができます。この記事では、Spire.Doc for C++ を使用して C++ で Word 文書に次のリストを挿入する方法について説明します。

Spire.Doc for C++ をインストールします

Spire.Doc for C++ をアプリケーションに組み込むには、2つの方法があります。一つは NuGet 経由でインストールする方法、もう一つは当社のウェブサイトからパッケージをダウンロードし、ライブラリをプログラムにコピーする方法です。NuGet 経由のインストールの方が便利で、より推奨されます。詳しくは、以下のリンクからご覧いただけます。

Spire.Doc for C++ を C++ アプリケーションに統合する方法

C++ で Word に番号付きリストを挿入する

Spire.Doc for C++ には、番号付きリストや箇条書きリストを作成するための ListStyle クラスが用意されています。次に、Paragraph->GetListFormat()->ApplyStyle() メソッドを使用して、リストスタイルを段落に適用できます。番号付きリストを作成する手順は次のとおりです。

  • Document オブジェクトを作成します。
  • Document->AddSection() メソッドを使用してセクションを追加します。
  • ListStyle クラスのインスタンスを作成し、リストのタイプを「Numbered」に指定します。
  • ListStyle->GetLevels()->GetItem(index) メソッドを使用して、リストの特定のレベルを取得します。
  • ListLevel->SetPatternType() メソッドを使用して番号タイプを設定します。
  • Document->GetListStyles()->Add() メソッドを使用して、リストのスタイルをドキュメントに追加します。
  • Section->AddParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。
  • Paragraph->GetListFormat()->ApplyStyle() メソッドを使用して、特定の段落にリストのスタイルを適用します。
  • Paragraph->GetListFormat()->GetListLevelNumber() メソッドを使用してリストのレベルを指定します。
  • Document->SaveToFile() メソッドを使用して、結果文書を保存します。
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Documentオブジェクトを作成する
    intrusive_ptr<Document> document = new Document();

    //セクションを追加する
    intrusive_ptr<Section> section = document->AddSection();

    //番号付きリストのスタイルを作成する
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
    listStyle->SetName(L"numberedList");
    listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedParen);
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    document->GetListStyles()->Add(listStyle);

    //段落を追加する
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Web開発に必要なスキル:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //段落を追加して番号付きリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"HTML");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //4つの段落を追加し、番号付きリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"CSS");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C++Script");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Python");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"MySQL");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //結果文書を保存する
document->SaveToFile(L"output/NumberedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++:Word 文書にリストを挿入する方法

C++ で Word に箇条書きリストを挿入する

箇条書きの作成は、番号付きリストを作成するプロセスと似ています。しかし、リストのスタイルを作成するときに、 [Bulleted] として指定してください。また、箇条書き記号も設定してください。次は詳細な手順です。

  • Document オブジェクトを作成します。
  • Document->AddSection() メソッドを使用してセクションを追加します。
  • ListStyle クラスのインスタンスを作成し、リストの種類を「Bulleted」に指定します。
  • ListStyle->GetLevels()->Get(index) メソッドを使用してリストの特定のレベルを取得します。
  • ListLevel->SetBulletCharacter() メソッドを使用して箇条書き符号を設定します。
  • Document->GetListStyles()->Add() メソッドを使用して、リストのスタイルをドキュメントに追加します。
  • Section->AddParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。
  • Paragraph->GetListFormat()->ApplyStyle() メソッドを使用して、特定の段落にリストのスタイルを適用します。
  • Paragraph->GetListFormat()->SetListLevelNumber() メソッドを使用してリストレベルを指定します。
  • Document->SaveToFile() メソッドを使用して、結果文書を保存します。
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Documentオブジェクトを作成する
    intrusive_ptr<Document> document = new Document();

    //セクションを追加する
    intrusive_ptr<Section> section = document->AddSection();

    //箇条書きリストのスタイルを作成する
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Bulleted);
    listStyle->SetName(L"bulletedList");
    listStyle->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7");
    listStyle->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol");
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    document->GetListStyles()->Add(listStyle);

    //段落を追加する
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Web開発に必要なスキル");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //段落を追加して箇条書きリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"HTML");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //5つの段落を追加し、箇条書きリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"CSS");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C++Script");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Python");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"MySQL");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"PHP");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //結果文書を保存する
document->SaveToFile(L"output/BulletedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++:Word 文書にリストを挿入する方法

C++ で Word に複数レベル番号付きリストを挿入する

多レベルリストは2つ以上のレベルで構成されています。各レベルは、ListStyle->GetLevels()->GetItem(index) メソッドを使用してアクセスできます。ListLevel オブジェクトを介して、特定のレベルの番号の種類やプレフィックスを設定することができます。次は詳細な手順です。

  • Document オブジェクトを作成します。
  • Document->AddSection() メソッドを使用してセクションを追加します。
  • ListStyle クラスのインスタンスを作成し、リストの種類を「Numbered」 に指定します。
  • ListStyle->GetLevels()->GetItem(index) メソッドを使用してリストの特定のレベルを取得し、番号の種類やプレフィックスを設定します。
  • Document->GetListStyles()->Add() メソッドを使用して、リストスタイルをドキュメントに追加します。
  • Section->AddParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。
  • Paragraph->GetListFormat()->ApplyStyle() メソッドを使用して、特定の段落にリストスタイルを適用します。
  • Paragraph->GetListFormat()->SetListLevelNumber() メソッドを使用してリストレベルを指定します。
  • Document->SaveToFile() メソッドを使用して、結果文書を保存します。
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Documentオブジェクトを作成する
    intrusive_ptr<Document> document = new Document();

    //セクションを追加する
    intrusive_ptr<Section> section = document->AddSection();

    //番号付きリストスタイルを作成し、各レベルのプレフィックスとパターンタイプを指定する
    intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered);
    listStyle->SetName(L"nestedStyle");
    listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
    listStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    listStyle->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1.");
    listStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic);
    listStyle->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2.");
    listStyle->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic);
    document->GetListStyles()->Add(listStyle);

    //段落を追加する
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"複数レベル番号付きリスト:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //段落を追加して番号付けリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //5つの段を追加し、番号付けリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->ContinueListNumbering();
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"A sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The third item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //結果文書を保存する
    document->SaveToFile(L"output/MultilevelNumberedList.docx", FileFormat::Docx2019);
    document->Dispose();
}

C++:Word 文書にリストを挿入する方法

C++ で Word に複数レベル混合型リストを挿入する

多レベルリストで数字と記号を同時に使用したい場合があります。この機能を実現するには、番号付きリストスタイルと箇条書きリストスタイルを作成し、それらを異なる段落に適用するだけです。詳細な手順は次のとおりです。

  • Document オブジェクトを作成します。
  • Document->AddSection() メソッドを使用してセクションを追加します。
  • 番号付きリスト スタイルと箇条書きリスト スタイルを作成します。
  • Section->AddParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。
  • Paragraph->GetListFormat()->ApplyStyle() メソッドを使用して、異なる段落に異なるリストスタイルを適用します。
  • Document->SaveToFile() メソッドを使用して、結果文書を保存します。
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Documentオブジェクトを作成する
    intrusive_ptr<Document> document = new Document();

    //セクションを追加する
    intrusive_ptr<Section> section = document->AddSection();

    //番号付きリストスタイルを作成する
    intrusive_ptr<ListStyle> numberedListStyle = new ListStyle(document, ListType::Numbered);
    numberedListStyle->SetName(L"numberedStyle");
    numberedListStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic);
    numberedListStyle->GetLevels()->GetItem(0)->SetTextPosition(20);
    numberedListStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter);
    document->GetListStyles()->Add(numberedListStyle);

    //箇条書きリストスタイルを作成する
    intrusive_ptr<ListStyle> bulletedListStyle = new ListStyle(document, ListType::Bulleted);
    bulletedListStyle->SetName(L"bulletedStyle");
    bulletedListStyle->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A");
    bulletedListStyle->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol");
    document->GetListStyles()->Add(bulletedListStyle);

    //段落を追加する
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"多レベル混合型リスト:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //段落を追加して番号付きリストのスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //5つの段落を追加し、異なるリストスタイルを適用する
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->SetListLevelNumber(1);
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //結果文書を保存する
document->SaveToFile(L"output/MultilevelMixedList.docx", FileFormat::Docx);
document->Dispose();
}

C++:Word 文書にリストを挿入する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 297 times