C#でWordの差し込み印刷テンプレートを作成し、テキストとイメージを結合する


Wordの差し込み印刷用テンプレートを作成し、異なるデータをテンプレートに適用することで、同じ形式であるが内容が異なるドキュメントをバッチ処理し、ドキュメントの処理効率を向上させることができます。 この資料では、Spire.Docコンポーネントを使用してWordの差し込み印刷テンプレートドキュメントを作成する方法、およびテキストとイメージを既存のテンプレートドキュメントに結合する方法について説明します。
使用する必要のあるツール:Spire.Doc for .NET

ステップ:
1.Spire.Doc for .NETをダウンロードしてインストールし、プロジェクトにSpire.Doc.dllファイルを参照してください。
2.Visual Studioにコードを挿入する:
差し込み印刷テンプレート文書を作成する
【C#】

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Meilmerge_Template2
{
    class Program
    {
        static void Main(string[] args)
        {
            //ドキュメントインスタンスを作成する 
            Document document = new Document();
            //セクションを追加
            Section section = document.AddSection();
            //段落を追加
            Paragraph paragraph = section.AddParagraph();

            //テキストを追加
            paragraph.AppendText("\n名 前 : ");
            //マージフィールド "Name"を追加
            paragraph.AppendField("Name", FieldType.FieldMergeField);

            //テキストを追加
            paragraph.AppendText("\n国 籍 : ");
            //差し込みフィールド"Country"を追加
            paragraph.AppendField("Country", FieldType.FieldMergeField);

            //テキストを追加
            paragraph.AppendText("\n故 郷 : ");
            //差し込みフィールド "Hometown"を追加
            paragraph.AppendField("Hometown", FieldType.FieldMergeField);

            //テキストを追加
            paragraph.AppendText("\n写 真 : ");
            //差し込みフィールド「写真」を追加
            paragraph.AppendField("Image:Photo", FieldType.FieldMergeField);

            //文書を保存して閉じる
            document.SaveToFile("テンプレート.docx", FileFormat.Docx2013);
        }
    }
}
'

デバッグしてコードを実行すると、生成されたドキュメントは次のようになります:

テキストと画像をテンプレートにマージする
【C#】

Program.cs
``
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Reporting;
using System.Drawing;

namespace Merge_Text_and_Image2
{
    class Program
    {
        static void Main(string[] args)
        {
            //テンプレート文書を読み込む 
            Document doc = new Document();
            doc.LoadFromFile("テンプレート.docx");

            var textFieldNames = new string[] { "Name", "Country", "Hometown" };
            var textFieldValues = new string[] { "豊 臣 は 仕 上 がっ た", "日 本", "大 阪 城" };

            var imageFieldNames = new string[] { "Photo" };
            var imageFieldValues = new string[] { "ピクチャ.jpg" };

            //テキストをテンプレートにマージする
            doc.MailMerge.Execute(textFieldNames, textFieldValues);

            //マージされた画像のカスタムイベントを作成する
           doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);

           //イメージをテンプレートにマージする
           doc.MailMerge.Execute(imageFieldNames, imageFieldValues);

           //ドキュメントを保存
           doc.SaveToFile("結果.docx", FileFormat.Docx);         

        }
        //画像を読み込む
        static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
        {
            string filePath = field.FieldValue as string;
            if (!string.IsNullOrEmpty(filePath))
            {
                field.Image = Image.FromFile(filePath);
            }
        }
    }
}
'

デバッグしてコードを実行すると、生成されたドキュメントは次のようになります: