ワープロ ドキュメントを開いてテキストを追加する
このトピックでは、Open XML SDK for Office のクラスを使用してプログラムで開き、Word処理ドキュメントにテキストを追加する方法について説明します。
ドキュメントを開いてテキストを追加する方法
Open XML SDK は、WordprocessingML
要素に対応する厳密に型指定されたクラスを使用して、Word処理ドキュメント構造とコンテンツを作成するのに役立ちます。 このトピックでは、Open XML SDK のクラスを使用して、Word処理ドキュメントを開き、それにテキストを追加する方法について説明します。 さらに、このトピックでは、 WordprocessingML
ドキュメントの基本的なドキュメント構造、関連する XML 要素、および対応する Open XML SDK クラスについて説明します。
WordprocessingDocument オブジェクトの作成
Open XML SDK では、WordprocessingDocument クラスはWord ドキュメント パッケージを表します。 Word ドキュメントを開いて操作するには、ドキュメントから WordprocessingDocument クラスのインスタンスを作成します。 ドキュメントからインスタンスを作成したら、そのドキュメントのテキストを含むメイン ドキュメント パーツへのアクセスを取得できます。 メイン ドキュメント パーツ内のテキストは、WordprocessingML
マークアップを使用してパッケージ内で XML として表されます。
ドキュメントからクラス インスタンスを作成するには、 Open
メソッドのいずれかを呼び出します。 このメソッドにはいくつかの種類があり、シグネチャがそれぞれ異なります。 このトピックのサンプル コードでは、 Open(String, Boolean) メソッドと、2 つのパラメーターを必要とするシグネチャを使用します。 最初のパラメーターは、開くドキュメントを表す完全なパスの文字列を取ります。 2 番目のパラメーターは、 true
または false
であり、編集のためにファイルを開くかどうかを表します。 このパラメーターが false
されている場合、ドキュメントに対して行った変更は保存されません。
次のコード例では、 Open
メソッドを呼び出します。
// Open a WordprocessingDocument for editing using the filepath.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
if (wordprocessingDocument is null)
{
throw new ArgumentNullException(nameof(wordprocessingDocument));
}
Word ドキュメント パッケージを開いたら、テキストをメイン ドキュメント パーツに追加できます。 メインドキュメント パーツの本文にアクセスするには、次のコード例に示すように、不足している要素を作成し、ドキュメント本文への参照を割り当てます。
// Assign a reference to the existing document body.
MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart();
mainDocumentPart.Document ??= new Document();
mainDocumentPart.Document.Body ??= mainDocumentPart.Document.AppendChild(new Body());
Body body = wordprocessingDocument.MainDocumentPart!.Document!.Body!;
WordProcessingML ドキュメントの構造
WordProcessingML
ドキュメントの基本的なドキュメント構造は、document
要素とbody
要素で構成され、その後に段落を表す p
などの 1 つ以上のブロック レベル要素が続きます。 段落には、1 つ以上の r
要素が含まれています。
r
は、書式設定などのプロパティの共通セットを持つテキストの領域である run を表します。 実行には、1 つ以上の t
要素が含まれています。
t
要素には、テキストの範囲が含まれています。 次のコード例は、"テキストの例" というテキストを含むドキュメントの WordprocessingML
マークアップを示しています。
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:t>Example text.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
Open XML SDK を使用すると、 WordprocessingML
要素に対応する厳密に型指定されたクラスを使用して、ドキュメント構造とコンテンツを作成できます。 これらのクラスは、 名前空間にあります。 次の表に、 document
、 body
、 p
、 r
、 t
の各要素に対応するクラスのクラス名を示します。
WordprocessingML 要素 | Open XML SDK クラス | 説明 |
---|---|---|
<document/> |
Document | メイン ドキュメント パーツのルート要素。 |
<body/> |
Body | ISO/IEC 29500 仕様で指定されている、段落、表、注釈などのブロック レベル構造のコンテナー |
<p/> |
Paragraph | 段落 |
<r/> |
Run | セクション |
<t/> |
Text | さまざまなテキスト |
WordprocessingML ドキュメントのパーツと要素の全体的な構造の詳細については、「 WordprocessingML ドキュメントの構造」を参照してください。
WordprocessingML マークアップを生成してテキストを追加する
メイン ドキュメント パーツの本文にアクセスできる場合は、Paragraph、Run、および Text クラスのインスタンスを追加してテキストを追加します。 これによって、必要な WordprocessingML マークアップが生成されます。 次のコード例では、段落を追加します。
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
サンプル コード
ここで示OpenAndAddTextToWordDocument
メソッドの例は、Word ドキュメントを開き、Open XML SDK を使用してテキストを追加するために使用できます。 このメソッドを呼び出すには、最初のパラメーターとして完全なパスのファイル名を渡し、2 つ目のパラメーターとして追加するテキストを渡します。
string file = args[0];
string txt = args[1];
OpenAndAddTextToWordDocument(args[0], args[1]);
以下は、C# および Visual Basic の完全なサンプル コードです。
OpenAndAddTextToWordDocument
メソッドには、Save
への明示的な呼び出しが含まれていないことに注意してください。 これは、自動保存機能が既定でオンになっており、OpenSettings
を使用してOpen
メソッドの呼び出しで無効になっていないためです。
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
static void OpenAndAddTextToWordDocument(string filepath, string txt)
{
// Open a WordprocessingDocument for editing using the filepath.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
if (wordprocessingDocument is null)
{
throw new ArgumentNullException(nameof(wordprocessingDocument));
}
// Assign a reference to the existing document body.
MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart();
mainDocumentPart.Document ??= new Document();
mainDocumentPart.Document.Body ??= mainDocumentPart.Document.AppendChild(new Body());
Body body = wordprocessingDocument.MainDocumentPart!.Document!.Body!;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
}
}