次の方法で共有


ワープロ ドキュメントからヘッダーとフッターを削除する

このトピックでは、Open XML SDK for Office のクラスを使用して、ワープロ ドキュメント内のすべてのヘッダーとフッターをプログラムで削除する方法について説明します。 このタスクを示すメソッド RemoveHeadersAndFooters 例が含まれています。

RemoveHeadersAndFooters メソッド

RemoveHeadersAndFooters メソッドを使用して、ワープロ ドキュメントからすべてのヘッダーとフッターの情報を削除できます。 ドキュメント ストレージからヘッダーとフッターの部分を削除するだけでなく、ドキュメントからそれらのパーツへの参照も削除する必要があることに注意してください。 サンプル コードでは、操作の両方の手順を示します。 RemoveHeadersAndFooters メソッドは、変更するファイルのパスを示す文字列である 1 つのパラメーターを受け取ります。

static void RemoveHeadersAndFooters(string filename)

このメソッドの完全なコードについては、「サンプル コード」セクションを参照してください。

サンプル メソッドの呼び出し

サンプル メソッドを呼び出すには、次のコード例に示すように、第 1 のパラメーターで変更するドキュメントのファイル名を含んだ文字列を渡します。

string filename = args[0];

RemoveHeadersAndFooters(filename);

コードの動作のしくみ

RemoveHeadersAndFooters メソッドは、指定したドキュメントで機能し、ヘッダーとフッターのすべてのパーツとそれらのパーツへの参照を削除します。 コードはまず、 Open メソッドを使用してドキュメントを開き、読み取り/書き込みアクセス (最後の true パラメーター) のためにドキュメントを開く必要があることを示します。 開いているドキュメントを指定すると、コードは MainDocumentPart プロパティを使用してメイン ドキュメントに移動し、参照を docPart という名前の変数に格納します。

// Given a document name, remove all of the headers and footers
// from the document.
using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
{
    if (doc.MainDocumentPart is null)
    {
        throw new ArgumentNullException("MainDocumentPart is null.");
    }

    // Get a reference to the main document part.
    var docPart = doc.MainDocumentPart;

ヘッダー/フッターの存在の確認

ドキュメント パーツへの参照を指定すると、次に、ドキュメントにヘッダーやフッターが含まれているかどうかなど、何らかの作業があるかどうかを判断します。 決定するために、コードはドキュメント パーツの HeaderParts プロパティと FooterParts プロパティの両方の Count メソッドを呼び出し、どちらかが 0 より大きい値を返す場合、コードは続行されます。 HeaderPartsプロパティと FooterParts プロパティはそれぞれ、HeaderPart または FooterPart オブジェクトのIEnumerable<T>を返します。

// Count the header and footer parts and continue if there 
// are any.
if (docPart.HeaderParts.Count() > 0 || docPart.FooterParts.Count() > 0)
{

ヘッダーとフッターの部分への参照のコレクションを指定すると、それぞれを個別に削除するコードを記述できますが、Open XML SDK のために必要ありません。 代わりに、 DeleteParts メソッドを呼び出し、削除するパーツのコレクションを渡すことができます。この単純なメソッドは、パーツのコレクションを削除するためのショートカットを提供します。 そのため、ループ処理のコード記述の必要はなく、次の数行のコードで済ませることができます。

// Remove the header and footer parts.
docPart.DeleteParts(docPart.HeaderParts);
docPart.DeleteParts(docPart.FooterParts);

この時点で、コードはヘッダーとフッターの部分を削除しましたが、ドキュメントにはそれらのパーツへの孤立した参照がまだ含まれています。 孤立した参照を削除する前に、コードはドキュメントのコンテンツ (つまり、メイン ドキュメント パーツに含まれる XML コンテンツ) への参照を取得する必要があります。

一元化された参照を削除するために、コードは最初に HeaderReference 要素のコレクションを取得し、コレクションを Listに変換してから、コレクションをループして、見つかった各要素の Remove() メソッドを呼び出します。 このコードでは、Descendants() メソッドによって返されたIEnumerableListに変換し、リストから項目を削除できるようにし、Open XML SDK によって提供されるHeaderReference型によって XML コンテンツ内のHeaderReference型の要素を簡単に参照できることに注意してください。 すべてのヘッダーを削除した後で、フッター要素についても同じ操作を繰り返します。

// Get a reference to the root element of the main
// document part.
Document document = docPart.Document;

// Remove all references to the headers and footers.

// First, create a list of all descendants of type
// HeaderReference. Then, navigate the list and call
// Remove on each item to delete the reference.
var headers = document.Descendants<HeaderReference>().ToList();

foreach (var header in headers)
{
    header.Remove();
}

// First, create a list of all descendants of type
// FooterReference. Then, navigate the list and call
// Remove on each item to delete the reference.
var footers = document.Descendants<FooterReference>().ToList();

foreach (var footer in footers)
{
    footer.Remove();
}

サンプル コード

C# と Visual Basic の完全な RemoveHeadersAndFooters コード サンプルを次に示します。

// Remove all of the headers and footers from a document.
static void RemoveHeadersAndFooters(string filename)
{
    // Given a document name, remove all of the headers and footers
    // from the document.
    using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
    {
        if (doc.MainDocumentPart is null)
        {
            throw new ArgumentNullException("MainDocumentPart is null.");
        }

        // Get a reference to the main document part.
        var docPart = doc.MainDocumentPart;

        // Count the header and footer parts and continue if there 
        // are any.
        if (docPart.HeaderParts.Count() > 0 || docPart.FooterParts.Count() > 0)
        {

            // Remove the header and footer parts.
            docPart.DeleteParts(docPart.HeaderParts);
            docPart.DeleteParts(docPart.FooterParts);

            // Get a reference to the root element of the main
            // document part.
            Document document = docPart.Document;

            // Remove all references to the headers and footers.

            // First, create a list of all descendants of type
            // HeaderReference. Then, navigate the list and call
            // Remove on each item to delete the reference.
            var headers = document.Descendants<HeaderReference>().ToList();

            foreach (var header in headers)
            {
                header.Remove();
            }

            // First, create a list of all descendants of type
            // FooterReference. Then, navigate the list and call
            // Remove on each item to delete the reference.
            var footers = document.Descendants<FooterReference>().ToList();

            foreach (var footer in footers)
            {
                footer.Remove();
            }
        }
    }
}