ワープロ ドキュメントからスタイルを抽出する
このトピックでは、Open XML SDK for Office のクラスを使用して、ワープロ ドキュメントから XDocument インスタンスにスタイルまたはスタイルWithEffects パーツをプログラムで抽出する方法について説明します。 このタスクを示すメソッド ExtractStylesPart
例が含まれています。
ExtractStylesPart メソッド
ExtractStylesPart
サンプル メソッドを使用して、Microsoft Word ドキュメントのスタイルまたはスタイルWithEffects パーツを含むXDocument
インスタンスを取得できます。 Word 2010 で作成されたドキュメントでは、スタイル パーツは 1 つだけであることに注意してください。Word 2013 以降では、2 つ目のスタイルWithEffects パーツが追加されます。 Word 2013+ から Word 2010 までドキュメントを "ラウンドトリップ" するために、Word 2013+ は元のスタイル パーツと新しいスタイル パーツの両方を保持します。 (Office Open XML ファイル形式の仕様では、Microsoft Wordが認識しない部分を無視する必要があります。Word 2010 では、2013 以降でドキュメントに追加される stylesWithEffects 部分Word気付きません)。ユーザー (およびアプリケーション) は、スタイルまたはスタイルWithEffects パーツを取得した結果を解釈する必要があります。
ExtractStylesPart
プロシージャでは、2 つのパラメーターを受け取ります。最初のパラメーターには、スタイルを抽出するファイルのパスを示す文字列が含まれており、2 番目のパラメーターはスタイル パーツを取得するか、新しい stylesWithEffects パーツを取得するかを示します (基本的には、2013 以降のドキュメント Wordでこのプロシージャを 2 回呼び出し、各パーツを取得する必要があります)。 プロシージャは、要求したスタイルまたはスタイルWithEffects パーツ全体を含む XDocument
インスタンスを返します。ドキュメントのすべてのスタイル情報 (要求したパーツが存在しない場合は null 参照)。
static XDocument? ExtractStylesPart(string fileName, string getStylesWithEffectsPart = "true")
このメソッドの完全なコードについては、「サンプル コード」セクションを参照してください。
サンプル メソッドの呼び出し
サンプル メソッドを呼び出すには、スタイルを抽出するドキュメントのファイル名を含む最初のパラメーターの文字列と、取得するパーツの型が styleWithEffects パーツ (true
)、スタイル パーツ (false
) かどうかを指定する 2 番目のパラメーターのブール型 (Boolean) を渡します。 次のサンプル コードは例を示しています。
XDocument
インスタンスがある場合は、必要な操作を実行できます。次のサンプル コードでは、XDocument
インスタンスの内容がコンソールに表示されます。
if (args is [{ } fileName, { } getStyleWithEffectsPart])
{
var styles = ExtractStylesPart(fileName, getStyleWithEffectsPart);
if (styles is not null)
{
Console.WriteLine(styles.ToString());
}
}
else if (args is [{ } fileName2])
{
var styles = ExtractStylesPart(fileName2);
if (styles is not null)
{
Console.WriteLine(styles.ToString());
}
}
コードの動作のしくみ
コードは、メソッドの戻り値を格納する styles
という名前の変数を作成することから始めます。
コードは引き続き、 Open メソッドを使用してドキュメントを開き、ドキュメントを読み取り専用アクセス用に開く必要があることを示します (最後の false パラメーター)。 開いているドキュメントを指定すると、コードは MainDocumentPart プロパティを使用してメインドキュメント パーツに移動し、スタイル パーツへの参照を保持するために stylesPart
という名前の変数を準備します。
// Declare a variable to hold the XDocument.
XDocument? styles = null;
// Open the document for read access and get a reference.
using (var document = WordprocessingDocument.Open(fileName, false))
{
if (
document.MainDocumentPart is null ||
(document.MainDocumentPart.StyleDefinitionsPart is null && document.MainDocumentPart.StylesWithEffectsPart is null)
)
{
throw new ArgumentNullException("MainDocumentPart and/or one or both of the Styles parts is null.");
}
// Get a reference to the main document part.
var docPart = document.MainDocumentPart;
// Assign a reference to the appropriate part to the
// stylesPart variable.
StylesPart? stylesPart = null;
適切なスタイル パーツを検索する
次のコードでは、 getStylesWithEffectsPart
Boolean パラメーターを使用して、要求されたスタイル パーツへの参照を取得します。
この値を基にして、コードは docPart
変数の特定のプロパティを取得し、 stylesPart
変数に格納します。
if (getStylesWithEffectsPart.ToLower() == "true")
{
stylesPart = docPart.StylesWithEffectsPart;
}
else
{
stylesPart = docPart.StyleDefinitionsPart;
}
パーツのコンテンツを取得する
要求されたスタイル パーツが存在する場合、コードは、 XDocument
インスタンス内のパーツの内容を返す必要があります。 各パーツには、Streamを返すGetStream() メソッドが用意されています。
コードは、Stream インスタンスを Create メソッドに渡し、Load メソッドを呼び出して、XmlNodeReader
をパラメーターとして渡します。
if (stylesPart is not null)
{
using var reader = XmlNodeReader.Create(stylesPart.GetStream(FileMode.Open, FileAccess.Read));
// Create the XDocument.
styles = XDocument.Load(reader);
}
サンプル コード
以下に、C# と Visual Basic の完全な ExtractStylesPart コード サンプルを示します。
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
// Extract the styles or stylesWithEffects part from a
// word processing document as an XDocument instance.
static XDocument? ExtractStylesPart(string fileName, string getStylesWithEffectsPart = "true")
{
// Declare a variable to hold the XDocument.
XDocument? styles = null;
// Open the document for read access and get a reference.
using (var document = WordprocessingDocument.Open(fileName, false))
{
if (
document.MainDocumentPart is null ||
(document.MainDocumentPart.StyleDefinitionsPart is null && document.MainDocumentPart.StylesWithEffectsPart is null)
)
{
throw new ArgumentNullException("MainDocumentPart and/or one or both of the Styles parts is null.");
}
// Get a reference to the main document part.
var docPart = document.MainDocumentPart;
// Assign a reference to the appropriate part to the
// stylesPart variable.
StylesPart? stylesPart = null;
if (getStylesWithEffectsPart.ToLower() == "true")
{
stylesPart = docPart.StylesWithEffectsPart;
}
else
{
stylesPart = docPart.StyleDefinitionsPart;
}
if (stylesPart is not null)
{
using var reader = XmlNodeReader.Create(stylesPart.GetStream(FileMode.Open, FileAccess.Read));
// Create the XDocument.
styles = XDocument.Load(reader);
}
}
// Return the XDocument instance.
return styles;
}