Format & Brand Your Word Documents

Aspose.Wordsを使用してWord文書にページをフォーマットし、ブランディングを追加する方法

ページの配置をカスタマイズし、ヘッダー、フット、ウォーターマークなどのブランド要素を適用することで、Word ドキュメントのプロフェッショナルな外観を向上させることができます Aspose.Words for .NET を使用すると、開発者はこれらの機能を正確にプログラム的に実装することができます。

要件: Word ドキュメントのカスタマイズのための環境を設定する

  • インストール → ネット SDK .
  • プロジェクトに Aspose.Words パッケージを追加する:dotnet add package Aspose.Words
  • 文書を作成する(template.docx) ページのフォーマットとブランディングをテストするために。

ページをフォーマットし、Wordファイルにブランドを追加するためのステップ・ステップガイド

ステップ1:カスタマイズのためのWordドキュメントをアップロード

using System;
using Aspose.Words;

class Program
{
    static void Main()
    {
        // Step 1: Load the Word document
        string filePath = "template.docx";
        Document doc = new Document(filePath);

        // Steps 2, 3, and 4 will be added below
    }
}

説明: このコードは、追加のカスタマイズのために指定されたWord文書をメモリにアップロードします。

ステップ2:ブランディングでヘッダーを追加

using System;
using Aspose.Words;

class Program
{
    static void Main()
    {
        string filePath = "template.docx";
        Document doc = new Document(filePath);

        // Step 2: Add a Header with Branding
        foreach (Section section in doc.Sections)
        {
            HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
            section.HeadersFooters.Add(header);

            Paragraph headerParagraph = new Paragraph(doc);
            headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
            header.Paragraphs.Add(headerParagraph);
        }

        // Steps 3 and 4 will be added below
    }
}

説明: このコードは、Word ドキュメントの各セクションにブランドテキストを含むタイトルを追加します。

ステップ3:ドキュメントに水マークを適用する

using System;
using Aspose.Words;
using System.Drawing;

class Program
{
    static void Main()
    {
        string filePath = "template.docx";
        Document doc = new Document(filePath);

        foreach (Section section in doc.Sections)
        {
            HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
            section.HeadersFooters.Add(header);

            Paragraph headerParagraph = new Paragraph(doc);
            headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
            header.Paragraphs.Add(headerParagraph);
        }

        // Step 3: Apply a Watermark
        AddWatermark(doc, "CONFIDENTIAL");

        // Step 4 will be added below
    }

    static void AddWatermark(Document doc, string watermarkText)
    {
        foreach (Section section in doc.Sections)
        {
            Shape watermark = new Shape(doc, ShapeType.TextPlainText)
            {
                TextPath = { Text = watermarkText, FontFamily = "Arial" },
                Width = 300,
                Height = 70,
                Rotation = -40,
                FillColor = Color.LightGray,
                StrokeColor = Color.LightGray,
                WrapType = WrapType.None,
                BehindText = true,
                RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
                RelativeVerticalPosition = RelativeVerticalPosition.Page,
                Left = 100,
                Top = 200
            };

            section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.AppendChild(watermark);
        }
    }
}

説明: このコードは、文書の各ページに「CONFIDENTIAL」の水標を追加します。

ステップ4:更新文書を保存する

using System;
using Aspose.Words;
using System.Drawing;

class Program
{
    static void Main()
    {
        string filePath = "template.docx";
        Document doc = new Document(filePath);

        foreach (Section section in doc.Sections)
        {
            HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
            section.HeadersFooters.Add(header);

            Paragraph headerParagraph = new Paragraph(doc);
            headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
            header.Paragraphs.Add(headerParagraph);
        }

        AddWatermark(doc, "CONFIDENTIAL");

        // Step 4: Save the Updated Document
        string outputPath = "FormattedDocument.docx";
        doc.Save(outputPath);

        Console.WriteLine("Document formatting and branding applied successfully.");
    }

    static void AddWatermark(Document doc, string watermarkText)
    {
        foreach (Section section in doc.Sections)
        {
            Shape watermark = new Shape(doc, ShapeType.TextPlainText)
            {
                TextPath = { Text = watermarkText, FontFamily = "Arial" },
                Width = 300,
                Height = 70,
                Rotation = -40,
                FillColor = Color.LightGray,
                StrokeColor = Color.LightGray,
                WrapType = WrapType.None,
                BehindText = true,
                RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
                RelativeVerticalPosition = RelativeVerticalPosition.Page,
                Left = 100,
                Top = 200
            };

            section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.AppendChild(watermark);
        }
    }
}

説明: このコードは、適用されたヘッダーとウォーターマークで修正された文書を保存します。

ステップ5:Wordドキュメントフォーマットソリューションをテスト

  • プログラムを実行し、以下のことを確認します。- タイトルはブランドテキストを含むすべてのページに追加されます。
  • 各ページに「CONFIDENTIAL」ウォーターマークが表示されます。

ホスティングオプション:さまざまなプラットフォームでWordドキュメント形式化ソリューションの導入

ウィンドウで実装

  • .NET の実行時間をインストールし、アプリケーションを IIS でホストして、より広範囲にアクセスできます。
  • アプリケーションを現地でテストするか、リモートで使用するか。

・Linuxで開発

  • ASP.NET Core の実行時間をインストールします。
  • Nginx を使用してアプリケーションにサービスを提供し、無線文書処理を可能にします。

マコスでの実装

  • Kestrel サーバーを使用してアプリケーションを現地でテストします。
  • スケール性のためのクラウド環境にソリューションを導入します。

Word ドキュメントでページをフォーマットする際の一般的な問題

  • 水の表示が表示されていない:- 水マークが各セクションのメインヘッダーに追加されることを確認します。

  • 特定のページに欠けているタイトル:- セクションのタイトルをチェックし、すべてのセクションでそれらが有効であることを確認します。

  • 文書調和の問題:- 水マークやヘッダーなどのブランド要素の位置を正確なコーディネートを使用して調整します。

このガイドに従って、カスタマイズされたブランド化と一貫したレイアウトでプロフェッショナルにフォーマットされたWordドキュメントを作成することができます。

 日本語