[方法] Open XML API を使用して、リレーションシップ ID を受け取る新しいドキュメント パーツを Office Open XML パッケージに追加する
Office Open XML パッケージの仕様は、コンテンツを含み、単一のパッケージに格納されているすべてのドキュメント パーツのリレーションシップを定義する XML ファイルのセットを定義しています。これらのパッケージは、Microsoft® Office Excel® 2007、Microsoft Office PowerPoint® 2007、および Microsoft Office Word 2007 のドキュメント ファイルを構成するパーツを結合します。Open XML アプリケーション プログラミング インターフェイス (API) を使用して、パッケージを作成したり、パッケージを構成するファイルを操作したりできます。このトピックでは、Word 2007 で Office Open XML パッケージのリレーションシップ Id パラメータを受け取るドキュメント パーツ (ファイル) を追加するためのコードと手順について説明していますが、手順は Office Open XML 形式をサポートする 3 つのどの 2007 Microsoft Office system プログラムでも同じです。
注意
これらのコード サンプルは、画面上で見やすくするために編集されています。このコードを使用する場合は、インデントを変更し、余分な行を削除してください。
' How to add a new document part that receives a relationship ID to a package
Public Sub AddNewPart(ByVal newFilePath As String)
' Create a new Word document.
Dim wordDoc As WordprocessingDocument =
WordprocessingDocument.Create(newFilePath,
WordprocessingDocumentType.Document)
' Add the MainDocumentPart part in new Word document.
wordDoc.AddNewPart(Of MainDocumentPart)
("application/vnd.openxmlformats-
officedocument.wordprocessingml.document.main+xml", "rId1")
' Add the CustomFilePropertiesPart part in new Word document.
wordDoc.AddNewPart(Of CustomFilePropertiesPart)("rId2")
' Add the CoreFilePropertiesPart part in the new Word document.
wordDoc.AddNewPart(Of CoreFilePropertiesPart)("rId3")
' Add the DigitalSignatureOriginPart part in the new Word document.
wordDoc.AddNewPart(Of DigitalSignatureOriginPart)("rId4")
' Add the ExtendedFilePropertiesPart part in the new Word document.
wordDoc.AddNewPart(Of ExtendedFilePropertiesPart)("rId5")
' Add the ThumbnailPart part in the new Word document.
wordDoc.AddNewPart(Of ThumbnailPart)("image/jpeg", "rId6")
wordDoc.Close()
End Sub
// How to add a new document part that receives a relationship ID to a package.
public void AddNewPart(string newFilePath)
{
// Create a new Word document.
WordprocessingDocument wordDoc =
WordprocessingDocument.Create(newFilePath,
WordprocessingDocumentType.Document);
// Add the MainDocumentPart part in the new Word document.
wordDoc.AddNewPart<MainDocumentPart>("application/vnd.openxmlformats-
officedocument.wordprocessingml.document.main+xml ", "rId1");
// Add the CustomFilePropertiesPart part in the new Word document.
wordDoc.AddNewPart<CustomFilePropertiesPart>("rId2");
// Add the CoreFilePropertiesPart part in the new Word document.
wordDoc.AddNewPart<CoreFilePropertiesPart>("rId3");
// Add the DigitalSignatureOriginPart part in the new Word document.
wordDoc.AddNewPart<DigitalSignatureOriginPart>("rId4");
// Add the ExtendedFilePropertiesPart part in the new Word document.
wordDoc.AddNewPart<ExtendedFilePropertiesPart>("rId5");
// Add the ThumbnailPart part in the new Word document.
wordDoc.AddNewPart<ThumbnailPart>("image/jpeg", "rId6");
wordDoc.Close();
}
カスタム XML を含む新しいドキュメント パーツを追加するには
最初に、ソース Word 2007 文書のパスを表すパラメータを渡します。
次に、文書を WordprocessingDocument オブジェクトとして開きます。
次に、MainDocumentPart パーツ、CustomFilePropertiesPart パーツ、CoreFilePropertiesPart パーツ、DigitalSignatureOriginPart パーツ、ExtendedFilePropertiesPart パーツ、および ThumbailPart パーツを追加します。リレーションシップ Id を各パッケージ パーツに渡す方法に注目してください。
最後に、その文書を選択します。
注意
AddNewPart<T> メソッドは、現在のドキュメント パーツをソースとするリレーションシップを作成し、新しく作成したドキュメント パーツをターゲットにします。このメソッドは、新しく作成したドキュメント パーツを返します。さらに、FeedData() メソッドを使用してドキュメント パーツにデータを入力することもできます。