次の方法で共有


Open XML パッケージ内のドキュメント パーツのコンテンツを別のパッケージ内のドキュメント パーツにコピーする

適用対象: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

この記事の内容
パッケージとドキュメント パーツ
WordprocessingDocument オブジェクトの取得
WordProcessingML ドキュメントの構造
テーマ パーツ
サンプル コードの動作のしくみ
サンプル コード

このトピックでは、Open XML SDK 2.0 for Microsoft Office のクラスを使用して、プログラムによって、Open XML ワープロ ドキュメント パーツのコンテンツを別のワープロ ドキュメント パーツにコピーする方法について説明します。

このトピックのコードをコンパイルするには、次のアセンブリ ディレクティブが必要です。

using System.IO;
using DocumentFormat.OpenXml.Packaging;
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging

パッケージとドキュメント パーツ

Open XML ドキュメントはパッケージとして保存されます。このパッケージの形式は、ISO/IEC 29500-2 (英語) に定義されています。パッケージの内部は、リレーションシップで結ばれた複数のパーツに分けることができます。パーツ間のリレーションシップによって、ドキュメントのカテゴリが決まります。パッケージのリレーションシップ アイテムにメイン ドキュメント パーツへのリレーションシップを含めると、そのドキュメントをワープロ ドキュメントとして定義できます。パッケージのリレーションシップ アイテムにプレゼンテーション パーツへのリレーションシップを含めると、そのドキュメントをプレゼンテーション ドキュメントとして定義できます。パッケージのリレーションシップ アイテムにブック パーツへのリレーションシップを含めると、そのドキュメントをスプレッドシート ドキュメントとして定義できます。このトピックでは、ワープロ ドキュメント パッケージを使用します。

WordprocessingDocument オブジェクトの取得

既存のドキュメントを開くには、次の例に示すように、2 つの using ステートメントを使用して、WordprocessingDocument クラスのインスタンスを作成します。また、同じステートメントで Open メソッドとブール値パラメーターを使用して、指定したファイル名を持つワープロ ファイルを開きます。コピー元ファイルは、ブール値パラメーターを false に設定して、読み取り専用で開きます。コピー先ファイルは、ブール値パラメーターを true に設定して、ドキュメントの編集を有効にします。

using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false))
using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
{
    // Insert other code here.
}
Dim wordDoc1 As WordprocessingDocument = WordprocessingDocument.Open(fromDocument1, False)
Dim wordDoc2 As WordprocessingDocument = WordprocessingDocument.Open(toDocument2, True)
Using (wordDoc2)
    ' Insert other code here.
End Using

using ステートメントは、一般的な .Open, .Save, .Close シーケンスに代わる手段として推奨されます。このステートメントでは、閉じかっこに達すると、Dispose メソッド (リソースをクリーンアップする Open XML SDK の内部メソッド) が自動的に呼び出されます。using ステートメントに続くブロックは、using ステートメントで作成または指定されたオブジェクトのスコープを設定します。Open XML SDK の WordprocessingDocument クラスは、System.IDisposable 実装の一部として自動的にオブジェクトを保存して閉じます。また、Dispose は、ブロックが終了すると自動的に呼び出されます。したがって、using を使用する場合は、Save と Close を明示的に呼び出す必要はありません。

WordProcessingML ドキュメントの構造

WordProcessingML ドキュメントの基本ドキュメント構造は、document 要素と body 要素、およびそれに続く 1 つ以上のブロック レベルの要素 (段落を表す p など) で構成されます。段落には 1 つ以上の r 要素が含まれます。r はセクションを表し、書式などの共通のプロパティ セットがあるテキストの領域です。セクションには 1 つ以上の t 要素が含まれます。t 要素は、さまざまなテキストを含みます。次のコード例は、テキスト "Example text." を含むドキュメントの WordprocessingML マークアップを示します。

<w:document xmlns:w="https://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 2.0 を使用すると、WordprocessingML 要素に対応した、厳密に型指定されたクラスを使用して、ドキュメント構造と内容を作成できます。これらのクラスは DocumentFormat.OpenXml.Wordprocessing 名前空間にあります。以下の表に、document、body、p、r、および t の各要素に対応するクラスのクラス名を示します。

WordprocessingML 要素

Open XML SDK 2.0 のクラス

説明

document

Document

メイン ドキュメント パーツのルート要素。

body

Body

段落、表、注釈など、ブロック レベル構造のコンテナーは、ISO/IEC 29500 (英語) 仕様で指定されています。

p

Paragraph

段落。

r

Run

セクション。

t

Text

一連のテキスト。

テーマ パーツ

テーマ パーツには、ドキュメントの色、フォント、および書式設定についての情報が含まれます。テーマ パーツは、ISO/IEC 29500 (英語) 仕様書に、次のように定義されています。

このパーツの種類のインスタンスには、ドキュメントのテーマに関する情報が含まれます。ドキュメントのテーマは、配色、フォント パターン、および書式設定スキームの組み合わせです (書式設定スキームは文字飾りとも呼ばれます)。WordprocessingML ドキュメントでは、選択するテーマによって、特に見出しの色とスタイルが変化します。SpreadsheetML ドキュメントでは、選択するテーマによって、特にセルの内容およびグラフの色とスタイルが変化します。PresentationML ドキュメントでは、選択するテーマによって、特にスライド、配付資料、およびノートの書式設定が、関連付けられたマスターを介して変化します。

WordprocessingML または SpreadsheetML パッケージには、ゼロまたは 1 つのテーマ パーツが含まれます。このパーツは、メイン ドキュメント (§11.3.10) またはブック (§12.3.23) パーツ内の暗黙的リレーションシップのターゲットになります。PresentationML パッケージには、暗黙的リレーションシップを介して、配付資料マスター (§13.3.3)、ノート マスター (§13.3.4)、スライド マスター (§13.3.10)、またはプレゼンテーション (§13.3.6) パーツごとにゼロまたは 1 つのテーマ パーツが含まれます。

[例: 次の WordprocessingML メイン ドキュメント パーツのリレーションシップ項目には、ZIP 項目 theme/theme1.xml に格納されているテーマ パーツとのリレーションシップが含まれています。

<Relationships xmlns="…">
   <Relationship Id="rId4"
      Type="http://…/theme" Target="theme/theme1.xml"/>
</Relationships>

例終わり]

c ISO/IEC29500: 2008.

サンプル コードの動作のしくみ

Open XML パッケージのドキュメント パーツのコンテンツを別のパッケージのドキュメント パーツにコピーするために、各ワープロ ドキュメントの完全パスをパラメーターとして CopyThemeContent メソッドに渡します。次に、両方のドキュメントを WordprocessingDocument オブジェクトとして開いて、ThemePart パーツを参照する変数を各パッケージに作成します。

public static void CopyThemeContent(string fromDocument1, string toDocument2)
{
   using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false))
   using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
   {
      ThemePart themePart1 = wordDoc1.MainDocumentPart.ThemePart;
      ThemePart themePart2 = wordDoc2.MainDocumentPart.ThemePart;
Public Sub CopyThemeContent(ByVal fromDocument1 As String, ByVal toDocument2 As String)
   Dim wordDoc1 As WordprocessingDocument = WordprocessingDocument.Open(fromDocument1, False)
   Dim wordDoc2 As WordprocessingDocument = WordprocessingDocument.Open(toDocument2, True)
   Using (wordDoc2)
      Dim themePart1 As ThemePart = wordDoc1.MainDocumentPart.ThemePart
      Dim themePart2 As ThemePart = wordDoc2.MainDocumentPart.ThemePart

次に、StreamReader オブジェクトを使用して、コピー元の ThemePart パーツのコンテンツを読み取り、StreamWriter オブジェクトを使用して、コピー先の ThemePart パーツに書き込みます。

using (StreamReader streamReader = new StreamReader(themePart1.GetStream()))
using (StreamWriter streamWriter = new StreamWriter(themePart2.GetStream(FileMode.Create))) 
{
    streamWriter.Write( streamReader.ReadToEnd());
}
Dim streamReader As StreamReader = New StreamReader(themePart1.GetStream())
Dim streamWriter As StreamWriter = New StreamWriter(themePart2.GetStream(FileMode.Create))
Using (streamWriter)
    streamWriter.Write(streamReader.ReadToEnd)
End Using

サンプル コード

以下のコードでは、Open XML パッケージ内のドキュメント パーツのコンテンツを別のパッケージ内のドキュメント パーツにコピーします。CopyThemeContent メソッドを呼び出す方法は、次の "MyPkg4.docx" から "MyPkg3.docx" にテーマ パーツをコピーする例を参考にしてください。

string fromDocument1 = @"C:\Users\Public\Documents\MyPkg4.docx";
string toDocument2 = @"C:\Users\Public\Documents\MyPkg3.docx";
CopyThemeContent(fromDocument1, toDocument2);
Dim fromDocument1 As String = "C:\Users\Public\Documents\MyPkg4.docx"
Dim toDocument2 As String = "C:\Users\Public\Documents\MyPkg3.docx"
CopyThemeContent(fromDocument1, toDocument2)

重要

プログラムを実行する前に、コピー元ドキュメント (MyPkg4.docx) にテーマが設定されていることを確認します。テーマが設定されていないと、例外がスローされます。ドキュメントにテーマを追加するには、ドキュメントを Word 2010 で開き、[ページ ​​レイアウト] タブをクリックし、[テーマ] をクリックして、使用可能なテーマのいずれかを選択します。

プログラムの実行後は、"MyPkg3.docx" を調べて、"MyPkg4.docx" からコピーされたテーマを確認できます。

以下は、C# および Visual Basic の完全なサンプル コードです。

// To copy contents of one package part.
public static void CopyThemeContent(string fromDocument1, string toDocument2)
{
   using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false))
   using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
   {
      ThemePart themePart1 = wordDoc1.MainDocumentPart.ThemePart;
      ThemePart themePart2 = wordDoc2.MainDocumentPart.ThemePart;

       using (StreamReader streamReader = new StreamReader(themePart1.GetStream()))
       using (StreamWriter streamWriter = new StreamWriter(themePart2.GetStream(FileMode.Create))) 
      {
         streamWriter.Write( streamReader.ReadToEnd() );
      }
   }
}
' To copy contents of one package part.
Public Sub CopyThemeContent(ByVal fromDocument1 As String, ByVal toDocument2 As String)
   Dim wordDoc1 As WordprocessingDocument = WordprocessingDocument.Open(fromDocument1, False)
   Dim wordDoc2 As WordprocessingDocument = WordprocessingDocument.Open(toDocument2, True)
   Using (wordDoc2)
      Dim themePart1 As ThemePart = wordDoc1.MainDocumentPart.ThemePart
      Dim themePart2 As ThemePart = wordDoc2.MainDocumentPart.ThemePart
      Dim streamReader As StreamReader = New StreamReader(themePart1.GetStream())
      Dim streamWriter As StreamWriter = New StreamWriter(themePart2.GetStream(FileMode.Create))
      Using (streamWriter)
         streamWriter.Write(streamReader.ReadToEnd)
      End Using
   End Using
End Sub

関連項目

参照

Class Library Reference